/
Endpoints.cs
121 lines (91 loc) · 3.11 KB
/
Endpoints.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using JasperFx.Core;
using Marten;
using Wolverine;
using Wolverine.Http;
using Wolverine.Marten;
namespace MultiTenantedTodoWebService;
#region sample_Todo
public class Todo
{
public int Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
}
#endregion
public record CreateTodo(string Name);
public record UpdateTodo(int Id, string Name, bool IsComplete);
public record DeleteTodo(int Id);
public record TodoCreated(int Id);
public static class TodoEndpoints
{
#region sample_get_all_todos
// The "tenant" route argument would be the route
[WolverineGet("/todoitems/{tenant}")]
public static Task<IReadOnlyList<Todo>> Get(string tenant, IQuerySession session)
{
return session.Query<Todo>().ToListAsync();
}
#endregion
[WolverineGet("/todoitems/{tenant}/complete")]
public static Task<IReadOnlyList<Todo>> GetComplete(IQuerySession session)
=> session
.Query<Todo>()
.Where(x => x.IsComplete)
.ToListAsync();
// Wolverine can infer the 200/404 status codes for you here
// so there's no code noise just to satisfy OpenAPI tooling
[WolverineGet("/todoitems/{tenant}/{id}")]
public static Task<Todo?> GetTodo(int id, IQuerySession session, CancellationToken cancellation)
{
return session.LoadAsync<Todo>(id, cancellation);
}
#region sample_calling_invoke_for_tenant_async_with_expected_result
[WolverinePost("/todoitems/{tenant}")]
public static CreationResponse<TodoCreated> Create(
// Only need this to express the location of the newly created
// Todo object
string tenant,
CreateTodo command,
IDocumentSession session)
{
var todo = new Todo { Name = command.Name };
// Marten itself sets the Todo.Id identity
// in this call
session.Store(todo);
// New syntax in Wolverine.HTTP 1.7
// Helps Wolverine
return CreationResponse.For(new TodoCreated(todo.Id), $"/todoitems/{tenant}/{todo.Id}");
}
#endregion
#region sample_invoke_for_tenant
// While this is still valid....
[WolverineDelete("/todoitems/{tenant}/longhand")]
public static async Task Delete(
string tenant,
DeleteTodo command,
IMessageBus bus)
{
// Invoke inline for the specified tenant
await bus.InvokeForTenantAsync(tenant, command);
}
// Wolverine.HTTP 1.7 added multi-tenancy support so
// this short hand works without the extra jump through
// "Wolverine as Mediator"
[WolverineDelete("/todoitems/{tenant}")]
public static void Delete(
DeleteTodo command, IDocumentSession session)
{
// Just mark this document as deleted,
// and Wolverine middleware takes care of the rest
// including the multi-tenancy detection now
session.Delete<Todo>(command.Id);
}
#endregion
}
public static class TodoCreatedHandler
{
public static void Handle(DeleteTodo command, IDocumentSession session)
{
session.Delete<Todo>(command.Id);
}
}