Skip to content

Commit

Permalink
Refactoring a bit and removing unused annotations
Browse files Browse the repository at this point in the history
Signed-off-by: JasonWhall <[email protected]>
  • Loading branch information
JasonWhall committed Jul 29, 2021
1 parent d5801fa commit 90c4d6e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 54 deletions.
51 changes: 51 additions & 0 deletions TodoApp.Api/Extensions/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
57 changes: 3 additions & 54 deletions TodoApp.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -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"];
Expand All @@ -21,62 +18,14 @@
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()
.UseSwagger()
.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<TodoContext> options)
: base(options) { }

public DbSet<TodoItem> TodoItems => Set<TodoItem>();
}
app.UseTodoRoutes(app.Configuration["BasePath"] ?? "/api/TodoItems");

await app.RunAsync();
9 changes: 9 additions & 0 deletions TodoApp.Api/TodoContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.EntityFrameworkCore;

public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options)
: base(options) { }

public DbSet<TodoItem> TodoItems => Set<TodoItem>();
}
1 change: 1 addition & 0 deletions TodoApp.Api/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public record TodoItem(long Id, string Name, bool IsComplete);
1 change: 1 addition & 0 deletions TodoApp.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
}
},
"AllowedHosts": "*",
"BasePath": "/api/TodoItems",
"ConnectionStrings": {
"SqlServer": ""
}
Expand Down

0 comments on commit 90c4d6e

Please sign in to comment.