forked from shawnwildermuth/StructuredApis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientApi.cs
95 lines (80 loc) · 2.32 KB
/
ClientApi.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
using StructuredApis.Data;
using StructuredApis.Data.Entities;
public class ClientApi : IApi
{
private readonly ILogger<ClientApi> _logger;
public ClientApi(ILogger<ClientApi> logger)
{
_logger = logger;
}
public void Register(WebApplication app)
{
app.MapGet("/api/clients", GetAll);
app.MapGet("/api/clients/{id}", Get);
app.MapPost("/api/clients", Post);
app.MapPut("/api/clients/{id}", Put);
app.MapDelete("/api/clients/{id}", Delete);
}
public async Task<IResult> Get(int id, IJurisRepository repository)
{
var result = await repository.GetClientAsync(id);
if (result is null) return Results.NotFound();
return Results.Ok(result);
}
public async Task<IResult> GetAll(IJurisRepository repository)
{
return Results.Ok(await repository.GetClientsAsync());
}
public async Task<IResult> Post(Client client, IJurisRepository repository)
{
try
{
repository.Add(client);
if (await repository.SaveAll())
{
return Results.Created($"/clients/{client.Id}", client);
}
}
catch (Exception ex)
{
return Results.BadRequest($"Error Occurred while posting to Client: {ex}");
}
return Results.BadRequest("Failed to Post Client");
}
public async Task<IResult> Put(int id, Client old, IJurisRepository repository)
{
try
{
if (!(await repository.HasClientAsync(id))) return Results.NotFound();
repository.Update(old); // Should do merge, but this is just replacing it
if (await repository.SaveAll())
{
return Results.Ok(old);
}
}
catch (Exception ex)
{
_logger.LogError($"Error Occurred while updating Client: {ex}");
return Results.BadRequest($"Error Occurred while updating Client: {ex}");
}
return Results.BadRequest("Failed to Update Client");
}
public async Task<IResult> Delete(int id, IJurisRepository repository)
{
try
{
var client = await repository.GetClientAsync(id);
if (client is null) return Results.NotFound();
repository.Delete(client);
if (await repository.SaveAll())
{
return Results.Ok();
}
}
catch (Exception ex)
{
return Results.BadRequest($"Error Occurred while deleting to Client: {ex}");
}
return Results.BadRequest("Failed to Delete Client");
}
}