Home > database >  ServiceStack Todo Rest Api with Crud operations - How to override Get, Post.. etc methods?
ServiceStack Todo Rest Api with Crud operations - How to override Get, Post.. etc methods?

Time:04-27

These are my first steps in ServiceStack. I am a total noob.

I created simple Todo Rest Api with MySql Db connection. I am trying to Get all the incoming Todos based on date (greater than or equal given date).

What I have is simple Get method where DateTo paramater is passed. enter image description here It works only if the date is equal to the existing, example : enter image description here

What I am trying to achieve is to modyfy the method that I can specyfy that the date is not equal but should be great than or equal.

How can I modify Post, Get, Update.. methods?

Types.cs

using System;
using ServiceStack.DataAnnotations;

namespace ToDoApp.ServiceModel.Types
{
    public class Todo
    {
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        public string Description { get; set; }
        public int Percentage { get; set; }
        public DateTime DateTo { get; set; }
        public int Done { get; set; }
    }

}

GetIncomingTodo.cs

using ServiceStack;
using System;
using ToDoApp.ServiceModel.Types;

namespace ToDoApp.ServiceModel
{
    [Route("/todo/incoming", "GET")]
    [Route("/todo/incoming/{DateTo}", "GET")]
    public class GetIncomingTodo
        : QueryDb<Todo>, IReturn<QueryResponse<Todo>>, IGet
    {
        public DateTime DateTo { get; set; }

    }

}


MyServices.cs

using ServiceStack;
using ToDoApp.ServiceModel;
using ToDoApp.ServiceModel.Types;

namespace ToDoApp.ServiceInterface;

public class MyServices : Service
{
    public object Any(Test request)
    {
        return new TestResponse { Result = $"Hello, {request.Title}!" };
    }
}

Program.cs

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
    app.UseHttpsRedirection();
}
app.UseServiceStack(new AppHost());

app.Run();

Configure.AppHost.cs

using Funq;
using ServiceStack;
using ToDoApp.ServiceInterface;

[assembly: HostingStartup(typeof(ToDoApp.AppHost))]

namespace ToDoApp;

public class AppHost : AppHostBase, IHostingStartup
{
    public void Configure(IWebHostBuilder builder) => builder
        .ConfigureServices(services => {
            // Configure ASP.NET Core IOC Dependencies
        });

    public AppHost() : base("ToDoApp", typeof(MyServices).Assembly) {}

    public override void Configure(Container container)
    {
        // Configure ServiceStack only IOC, Config & Plugins
        SetConfig(new HostConfig {
            UseSameSiteCookies = true,
        });

    }
}

CodePudding user response:

The v6 Release Notes has TODO MVC examples for all its jamstacks.net templates whose C# & UI source code is linked on each of its pages:

It makes use of the new InMemory AutoQuery PocoDataSource which lets you query each property with AutoQuery's Implicit Conventions:

If your AutoQuery API Data Model had a DateTo property then you would be able to perform a "Greater than or equal to" query with any of:

 - /todos?DateTo>=2022-01-01
 - /todos?DateToOnOrAfter=2022-01-01
 - /todos?DateToFrom=2022-01-01
 - /todos?FromDateTo=2022-01-01
 - /todos?DateToSince=2022-01-01
 - /todos?DateToGreaterThanOrEqualTo=2022-01-01

etc.

  • Related