diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/Todo.cs b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/Todo.cs new file mode 100644 index 0000000000..1d231d41b8 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/Todo.cs @@ -0,0 +1,8 @@ +namespace Data; +public class Todo +{ + public int Id { get; set; } + public string Title { get; set; } = String.Empty; + public string Description { get; set; } = String.Empty; + public bool IsDone { get; set; } +} diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoDto.cs b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoDto.cs new file mode 100644 index 0000000000..a75996caaa --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoDto.cs @@ -0,0 +1,8 @@ +namespace Data; + +public class TodoDto +{ + public string Title { get; set; } = String.Empty; + public string Description { get; set; } = String.Empty; + public bool IsDone { get; set; } +} diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoGroupDbContext.cs b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoGroupDbContext.cs new file mode 100644 index 0000000000..d17e99d46b --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoGroupDbContext.cs @@ -0,0 +1,14 @@ +using Microsoft.EntityFrameworkCore; + +namespace Data; + +public class TodoGroupDbContext : DbContext +{ + public TodoGroupDbContext(DbContextOptions options) + : base(options) + { + Database.EnsureCreated(); + } + + public DbSet Todos { get; set; } = default!; +} diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Program.cs b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Program.cs new file mode 100644 index 0000000000..adc1fbfb8d --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Program.cs @@ -0,0 +1,55 @@ +using Data; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; + +var builder = WebApplication.CreateBuilder(args); +var connection = new SqliteConnection("DataSource=:memory:"); +connection.Open(); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddDbContext(options => +{ + options.UseSqlite(connection); +}); + +var app = builder.Build(); + +if (app.Environment.IsDevelopment()) +{ + // localhost:{port}/swagger + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapGet("/", () => "Hello World!"); + +// todo endpoints +var todos = app.MapGroup("/todos").WithTags("Todo Endpoints").AddRouteHandlerFilter(async (context, next) => +{ + app.Logger.LogInformation("Accessing todo endpoints"); + return await next(context); +}); +todos.MapGet("/", RouteHandlers.GetAllTodos); +todos.MapGet("/{id}", RouteHandlers.GetTodo); +todos.MapPost("/", RouteHandlers.CreateTodo).AddRouteHandlerFilter(async (context, next) => +{ + // log time taken to process + var start = DateTime.Now; + var result = await next(context); + var end = DateTime.Now; + app.Logger.LogInformation($"{context.HttpContext.Request.Path.Value} took {(end - start).TotalMilliseconds}ms"); + return result; +}); +todos.MapPut("/{id}", RouteHandlers.UpdateTodo).AddRouteHandlerFilter(async (context, next) => +{ + // log time taken to process + var start = DateTime.Now; + var result = await next(context); + var end = DateTime.Now; + app.Logger.LogInformation($"{context.HttpContext.Request.Path.Value} took {(end - start).TotalMilliseconds}ms"); + return result; +}); +todos.MapDelete("/{id}", RouteHandlers.DeleteTodo); + +app.Run(); diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/RouteHandlers.cs b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/RouteHandlers.cs new file mode 100644 index 0000000000..cb2334ccfd --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/RouteHandlers.cs @@ -0,0 +1,66 @@ +using Data; +using Microsoft.EntityFrameworkCore; + +public class RouteHandlers +{ + // get all todos + public static async Task GetAllTodos(TodoGroupDbContext database) + { + var todos = await database.Todos.ToListAsync(); + return TypedResults.Ok(todos); + } + + // get todo by id + public static async Task GetTodo(int id, TodoGroupDbContext database) + { + var todo = await database.Todos.FindAsync(id); + if (todo != null) + { + return TypedResults.Ok(todo); + } + return TypedResults.NotFound(); + } + + // create todo + public static async Task CreateTodo(TodoDto todo, TodoGroupDbContext database) + { + var newTodo = new Todo + { + Title = todo.Title, + Description = todo.Description, + IsDone = todo.IsDone + }; + await database.Todos.AddAsync(newTodo); + await database.SaveChangesAsync(); + return TypedResults.Created($"/public/todos/{newTodo.Id}", newTodo); + } + + // update todo + public static async Task UpdateTodo(Todo todo, TodoGroupDbContext database) + { + var existingTodo = await database.Todos.FindAsync(todo.Id); + if (existingTodo != null) + { + existingTodo.Title = todo.Title; + existingTodo.Description = todo.Description; + existingTodo.IsDone = todo.IsDone; + await database.SaveChangesAsync(); + return TypedResults.Created($"/public/todos/{existingTodo.Id}", existingTodo); + } + + return TypedResults.NotFound(); + } + + // delete todo + public static async Task DeleteTodo(int id, TodoGroupDbContext database) + { + var todo = await database.Todos.FindAsync(id); + if (todo != null) + { + database.Todos.Remove(todo); + await database.SaveChangesAsync(); + return TypedResults.NoContent(); + } + return TypedResults.NotFound(); + } +} diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.Development.json b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.Development.json new file mode 100644 index 0000000000..0c208ae918 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.json b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.json new file mode 100644 index 0000000000..10f68b8c8b --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/todo-group.csproj b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/todo-group.csproj new file mode 100644 index 0000000000..80f875509f --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/todo-group.csproj @@ -0,0 +1,16 @@ + + + + net7.0 + enable + enable + todo_group + + + + + + + + +