Minimal api todo group (#26516)

* created project

* added required test nugets

* added custom controller

* created test file

* added comments

* add new line

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* remove a line

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* remove line

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* removed comment

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* created new project

* models added

* dbcontext added

* modified model

* repository added

* actions added

* action comments added

* repository comments added

* formatted code

* edit

* formatted code

* add changes

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* added one line ending

* grouped apis

* rewind

* rewind

* rewind

* rewind

* removed public

* reset

* added space

Co-authored-by: Stephen Halter <halter73@gmail.com>

* removed extra line

Co-authored-by: Stephen Halter <halter73@gmail.com>

* added public access modifier

Co-authored-by: Stephen Halter <halter73@gmail.com>

* added sqlite package

* added in memory sqlite connection

* added meta data

* git format

* added filter

* added applicationdbcontext to ioc

* prior to deleting repository

* repository deleted

* git format

* git format

* format

* format

* validationresponse added

Co-authored-by: Stephen Halter <halter73@gmail.com>

* adding services together

Co-authored-by: Stephen Halter <halter73@gmail.com>

* refactored

* deleted dto file

* created folder for routehandlers

* remove comment

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* remove comment

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* remove comment

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* remove comment

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>

* added cs extension to routehandlers

* dotnet format

* changed error message

* add a line

Co-authored-by: Stephen Halter <halter73@gmail.com>

* remove space

Co-authored-by: Stephen Halter <halter73@gmail.com>

* made changes

Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com>
Co-authored-by: Stephen Halter <halter73@gmail.com>
pull/26633/head
sammychinedu2ky 2022-08-04 21:47:27 +01:00 committed by GitHub
parent 67174015a4
commit d7c4f240e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,8 @@
namespace Data;
public class Todo
{
public int Id { get; set; }
public string Title { get; set; } = String.Empty;
public string Description { get; set; } = String.Empty;
public bool IsDone { get; set; }
}

View File

@ -0,0 +1,8 @@
namespace Data;
public class TodoDto
{
public string Title { get; set; } = String.Empty;
public string Description { get; set; } = String.Empty;
public bool IsDone { get; set; }
}

View File

@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace Data;
public class TodoGroupDbContext : DbContext
{
public TodoGroupDbContext(DbContextOptions<TodoGroupDbContext> options)
: base(options)
{
Database.EnsureCreated();
}
public DbSet<Todo> Todos { get; set; } = default!;
}

View File

@ -0,0 +1,55 @@
using Data;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<TodoGroupDbContext>(options =>
{
options.UseSqlite(connection);
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
// localhost:{port}/swagger
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/", () => "Hello World!");
// todo endpoints
var todos = app.MapGroup("/todos").WithTags("Todo Endpoints").AddRouteHandlerFilter(async (context, next) =>
{
app.Logger.LogInformation("Accessing todo endpoints");
return await next(context);
});
todos.MapGet("/", RouteHandlers.GetAllTodos);
todos.MapGet("/{id}", RouteHandlers.GetTodo);
todos.MapPost("/", RouteHandlers.CreateTodo).AddRouteHandlerFilter(async (context, next) =>
{
// log time taken to process
var start = DateTime.Now;
var result = await next(context);
var end = DateTime.Now;
app.Logger.LogInformation($"{context.HttpContext.Request.Path.Value} took {(end - start).TotalMilliseconds}ms");
return result;
});
todos.MapPut("/{id}", RouteHandlers.UpdateTodo).AddRouteHandlerFilter(async (context, next) =>
{
// log time taken to process
var start = DateTime.Now;
var result = await next(context);
var end = DateTime.Now;
app.Logger.LogInformation($"{context.HttpContext.Request.Path.Value} took {(end - start).TotalMilliseconds}ms");
return result;
});
todos.MapDelete("/{id}", RouteHandlers.DeleteTodo);
app.Run();

View File

@ -0,0 +1,66 @@
using Data;
using Microsoft.EntityFrameworkCore;
public class RouteHandlers
{
// get all todos
public static async Task<IResult> GetAllTodos(TodoGroupDbContext database)
{
var todos = await database.Todos.ToListAsync();
return TypedResults.Ok(todos);
}
// get todo by id
public static async Task<IResult> GetTodo(int id, TodoGroupDbContext database)
{
var todo = await database.Todos.FindAsync(id);
if (todo != null)
{
return TypedResults.Ok(todo);
}
return TypedResults.NotFound();
}
// create todo
public static async Task<IResult> CreateTodo(TodoDto todo, TodoGroupDbContext database)
{
var newTodo = new Todo
{
Title = todo.Title,
Description = todo.Description,
IsDone = todo.IsDone
};
await database.Todos.AddAsync(newTodo);
await database.SaveChangesAsync();
return TypedResults.Created($"/public/todos/{newTodo.Id}", newTodo);
}
// update todo
public static async Task<IResult> UpdateTodo(Todo todo, TodoGroupDbContext database)
{
var existingTodo = await database.Todos.FindAsync(todo.Id);
if (existingTodo != null)
{
existingTodo.Title = todo.Title;
existingTodo.Description = todo.Description;
existingTodo.IsDone = todo.IsDone;
await database.SaveChangesAsync();
return TypedResults.Created($"/public/todos/{existingTodo.Id}", existingTodo);
}
return TypedResults.NotFound();
}
// delete todo
public static async Task<IResult> DeleteTodo(int id, TodoGroupDbContext database)
{
var todo = await database.Todos.FindAsync(id);
if (todo != null)
{
database.Todos.Remove(todo);
await database.SaveChangesAsync();
return TypedResults.NoContent();
}
return TypedResults.NotFound();
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>todo_group</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.5.22303.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>