diff --git a/TodoApp.Api/Extensions/WebApplicationExtensions.cs b/TodoApp.Api/Extensions/WebApplicationExtensions.cs new file mode 100644 index 0000000..bd5310c --- /dev/null +++ b/TodoApp.Api/Extensions/WebApplicationExtensions.cs @@ -0,0 +1,51 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using static ResultMapper; + +namespace Microsoft.AspNetCore.Builder +{ + public static class WebApplicationExtensions + { + public static WebApplication UseTodoRoutes(this WebApplication app, string basePath) + { + // Get All Todos + app.MapGet(basePath, async (TodoContext _context) => await _context.TodoItems.ToListAsync()); + + // Get single Todo + app.MapGet($"{basePath}/{{id}}", async (TodoContext _context, long id) => + await _context.TodoItems.FindAsync(id) is TodoItem todoItem ? Ok(todoItem) : NotFound()); + + // Update Todo + app.MapPut($"{basePath}/{{id}}", async (TodoContext _context, long id, TodoItem todoItem) => { + if (id != todoItem.Id) + return BadRequest(); + + _context.Entry(todoItem).State = EntityState.Modified; + await _context.SaveChangesAsync(); + + return NoContent(); + }); + + // Create Todo + app.MapPost(basePath, async (TodoContext _context, TodoItem todoItem) => { + await _context.TodoItems.AddAsync(todoItem); + await _context.SaveChangesAsync(); + + return Created(); + }); + + // Delete Todo + app.MapDelete($"{basePath}/{{id}}", async (TodoContext _context, long id) => { + if (await _context.TodoItems.FindAsync(id) is TodoItem todoItem) { + _context.TodoItems.Remove(todoItem); + await _context.SaveChangesAsync(); + return NoContent(); + } + + return NotFound(); + }); + + return app; + } + } +} diff --git a/TodoApp.Api/Program.cs b/TodoApp.Api/Program.cs index 62dda5d..e8d05f7 100644 --- a/TodoApp.Api/Program.cs +++ b/TodoApp.Api/Program.cs @@ -1,11 +1,8 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using System.ComponentModel.DataAnnotations; -using static ResultMapper; var builder = WebApplication.CreateBuilder(args); var sqlConnection = builder.Configuration.GetSection("ConnectionStrings")["SqlServer"]; @@ -21,7 +18,7 @@ builder.Services.AddSwaggerGen(opts => opts.SwaggerDoc("v1", new() { Title = builder.Environment.ApplicationName, Version = "v1" })); -await using WebApplication app = builder.Build(); +await using var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage() @@ -29,54 +26,6 @@ .UseSwaggerUI(); } -var basePath = "/api/TodoItems"; - -// Get All Todos -app.MapGet(basePath, async ([FromServices] TodoContext _context) => await _context.TodoItems.ToListAsync()); - -// Get single Todo -app.MapGet($"{basePath}/{{id}}", async ([FromServices] TodoContext _context, long id) => - await _context.TodoItems.FindAsync(id) is TodoItem todoItem ? Ok(todoItem) : NotFound()); - -// Update Todo -app.MapPut($"{basePath}/{{id}}", async ([FromServices] TodoContext _context, long id, TodoItem todoItem) => { - if (id != todoItem.Id) - return BadRequest(); - - _context.Entry(todoItem).State = EntityState.Modified; - await _context.SaveChangesAsync(); - - return NoContent(); -}); - -// Create Todo -app.MapPost(basePath, async ([FromServices] TodoContext _context, TodoItem todoItem) => { - await _context.TodoItems.AddAsync(todoItem); - await _context.SaveChangesAsync(); - - return Created(); -}); - -// Delete Todo -app.MapDelete($"{basePath}/{{id}}", async ([FromServices] TodoContext _context, long id) => { - if (await _context.TodoItems.FindAsync(id) is TodoItem todoItem) { - _context.TodoItems.Remove(todoItem); - await _context.SaveChangesAsync(); - return NoContent(); - } - - return NotFound(); -}); - -await app.RunAsync(); - -public record TodoItem(long Id, [Required] string Name, bool IsComplete); - -public class TodoContext : DbContext -{ - public TodoContext(DbContextOptions options) - : base(options) { } - - public DbSet TodoItems => Set(); -} +app.UseTodoRoutes(app.Configuration["BasePath"] ?? "/api/TodoItems"); +await app.RunAsync(); \ No newline at end of file diff --git a/TodoApp.Api/TodoContext.cs b/TodoApp.Api/TodoContext.cs new file mode 100644 index 0000000..eb687ec --- /dev/null +++ b/TodoApp.Api/TodoContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; + +public class TodoContext : DbContext +{ + public TodoContext(DbContextOptions options) + : base(options) { } + + public DbSet TodoItems => Set(); +} diff --git a/TodoApp.Api/TodoItem.cs b/TodoApp.Api/TodoItem.cs new file mode 100644 index 0000000..f3ccfad --- /dev/null +++ b/TodoApp.Api/TodoItem.cs @@ -0,0 +1 @@ +public record TodoItem(long Id, string Name, bool IsComplete); \ No newline at end of file diff --git a/TodoApp.Api/appsettings.json b/TodoApp.Api/appsettings.json index 0399f08..5b9c1c1 100644 --- a/TodoApp.Api/appsettings.json +++ b/TodoApp.Api/appsettings.json @@ -7,6 +7,7 @@ } }, "AllowedHosts": "*", + "BasePath": "/api/TodoItems", "ConnectionStrings": { "SqlServer": "" }