Skip to content

Commit 90c4d6e

Browse files
committed
Refactoring a bit and removing unused annotations
Signed-off-by: JasonWhall <[email protected]>
1 parent d5801fa commit 90c4d6e

File tree

5 files changed

+65
-54
lines changed

5 files changed

+65
-54
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.EntityFrameworkCore;
3+
using static ResultMapper;
4+
5+
namespace Microsoft.AspNetCore.Builder
6+
{
7+
public static class WebApplicationExtensions
8+
{
9+
public static WebApplication UseTodoRoutes(this WebApplication app, string basePath)
10+
{
11+
// Get All Todos
12+
app.MapGet(basePath, async (TodoContext _context) => await _context.TodoItems.ToListAsync());
13+
14+
// Get single Todo
15+
app.MapGet($"{basePath}/{{id}}", async (TodoContext _context, long id) =>
16+
await _context.TodoItems.FindAsync(id) is TodoItem todoItem ? Ok(todoItem) : NotFound());
17+
18+
// Update Todo
19+
app.MapPut($"{basePath}/{{id}}", async (TodoContext _context, long id, TodoItem todoItem) => {
20+
if (id != todoItem.Id)
21+
return BadRequest();
22+
23+
_context.Entry(todoItem).State = EntityState.Modified;
24+
await _context.SaveChangesAsync();
25+
26+
return NoContent();
27+
});
28+
29+
// Create Todo
30+
app.MapPost(basePath, async (TodoContext _context, TodoItem todoItem) => {
31+
await _context.TodoItems.AddAsync(todoItem);
32+
await _context.SaveChangesAsync();
33+
34+
return Created();
35+
});
36+
37+
// Delete Todo
38+
app.MapDelete($"{basePath}/{{id}}", async (TodoContext _context, long id) => {
39+
if (await _context.TodoItems.FindAsync(id) is TodoItem todoItem) {
40+
_context.TodoItems.Remove(todoItem);
41+
await _context.SaveChangesAsync();
42+
return NoContent();
43+
}
44+
45+
return NotFound();
46+
});
47+
48+
return app;
49+
}
50+
}
51+
}

TodoApp.Api/Program.cs

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3-
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.EntityFrameworkCore;
54
using Microsoft.Extensions.DependencyInjection;
65
using Microsoft.Extensions.Hosting;
7-
using System.ComponentModel.DataAnnotations;
8-
using static ResultMapper;
96

107
var builder = WebApplication.CreateBuilder(args);
118
var sqlConnection = builder.Configuration.GetSection("ConnectionStrings")["SqlServer"];
@@ -21,62 +18,14 @@
2118
builder.Services.AddSwaggerGen(opts =>
2219
opts.SwaggerDoc("v1", new() { Title = builder.Environment.ApplicationName, Version = "v1" }));
2320

24-
await using WebApplication app = builder.Build();
21+
await using var app = builder.Build();
2522

2623
if (app.Environment.IsDevelopment()) {
2724
app.UseDeveloperExceptionPage()
2825
.UseSwagger()
2926
.UseSwaggerUI();
3027
}
3128

32-
var basePath = "/api/TodoItems";
33-
34-
// Get All Todos
35-
app.MapGet(basePath, async ([FromServices] TodoContext _context) => await _context.TodoItems.ToListAsync());
36-
37-
// Get single Todo
38-
app.MapGet($"{basePath}/{{id}}", async ([FromServices] TodoContext _context, long id) =>
39-
await _context.TodoItems.FindAsync(id) is TodoItem todoItem ? Ok(todoItem) : NotFound());
40-
41-
// Update Todo
42-
app.MapPut($"{basePath}/{{id}}", async ([FromServices] TodoContext _context, long id, TodoItem todoItem) => {
43-
if (id != todoItem.Id)
44-
return BadRequest();
45-
46-
_context.Entry(todoItem).State = EntityState.Modified;
47-
await _context.SaveChangesAsync();
48-
49-
return NoContent();
50-
});
51-
52-
// Create Todo
53-
app.MapPost(basePath, async ([FromServices] TodoContext _context, TodoItem todoItem) => {
54-
await _context.TodoItems.AddAsync(todoItem);
55-
await _context.SaveChangesAsync();
56-
57-
return Created();
58-
});
59-
60-
// Delete Todo
61-
app.MapDelete($"{basePath}/{{id}}", async ([FromServices] TodoContext _context, long id) => {
62-
if (await _context.TodoItems.FindAsync(id) is TodoItem todoItem) {
63-
_context.TodoItems.Remove(todoItem);
64-
await _context.SaveChangesAsync();
65-
return NoContent();
66-
}
67-
68-
return NotFound();
69-
});
70-
71-
await app.RunAsync();
72-
73-
public record TodoItem(long Id, [Required] string Name, bool IsComplete);
74-
75-
public class TodoContext : DbContext
76-
{
77-
public TodoContext(DbContextOptions<TodoContext> options)
78-
: base(options) { }
79-
80-
public DbSet<TodoItem> TodoItems => Set<TodoItem>();
81-
}
29+
app.UseTodoRoutes(app.Configuration["BasePath"] ?? "/api/TodoItems");
8230

31+
await app.RunAsync();

TodoApp.Api/TodoContext.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
public class TodoContext : DbContext
4+
{
5+
public TodoContext(DbContextOptions<TodoContext> options)
6+
: base(options) { }
7+
8+
public DbSet<TodoItem> TodoItems => Set<TodoItem>();
9+
}

TodoApp.Api/TodoItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public record TodoItem(long Id, string Name, bool IsComplete);

TodoApp.Api/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
}
88
},
99
"AllowedHosts": "*",
10+
"BasePath": "/api/TodoItems",
1011
"ConnectionStrings": {
1112
"SqlServer": ""
1213
}

0 commit comments

Comments
 (0)