Skip to content

Commit fa61220

Browse files
Route Group Fundamental Sample (#27249)
1 parent faaef8d commit fa61220

File tree

10 files changed

+295
-0
lines changed

10 files changed

+295
-0
lines changed

aspnetcore/fundamentals/minimal-apis/7.0-samples/todo-group2/Migrations/20221011191807_Initial.Designer.cs

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace MinApiRouteGroupSample.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class Initial : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.CreateTable(
14+
name: "Todos",
15+
columns: table => new
16+
{
17+
Id = table.Column<int>(type: "INTEGER", nullable: false)
18+
.Annotation("Sqlite:Autoincrement", true),
19+
Title = table.Column<string>(type: "TEXT", nullable: false),
20+
Description = table.Column<string>(type: "TEXT", nullable: false),
21+
IsDone = table.Column<bool>(type: "INTEGER", nullable: false)
22+
},
23+
constraints: table =>
24+
{
25+
table.PrimaryKey("PK_Todos", x => x.Id);
26+
});
27+
}
28+
29+
/// <inheritdoc />
30+
protected override void Down(MigrationBuilder migrationBuilder)
31+
{
32+
migrationBuilder.DropTable(
33+
name: "Todos");
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <auto-generated />
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
5+
using MinApiRouteGroupSample;
6+
7+
#nullable disable
8+
9+
namespace MinApiRouteGroupSample.Migrations
10+
{
11+
[DbContext(typeof(TodoGroupDbContext))]
12+
partial class TodoGroupDbContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder.HasAnnotation("ProductVersion", "7.0.0-rc.1.22426.7");
18+
19+
modelBuilder.Entity("MinApiRouteGroupSample.Todo", b =>
20+
{
21+
b.Property<int>("Id")
22+
.ValueGeneratedOnAdd()
23+
.HasColumnType("INTEGER");
24+
25+
b.Property<string>("Description")
26+
.IsRequired()
27+
.HasColumnType("TEXT");
28+
29+
b.Property<bool>("IsDone")
30+
.HasColumnType("INTEGER");
31+
32+
b.Property<string>("Title")
33+
.IsRequired()
34+
.HasColumnType("TEXT");
35+
36+
b.HasKey("Id");
37+
38+
b.ToTable("Todos");
39+
});
40+
#pragma warning restore 612, 618
41+
}
42+
}
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-rc.1.22426.7" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0-rc.1.22426.7">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
15+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using MinApiRouteGroupSample;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Add services to the container.
7+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
8+
builder.Services.AddEndpointsApiExplorer();
9+
builder.Services.AddSwaggerGen();
10+
builder.Services.AddDbContext<TodoGroupDbContext>(options =>
11+
{
12+
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
13+
options.UseSqlite($"Data Source={Path.Join(path, "WebMinRouteGroup.db")}");
14+
});
15+
16+
var app = builder.Build();
17+
18+
// Configure the HTTP request pipeline.
19+
if (app.Environment.IsDevelopment())
20+
{
21+
app.UseSwagger();
22+
app.UseSwaggerUI();
23+
}
24+
25+
using var scope = app.Services.CreateScope();
26+
var db = scope.ServiceProvider.GetService<TodoGroupDbContext>();
27+
db?.Database.MigrateAsync();
28+
29+
app.MapGroup("/public/todos")
30+
.MapTodosApi()
31+
.WithTags("Public Todo Endpoints");
32+
33+
app.MapGroup("/private/todos")
34+
.MapTodosApi()
35+
.RequireAuthorization();
36+
37+
app.Run();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MinApiRouteGroupSample;
2+
3+
public class Todo
4+
{
5+
public int Id { get; set; }
6+
public string Title { get; set; } = string.Empty;
7+
public string Description { get; set; } = string.Empty;
8+
public bool IsDone { get; set; }
9+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Microsoft.AspNetCore.Http.HttpResults;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace MinApiRouteGroupSample;
5+
6+
public static class TodoEndpoints
7+
{
8+
public static RouteGroupBuilder MapTodosApi(this RouteGroupBuilder group)
9+
{
10+
group.MapGet("/", GetAllTodos);
11+
group.MapGet("/{id}", GetTodo);
12+
group.MapPost("/", CreateTodo);
13+
group.MapPut("/{id}", UpdateTodo);
14+
group.MapDelete("/{id}", DeleteTodo);
15+
16+
return group;
17+
}
18+
19+
// get all todos
20+
public static async Task<Ok<List<Todo>>> GetAllTodos(TodoGroupDbContext database)
21+
{
22+
var todos = await database.Todos.ToListAsync();
23+
return TypedResults.Ok(todos);
24+
}
25+
26+
// get todo by id
27+
public static async Task<Results<Ok<Todo>, NotFound>> GetTodo(int id, TodoGroupDbContext database)
28+
{
29+
var todo = await database.Todos.FindAsync(id);
30+
31+
if (todo is null)
32+
return TypedResults.NotFound();
33+
34+
return TypedResults.Ok(todo);
35+
}
36+
37+
// create todo
38+
public static async Task<Created<Todo>> CreateTodo(Todo todo, TodoGroupDbContext database)
39+
{
40+
await database.Todos.AddAsync(todo);
41+
await database.SaveChangesAsync();
42+
43+
return TypedResults.Created($"{todo.Id}", todo);
44+
}
45+
46+
// update todo
47+
public static async Task<Results<NoContent, NotFound>> UpdateTodo(Todo todo, TodoGroupDbContext database)
48+
{
49+
var existingTodo = await database.Todos.FindAsync(todo.Id);
50+
51+
if (existingTodo is null)
52+
return TypedResults.NotFound();
53+
54+
existingTodo.Title = todo.Title;
55+
existingTodo.Description = todo.Description;
56+
existingTodo.IsDone = todo.IsDone;
57+
58+
await database.SaveChangesAsync();
59+
60+
return TypedResults.NoContent();
61+
}
62+
63+
// delete todo
64+
public static async Task<Results<NoContent, NotFound>> DeleteTodo(int id, TodoGroupDbContext database)
65+
{
66+
var todo = await database.Todos.FindAsync(id);
67+
68+
if (todo is null)
69+
return TypedResults.NotFound();
70+
71+
database.Todos.Remove(todo);
72+
await database.SaveChangesAsync();
73+
74+
return TypedResults.NoContent();
75+
}
76+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace MinApiRouteGroupSample;
4+
5+
public class TodoGroupDbContext : DbContext
6+
{
7+
public DbSet<Todo> Todos => Set<Todo>();
8+
9+
public TodoGroupDbContext(DbContextOptions<TodoGroupDbContext> options)
10+
: base(options)
11+
{
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)