Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created February 25, 2021 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DamianEdwards/65b97b3c05e06ffe02e0cdc04d08c445 to your computer and use it in GitHub Desktop.
Save DamianEdwards/65b97b3c05e06ffe02e0cdc04d08c445 to your computer and use it in GitHub Desktop.
Simplified ASP.NET Core app exploration
#!/usr/bin/env dotnet run
var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
var connString = config["connectionString"] ?? "Data Source=todos.db";
builder.AddDbContext<TodoDb>(options => options.UseSqlite(connString));
builder.AddSqlite<Todo>(connString) // Higher level API perhaps?
var app = builder.Build();
// Step 0: Request delegate
app.MapGet("/", ctx => ctx.Respsone.WriteAsync("Hello, World!"));
// Step 1: Return string
app.MapGet("/hello", () => "Hello, World!");
// Step 2: Return custom type
app.MapGet("/todo", () => new Todo("Do the thing"));
// Step 3: No DI
app.MapGet("/todo-db/{id}", async (int id) =>
{
using var db = new TodoDb(connString);
return await db.Todos.FindAsync(id) is Todo todo
? Ok(todo) : NotFound();
});
// Step 4: Use a DB
app.MapGet("/todo-db/{id}", async (int id, TodoDb db) =>
{
return await db.Todos.FindAsync(id) is Todo todo
? Ok(todo) : NotFound();
});
app.MapPost("/todo-db", async (Todo todo, TodoDb db) =>
{
db.Todos.Add(todo);
await Todos.SaveChangesAsync();
return Created(); // 204
// return CreatedAt($"/todo-db/{todo.Id}", todo);
};
app.MapPut("/todo-db", async (int id, Todo inputTodo, TodoDb db) =>
{
var todo = await db.Todos.FindAsync(id);
if (todo is null) return NotFound();
todo.Title = inputTodo.Title;
todo.IsComplete = inputTodo.IsComplete;
await db.SaveChanges();
return NoContent();
});
app.MapDelete("/todo-db", async (int id, TodoDb db) =>
{
if (await db.Todos.FindAsync(id) is Todo todo)
{
db.Todos.Remove(todo);
await db.SaveChanges();
return OK(todo);
}
return NotFound();
});
// Step 5: Input validation
app.MapPost("/todo-db", async (Validated<Todo> todo, TodoDb db) =>
{
if (!todo.IsValid) return Problem(todo);
db.Todos.Add(todo);
await Todos.SaveChangesAsync();
return CreatedAt($"/todo-db/{todo.InputObject.Id}", todo);
};
// Run app
await app.RunAsync();
// Data types
record Todo(string Title)
{
public bool IsComplete { get; set; }
}
class TodoDb : DbContext
{
private readonly string _cs;
public TodoDb(string connectionString) : base() => _cs = connectionString; // Non-DI use scenario
public TodoDb(DbContextOptions<TodoDb> options)
: base(options)
{
}
protected override OnConfiguring(DbContextOptionsBuilder optionsBuilder) // Non-DI use scenario
{
optionsBuilder.UseSqlite(_cs);
}
public DbSet<Todo> Todos { get; set; }
}
@alexsandro-xpt
Copy link

No .csproj required? It's look great.

@DamianEdwards
Copy link
Author

That's a scenario we're exploring but not actually confirmed for .NET 6 yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment