mirror of
https://git.tonybark.com/tonytins/bark.api.git
synced 2026-02-10 08:14:47 -05:00
Initial source commit 🎉
Set up a new ASP.NET Core Web API project targeting .NET 10.0. Added basic Todo model, in-memory database context, and CRUD endpoints for Todo items based on tutorial. Included configuration files, launch settings, and .gitignore for Visual Studio and JetBrains IDEs.
This commit is contained in:
commit
3c43ce2f71
11 changed files with 646 additions and 0 deletions
52
Program.cs
Normal file
52
Program.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoDb"));
|
||||
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("/", () => "Bark API");
|
||||
|
||||
app.MapGet("/todo", async (TodoDb db)
|
||||
=> await db.Todos.ToListAsync());
|
||||
|
||||
app.MapGet("/todo", async (TodoDb db)
|
||||
=> await db.Todos.Where(t => t.IsCompleted).ToListAsync());
|
||||
|
||||
app.MapGet("/todo/{id}", async (int id, TodoDb db) =>
|
||||
await db.Todos.FindAsync(id) is Todo todo ? Results.Ok() : Results.NotFound());
|
||||
|
||||
app.MapGet("/todo", async (Todo todo, TodoDb db)
|
||||
=>
|
||||
{
|
||||
db.Todos.Add(todo);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return Results.Created($"/todo/{todo.Id}", todo);
|
||||
});
|
||||
|
||||
app.MapGet("/todo/{id}", async (int id, Todo input, TodoDb db) =>
|
||||
{
|
||||
var todo = await db.Todos.FindAsync(id);
|
||||
|
||||
if (todo is null) return Results.NotFound();
|
||||
|
||||
todo.Name = input.Name;
|
||||
todo.IsCompleted = input.IsCompleted;
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return Results.NoContent();
|
||||
});
|
||||
|
||||
app.MapGet("/todo/{id}", async (int id, TodoDb db) =>
|
||||
{
|
||||
if (await db.Todos.FindAsync(id) is Todo todo)
|
||||
{
|
||||
db.Todos.Remove(todo);
|
||||
await db.SaveChangesAsync();
|
||||
return Results.NoContent();
|
||||
}
|
||||
|
||||
return Results.NotFound();
|
||||
});
|
||||
|
||||
app.Run();
|
||||
Loading…
Add table
Add a link
Reference in a new issue