From d7c4f240e667361ce5561a42f94b9b3d18d17dfa Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Thu, 4 Aug 2022 21:47:27 +0100 Subject: [PATCH] Minimal api todo group (#26516) * created project * added required test nugets * added custom controller * created test file * added comments * add new line Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * remove a line Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * remove line Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * removed comment Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * created new project * models added * dbcontext added * modified model * repository added * actions added * action comments added * repository comments added * formatted code * edit * formatted code * add changes Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * added one line ending * grouped apis * rewind * rewind * rewind * rewind * removed public * reset * added space Co-authored-by: Stephen Halter * removed extra line Co-authored-by: Stephen Halter * added public access modifier Co-authored-by: Stephen Halter * added sqlite package * added in memory sqlite connection * added meta data * git format * added filter * added applicationdbcontext to ioc * prior to deleting repository * repository deleted * git format * git format * format * format * validationresponse added Co-authored-by: Stephen Halter * adding services together Co-authored-by: Stephen Halter * refactored * deleted dto file * created folder for routehandlers * remove comment Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * remove comment Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * remove comment Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * remove comment Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> * added cs extension to routehandlers * dotnet format * changed error message * add a line Co-authored-by: Stephen Halter * remove space Co-authored-by: Stephen Halter * made changes Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Co-authored-by: Stephen Halter --- .../7.0-samples/todo-group/Data/Todo.cs | 8 +++ .../7.0-samples/todo-group/Data/TodoDto.cs | 8 +++ .../todo-group/Data/TodoGroupDbContext.cs | 14 ++++ .../7.0-samples/todo-group/Program.cs | 55 ++++++++++++++++ .../7.0-samples/todo-group/RouteHandlers.cs | 66 +++++++++++++++++++ .../todo-group/appsettings.Development.json | 8 +++ .../7.0-samples/todo-group/appsettings.json | 9 +++ .../7.0-samples/todo-group/todo-group.csproj | 16 +++++ 8 files changed, 184 insertions(+) create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/Todo.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoDto.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Data/TodoGroupDbContext.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/Program.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/RouteHandlers.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.Development.json create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/appsettings.json create mode 100644 aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group/todo-group.csproj 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 + + + + + + + + +