/
Program.cs
121 lines (97 loc) · 4.39 KB
/
Program.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
120
121
using System.Net.Mime;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OperationResults.AspNetCore.Http;
using OperationResults.Sample.BusinessLayer;
using OperationResults.Sample.BusinessLayer.Services;
using OperationResults.Sample.BusinessLayer.Services.Interfaces;
using OperationResults.Sample.DataAccessLayer;
using OperationResults.Sample.Shared.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseInMemoryDatabase("ApplicationDatabase");
});
builder.Services.AddScoped<IPeopleService, PeopleService>();
builder.Services.AddScoped<IImageService, ImageService>();
builder.Services.AddOperationResult(options =>
{
options.ErrorResponseFormat = ErrorResponseFormat.Default;
// Adds a custom mapping between CustomFailureReasons.NotAvailable (1001) and the 501 HTTP Status Code.
// Adding new mappings or editing the existing ones allows to define what HTTP Status Codes the API must return
// for the Operation Results of our Business Logic methods.
options.StatusCodesMapping.Add(CustomFailureReasons.NotAvailable, StatusCodes.Status501NotImplemented);
// If you just want to directly use HTTP status codes as failure reasons, set the following property to true.
// In this way, the code you use with Result.Fail() will be used as response status code with no further mapping.
//options.UseHttpStatusCodes = true;
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
var peopleApi = app.MapGroup("api/people");
peopleApi.MapGet("/", async (IPeopleService peopleService, HttpContext httpContext) =>
{
// You can collapse the following instructions into a single one.
var result = await peopleService.GetAsync();
var response = httpContext.CreateResponse(result); // Or result.ToResponse(httpContext)
return response;
})
.Produces<IEnumerable<Person>>()
.WithOpenApi();
peopleApi.MapGet("{id:guid}", async (Guid id, IPeopleService peopleService, HttpContext httpContext) =>
{
// You can collapse the following instructions into a single one.
var result = await peopleService.GetAsync(id);
var response = httpContext.CreateResponse(result); // Or result.ToResponse(httpContext)
return response;
})
.Produces<Person>()
.Produces(StatusCodes.Status404NotFound)
.WithName("GetPerson")
.WithOpenApi();
peopleApi.MapGet("{id:guid}/with-image", async (Guid id, IPeopleService peopleService, HttpContext httpContext) =>
{
// You can collapse the following instructions into a single one.
var result = await peopleService.GetWithImageAsync(id);
var response = httpContext.CreateResponse(result); // Or result.ToResponse(httpContext)
return response;
})
.Produces<PersonWithImage>()
.Produces(StatusCodes.Status404NotFound)
.WithName("GetPersonWithImage")
.WithOpenApi();
peopleApi.MapPost("/", async (Person person, IPeopleService peopleService, HttpContext httpContext) =>
{
// You can collapse the following instructions into a single one.
var result = await peopleService.SaveAsync(person);
var response = httpContext.CreateResponse(result, "GetPerson", new { id = result.Content?.Id }); // Or result.ToResponse(httpContext, nameof(GetPerson), new { id = result.Content?.Id });
return response;
})
.Produces<Person>(StatusCodes.Status201Created)
.ProducesProblem(StatusCodes.Status400BadRequest, MediaTypeNames.Application.Json)
.WithOpenApi();
peopleApi.MapDelete("{id:guid}", async (Guid id, IPeopleService peopleService, HttpContext httpContext) =>
{
// You can collapse the following instructions into a single one.
var result = await peopleService.DeleteAsync(id);
var response = httpContext.CreateResponse(result); // Or result.ToResponse(httpContext)
return response;
})
.Produces(StatusCodes.Status204NoContent)
.Produces(StatusCodes.Status404NotFound)
.WithOpenApi();
app.MapGet("api/image", async (IImageService imageService, HttpContext httpContext)
=> httpContext.CreateResponse(await imageService.GetImageAsync())
)
.Produces<FileContentResult>(StatusCodes.Status200OK, contentType: MediaTypeNames.Application.Octet)
.Produces(StatusCodes.Status404NotFound)
.WithOpenApi();
app.Run();