带有 Crud 操作的 ServiceStack Todo Rest Api - 如何覆盖 Get、Post.. 等方法?
ServiceStack Todo Rest Api with Crud operations - How to override Get, Post.. etc methods?
这是我在 ServiceStack 中的第一步。我是个菜鸟。
我使用 MySql 数据库连接创建了简单的 Todo Rest Api。我正在尝试根据日期(大于或等于给定日期)获取所有传入的待办事项。
我拥有的是传递 DateTo 参数的简单 Get 方法。
它仅在日期等于现有日期时才有效,例如:
我想要实现的是修改我可以指定日期不相等但应该大于或等于的方法。
如何修改 Post、Get、Update.. 方法?
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,
});
}
}
v6 发行说明有 TODO MVC examples for all its jamstacks.net 个模板,其 C# 和 UI 源代码在其每个页面上都有链接:
- https://vue-ssg.jamstacks.net/todomvc
- https://blazor-wasm.jamstacks.net/todomvc
- https://nextjs.jamstacks.net/todomvc
它利用了新的 InMemory AutoQuery PocoDataSource which lets you query each property with AutoQuery's Implicit Conventions:
如果您的 AutoQuery API 数据模型有 DateTo
属性 那么您将能够执行 "大于或等于" 查询:
- /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
等等
这是我在 ServiceStack 中的第一步。我是个菜鸟。
我使用 MySql 数据库连接创建了简单的 Todo Rest Api。我正在尝试根据日期(大于或等于给定日期)获取所有传入的待办事项。
我拥有的是传递 DateTo 参数的简单 Get 方法。
我想要实现的是修改我可以指定日期不相等但应该大于或等于的方法。
如何修改 Post、Get、Update.. 方法?
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,
});
}
}
v6 发行说明有 TODO MVC examples for all its jamstacks.net 个模板,其 C# 和 UI 源代码在其每个页面上都有链接:
- https://vue-ssg.jamstacks.net/todomvc
- https://blazor-wasm.jamstacks.net/todomvc
- https://nextjs.jamstacks.net/todomvc
它利用了新的 InMemory AutoQuery PocoDataSource which lets you query each property with AutoQuery's Implicit Conventions:
如果您的 AutoQuery API 数据模型有 DateTo
属性 那么您将能够执行 "大于或等于" 查询:
- /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
等等