todo-minimal-api.git

git clone https://git.crispbyte.dev/todo-minimal-api.git

todo-minimal-api.git / TodoMinimal.WebApi
CheddarCrisp  ·  2024-02-12

Program.cs

 1const string BASE_URL = "http://localhost:5143";
 2
 3var builder = WebApplication.CreateBuilder(args);
 4
 5// Add services to the container.
 6// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
 7builder.Services.AddEndpointsApiExplorer();
 8builder.Services.AddSwaggerGen();
 9builder.Services.AddCors(options => {
10    options.AddDefaultPolicy(policy => {
11        policy.AllowAnyOrigin();
12        policy.AllowAnyMethod();
13        policy.AllowAnyHeader();
14    });
15});
16
17var app = builder.Build();
18
19// Configure the HTTP request pipeline.
20if (app.Environment.IsDevelopment())
21{
22    app.UseSwagger();
23    app.UseSwaggerUI();
24}
25
26app.UseCors();
27
28var repo = new TodoRepo();
29var service = new TodoService(BASE_URL, repo);
30
31app.MapGet("/", service.All)
32.WithName("GetTodos")
33.WithOpenApi();
34
35app.MapGet("/{id}", service.Find)
36.WithName("GetTodo")
37.WithOpenApi();
38
39app.MapPost("/", service.Add)
40.WithName("CreateTodo")
41.WithOpenApi();
42
43app.MapDelete("/", service.Clear)
44.WithName("DeleteTodos")
45.WithOpenApi();
46
47app.MapPatch("/{id}", service.Update)
48.WithName("UpdateTodo")
49.WithOpenApi();
50
51app.MapDelete("/{id}", service.Delete)
52.WithName("DeleteTodo")
53.WithOpenApi();
54
55app.Run(BASE_URL);