Running a query over http using servicestack ormlite - ormlite-servicestack

I have an app running in the browser. The client is fetching data from the server using .net core WEBAPI. normal REST requests. Something like a criteria parser...
I wonder if there is a way to pass search criteria from the client to the server in order to query the database. converting a get request to a ormlite query is possible but I wonder if it's already done...
I'm talking about serverside implementation, paging, sorting, searching (and,or) and so on...
I have a license for ormlite only.
Thanks

This sounds almost exactly like what ServiceStack's AutoQuery already does, where it's able to implement the Query Service for your RDBMS Table from just a Request DTO definition, e.g:
[Route("/movies")]
public class FindMovies : QueryDb<Movie>
{
public string[] Ratings { get; set; }
}
Where you will then be able to call your Service with either a Typed Service Client:
var movies = client.Get(new FindMovies { Ratings = new[]{"G","PG-13"} })
Or via a HTTP request:
/movies?ratings=G,PG-13

Related

.NET Core 6 + Blazor: How to call server while only passing data in the uri

My question is about how to do the following with .NET Core 6 Blazor. If I knew how to simply make Blazor send some URI to the server, I would just do so, but I don't. I have googled for this quite extensively, but not found an answer.
I want to modify a single property of a record stored in a table. The server should load the data record with the given id, increment or decrement a counter stored in it, and save the record. There is no need to send the data to the client, have it modify the data, and have it send the modified data back to the server. Basically I am trying to model kind of RPC with Blazor.
So I want to make a call to my .NET Core 6 Blazor server, only passing a parameter in the URI (the ID of the database record to be modified).
Is there a method I could call that doesn't require a content parameter like PostAsync() and PutAsync() do? Right now I am doing this:
public static async Task<bool> AddReference (Guid ID)
{
HttpResponseMessage response =
await httpClient.PostAsync(Routes.WatermarkApi + $"AddReference/{ID}", null);
return (response.StatusCode == System.Net.HttpStatusCode.OK);
}
passing null for the content parameter, but this looks a little awkward to me.

Validate Values before Apply PATCH in REST

I'm looking for a way to validate and check the values of the model that is sent to my PATCH method for updating. I haven't found my answer yet.
I've read the JsonPatch Documentation but I didn't find what I want.
Scenario: I have a RESTful Web API, and say I have a resource named "users". Say a client wants to partially update a "user" resource: (PATCH api/users/{id}), when the client sends a JsonPatch Document, for example, I have to check if the email is sent for updating and if yes, I have to check if the email wasn't duplicate. So, how could I do this in a method like below: (I use ASP.NET Web API)
[HttpPatch]
[Route("{userId}")]
public HttpResponseMessage UpdateUser(int userId, JsonPatch.JsonPatchDocument<User> patchDocument)
{
// I could do this but before that I wanna validate the proeprties:
patchDocument.ApplyUpdatesTo(dbContext.Users.Single(u => u.Id == userId));
}

ASP.NET DbSet Join

I have two classes, client and user. User is a field in client (there is a foreign key). I am trying to do a join, getting all the clients and the related user (it is one to one).
I am using Entity Framework and a web service that gives me my data.
I currently am getting all my clients like:
public DbSet<Client> getClients()
{
return context.Clients;
}
I need to also get the related object user. I found an example that tells me to do:
public DbSet<Client> getClients()
{
return context.Clients.include(x => x.User);
}
This throws an exception, I need to be working with IQueryable. If I change my function the connection to the web service does not work.
How do I do what I am trying to do?
EDIT:
The exception I get from the webservice is An error occurred while receiving the HTTP response to http://localhost:60148/WebService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
try these links:
Expose IQueryable Over WCF Service
and IQueryable problems using WCF you need to send something else back, like a List, as IQueryable is actually a query, which you can't send back. the links above provide alternative ways to do it, to get around the IQueryable restriction.

Cache database results in WebAPI authentication

I am creating a RESTful web service using ASP.NET WebApi. I am requiring that all incoming requests go over SSL because I will be using Basic HTTP Authentication. The client is responsible for including the credentials in the request header and the web service will authenticate the client on each request.
The actual authentication requires making a database call. Is there a way to cache the database results (the username and password) so that I don't have to make a database call for every incoming request that occurs in a short period of time?
So when a request comes in the web service will look for the username/password combo in the cache. If it is found it will process the request. If it isn't found, it will make the database call to authenticate and then add the username/password to the cache.
The easiest cache that I can think of to use would be using System.Web.Caching. Below is an extremely simplified version of adding a value to the current cache and then retrieving it for processing. I would not recommend using this code as is.
// GET api/values/5
[HttpGet]
public string GetValue(int id)
{
HttpContext.Current.Cache.Insert("2", "test1");
var value = Convert.ToString(HttpContext.Current.Cache.Get(id.ToString()));
return !string.IsNullOrEmpty(value) ? value : "nothing found";
}

What is the query string of a BlazeDS request?

I have a Tomcat service running on localhost:8080 and I have installed BlazeDS. I created and configured a simple hello world application like this...
package com.adobe.remoteobjects;
import java.util.Date;
public class RemoteServiceHandler {
public RemoteServiceHandler()
{
//This is required for the Blaze DS to instantiate the class
}
public String getResults(String name)
{
String result = “Hi ” + name + “, the time is : ” + new Date();
return result;
}
}
With what query string can I invoke RemoteServiceHandler to my Tomcat instance via just a browser? Something like... http://localhost:8080/blazeds/?xyz
Unfortunately you can't. First the requests (and responses) are encoded in AMF and second I believe they have to be POSTs. If you dig through the BlazeDS source code and the Flex SDK's RPC library you can probably figure out what it's sending. But AFAIK this hasn't been documented anywhere else.
I think that AMFX (which is AMF in XML) will work for you, using HTTPChannel instead of AMFChannel.
From http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=lcarch_2.html#1073189, Channels and channel sets:
Flex clients can use different channel
types such as the AMFChannel and
HTTPChannel. Channel selection depends
on a number of factors, including the
type of application you are building.
If non-binary data transfer is
required, you would use the
HTTPChannel, which uses a non-binary
format called AMFX (AMF in XML). For
more information about channels, see
Channels and endpoints.
This way you can use simple netcat to send the request.
Not sure how authentication will be handled though, you will probably need do a login using Flash, extract the authentication cookie and then submit it as part of your request.
Please update this thread once you make progress so that we all can learn.

Resources