|
| 1 | +// |
| 2 | +// Copyright Grafana Labs |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | + |
| 6 | +using Microsoft.EntityFrameworkCore; |
| 7 | + |
| 8 | +namespace aspnetcore; |
| 9 | + |
| 10 | +public static class TodoAppEndpoints |
| 11 | +{ |
| 12 | + public static IServiceCollection AddTodoApp(this IServiceCollection services) |
| 13 | + { |
| 14 | + services.AddSingleton(TimeProvider.System); |
| 15 | + services.AddScoped<TodoRepository>(); |
| 16 | + |
| 17 | + services.AddDbContext<TodoContext>((serviceProvider, options) => |
| 18 | + { |
| 19 | + var configuration = serviceProvider.GetRequiredService<IConfiguration>(); |
| 20 | + var dataDirectory = configuration["DataDirectory"]; |
| 21 | + |
| 22 | + if (string.IsNullOrEmpty(dataDirectory) || !Path.IsPathRooted(dataDirectory)) |
| 23 | + { |
| 24 | + var environment = serviceProvider.GetRequiredService<IHostEnvironment>(); |
| 25 | + dataDirectory = Path.Combine(environment.ContentRootPath, "App_Data"); |
| 26 | + } |
| 27 | + |
| 28 | + if (!Directory.Exists(dataDirectory)) |
| 29 | + { |
| 30 | + Directory.CreateDirectory(dataDirectory); |
| 31 | + } |
| 32 | + |
| 33 | + var databaseFile = Path.Combine(dataDirectory, "TodoApp.db"); |
| 34 | + |
| 35 | + options.UseSqlite("Data Source=" + databaseFile); |
| 36 | + }); |
| 37 | + |
| 38 | + return services; |
| 39 | + } |
| 40 | + |
| 41 | + public static IEndpointRouteBuilder MapTodoApp(this IEndpointRouteBuilder builder) |
| 42 | + { |
| 43 | + var todos = builder.MapGroup("/api/todo/items").WithTags("Todo"); |
| 44 | + { |
| 45 | + todos.MapPost("/", async (CreateTodoItemModel model, TodoRepository repository) => |
| 46 | + { |
| 47 | + var id = await repository.AddItemAsync(model.Text); |
| 48 | + return Results.Created($"/api/items/{id}", new { Id = id }); |
| 49 | + }); |
| 50 | + |
| 51 | + todos.MapGet("/", async (TodoRepository repository) => await repository.GetItemsAsync()); |
| 52 | + |
| 53 | + todos.MapGet("/{id}", async (Guid id, TodoRepository repository) => await repository.GetItemAsync(id)); |
| 54 | + |
| 55 | + todos.MapPost("/{id}/complete", async (Guid id, TodoRepository repository) => |
| 56 | + { |
| 57 | + return await repository.CompleteItemAsync(id) switch |
| 58 | + { |
| 59 | + true => Results.NoContent(), |
| 60 | + false => Results.Problem(statusCode: StatusCodes.Status400BadRequest), |
| 61 | + _ => Results.Problem(statusCode: StatusCodes.Status404NotFound), |
| 62 | + }; |
| 63 | + }); |
| 64 | + |
| 65 | + todos.MapDelete("/{id}", async (Guid id, TodoRepository repository) => |
| 66 | + { |
| 67 | + var deleted = await repository.DeleteItemAsync(id); |
| 68 | + return deleted switch |
| 69 | + { |
| 70 | + true => Results.NoContent(), |
| 71 | + false => Results.Problem(statusCode: StatusCodes.Status404NotFound), |
| 72 | + }; |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + return builder; |
| 77 | + } |
| 78 | + |
| 79 | + public class TodoContext(DbContextOptions<TodoContext> options) : DbContext(options) |
| 80 | + { |
| 81 | + public DbSet<TodoItem> Items { get; set; } = default!; |
| 82 | + } |
| 83 | + |
| 84 | + public class TodoRepository(TimeProvider timeProvider, TodoContext context) |
| 85 | + { |
| 86 | + public async Task<TodoItem> AddItemAsync(string text) |
| 87 | + { |
| 88 | + await this.EnsureDatabaseAsync(); |
| 89 | + |
| 90 | + var item = new TodoItem |
| 91 | + { |
| 92 | + CreatedAt = this.UtcNow(), |
| 93 | + Text = text |
| 94 | + }; |
| 95 | + |
| 96 | + context.Add(item); |
| 97 | + |
| 98 | + await context.SaveChangesAsync(); |
| 99 | + |
| 100 | + return item; |
| 101 | + } |
| 102 | + |
| 103 | + public async Task<bool?> CompleteItemAsync(Guid itemId) |
| 104 | + { |
| 105 | + var item = await this.GetItemAsync(itemId); |
| 106 | + |
| 107 | + if (item is null) |
| 108 | + { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + if (item.CompletedAt.HasValue) |
| 113 | + { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + item.CompletedAt = this.UtcNow(); |
| 118 | + |
| 119 | + context.Items.Update(item); |
| 120 | + |
| 121 | + await context.SaveChangesAsync(); |
| 122 | + |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + public async Task<bool> DeleteItemAsync(Guid itemId) |
| 127 | + { |
| 128 | + var item = await this.GetItemAsync(itemId); |
| 129 | + |
| 130 | + if (item is null) |
| 131 | + { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + context.Items.Remove(item); |
| 136 | + |
| 137 | + await context.SaveChangesAsync(); |
| 138 | + |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + public async Task<TodoItem?> GetItemAsync(Guid itemId) |
| 143 | + { |
| 144 | + await this.EnsureDatabaseAsync(); |
| 145 | + |
| 146 | + return await context.Items.FindAsync([itemId]); |
| 147 | + } |
| 148 | + |
| 149 | + public async Task<IList<TodoItem>> GetItemsAsync() |
| 150 | + { |
| 151 | + await this.EnsureDatabaseAsync(); |
| 152 | + |
| 153 | + return await context.Items |
| 154 | + .OrderBy(x => x.CompletedAt.HasValue) |
| 155 | + .ThenBy(x => x.CreatedAt) |
| 156 | + .ToListAsync(); |
| 157 | + } |
| 158 | + |
| 159 | + private async Task EnsureDatabaseAsync() => await context.Database.EnsureCreatedAsync(); |
| 160 | + |
| 161 | + private DateTime UtcNow() => timeProvider.GetUtcNow().UtcDateTime; |
| 162 | + } |
| 163 | + |
| 164 | + public class CreateTodoItemModel |
| 165 | + { |
| 166 | + public string Text { get; set; } = string.Empty; |
| 167 | + } |
| 168 | + |
| 169 | + public class TodoItem |
| 170 | + { |
| 171 | + public Guid Id { get; set; } |
| 172 | + |
| 173 | + public string Text { get; set; } = default!; |
| 174 | + |
| 175 | + public DateTime CreatedAt { get; set; } |
| 176 | + |
| 177 | + public DateTime? CompletedAt { get; set; } |
| 178 | + } |
| 179 | +} |
0 commit comments