LIMITING FIELDS IN API REQUEST  Topic is solved

Discussions relating to the REST API of Jiwa 7.

LIMITING FIELDS IN API REQUEST

Postby JuiceyBrucey » Mon Mar 01, 2021 7:10 pm

Hi,
I have the following query but need to limit the fields so that it runs fast and I do not get data I dont need.

public MyGetInventoryBlockResponse Get(MyGetInventoryBlockRequest request)
{
var query = Db.From<IN_Main>().Join<IN_Main, IN_SellingPrices>((main, prices) => main.InventoryID == prices.InventoryID).Limit(request.StartPosition,request.EndPosition);
return new MyGetInventoryBlockResponse() { RecordResults = Db.Select(query) } ;
}

What is the best way of doing this?
Cheers
JuiceyBrucey
Frequent Contributor
Frequent Contributor
 
Posts: 132
Joined: Tue Aug 18, 2020 7:19 pm
Topics Solved: 1

Re: LIMITING FIELDS IN API REQUEST

Postby Mike.Sheen » Tue Mar 02, 2021 12:16 pm

JuiceyBrucey wrote:need to limit the fields so that it runs fast and I do not get data I dont need.


Without knowing what your constraints and requirements are, it's hard to answer that.

What I think your ormlite is currently doing is getting rows x to y from a query like:

Code: Select all
SELECT *
FROM IN_Main
JOIN IN_SellingPrices ON IN_Main.InventoryID = IN_SellingPrices.InventoryID
ORDER BY IN_Main.PartNo
OFFSET request.StartPosition ROWS
FETCH NEXT request.EndPosition - request.StartPosition ROWS ONLY


So you're asking for all rows between request.StartPosition and request.EndPosition - that may be a lot or a little - depending on what those values provided are.

If you run a SQL Profiler trace to capture the actual SQL query issued by your service, and then post that here along with what you would want your SQL query to look like I can try to reverse-engineer that and give you the ormlite syntax to match.


EDIT - just realised you said you wanted to limit the fields returned... my mind read rows for some reason.

Can you provide your MyGetInventoryBlockRequest and MyGetInventoryBlockResponse classes? Even better would be a small isolated plugin with all I need to get your service + route working.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: LIMITING FIELDS IN API REQUEST

Postby JuiceyBrucey » Tue Mar 02, 2021 2:05 pm

Yes, to clarify, I dont want all fields/columns returned. I want to specify which fields to return and not any others.

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using JiwaFinancials.Jiwa;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;
using ServiceStack;
using ServiceStack.DataAnnotations;
using ServiceStack.Model;
using JiwaFinancials.Jiwa.JiwaServiceModel.Tables;
using ServiceStack.Auth;
using System.Linq;
using ServiceStack.OrmLite;

namespace JiwaFinancials.Jiwa.JiwaServiceModel
{
public class RESTAPIPlugin : System.MarshalByRefObject, JiwaFinancials.Jiwa.JiwaApplication.IJiwaRESTAPIPlugin
{
public void Configure(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, ServiceStack.ServiceStackHost AppHost, Funq.Container Container, JiwaApplication.Manager JiwaApplicationManager)
{
AppHost.RegisterService<CustomServices>();

AppHost.Routes.Add(typeof(MyGetInventoryBlockRequest), "/Custom/GetInventoryBlock/{StartPosition}/{EndPosition}", "GET", "Retrieves a block of inventory specified by start and end position.", "");
}
}

#region "Requests"
[Serializable()]
[ApiResponse(200, "Records retrieved OK")]
[ApiResponse(401, "Not authenticated")]
[ApiResponse(404, "No debtor with the Account No. provided was found")]
public class MyGetInventoryBlockRequest : IReturn<MyGetInventoryBlockResponse>
{
public int StartPosition{ get; set; }
public int EndPosition { get; set; }
}

#endregion

#region "Responses"
[Serializable()]
public class MyGetInventoryBlockResponse
{
public List<IN_Main> RecordResults {get; set;}
}
#endregion

#region "Services"
public class CustomServices : Service
{
[Authenticate]
public MyGetInventoryBlockResponse Get(MyGetInventoryBlockRequest request)
{

var query = Db.From<IN_Main>().Join<IN_Main, IN_SellingPrices>((main, prices) => main.InventoryID == prices.InventoryID).Limit(request.StartPosition,request.EndPosition);
return new MyGetInventoryBlockResponse() { RecordResults = Db.Select(query) } ;
}

}
#endregion
}
JuiceyBrucey
Frequent Contributor
Frequent Contributor
 
Posts: 132
Joined: Tue Aug 18, 2020 7:19 pm
Topics Solved: 1

Re: LIMITING FIELDS IN API REQUEST  Topic is solved

Postby Mike.Sheen » Tue Mar 02, 2021 6:33 pm

I can't immediately see a way of doing this using an OrmLite query which is an ad-hoc join - you might want to check the official docs - there may be something in their examples.

Personally, for a curated result like that, I'd create a simple view and use AutoQuery on that instead.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: LIMITING FIELDS IN API REQUEST

Postby Scott.Pearce » Wed Mar 10, 2021 11:57 am

Moved to appropriate forum.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221


Return to REST API

Who is online

Users browsing this forum: No registered users and 3 guests

cron