-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring a bit and removing unused annotations
Signed-off-by: JasonWhall <[email protected]>
- Loading branch information
1 parent
d5801fa
commit 90c4d6e
Showing
5 changed files
with
65 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
public record TodoItem(long Id, string Name, bool IsComplete); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
} | ||
}, | ||
"AllowedHosts": "*", | ||
"BasePath": "/api/TodoItems", | ||
"ConnectionStrings": { | ||
"SqlServer": "" | ||
} | ||
|