forked from davidfowl/Todos
-
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.
- Loading branch information
0 parents
commit 93cad98
Showing
10 changed files
with
303 additions
and
0 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,9 @@ | ||
root = true | ||
|
||
[*.props, *.targets, *.csproj] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.cs] | ||
indent_style = space | ||
indent_size = 4 |
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,68 @@ | ||
############################################################################### | ||
# Set default behavior to automatically normalize line endings. | ||
############################################################################### | ||
* text=auto | ||
|
||
############################################################################### | ||
# Make sh files under the build directory always have LF as line endings | ||
############################################################################### | ||
*.sh eol=lf | ||
|
||
############################################################################### | ||
# Set default behavior for command prompt diff. | ||
# | ||
# This is need for earlier builds of msysgit that does not have it on by | ||
# default for csharp files. | ||
# Note: This is only used by command line | ||
############################################################################### | ||
#*.cs diff=csharp | ||
|
||
############################################################################### | ||
# Set the merge driver for project and solution files | ||
# | ||
# Merging from the command prompt will add diff markers to the files if there | ||
# are conflicts (Merging from VS is not affected by the settings below, in VS | ||
# the diff markers are never inserted). Diff markers may cause the following | ||
# file extensions to fail to load in VS. An alternative would be to treat | ||
# these files as binary and thus will always conflict and require user | ||
# intervention with every merge. To do so, just uncomment the entries below | ||
############################################################################### | ||
#*.sln merge=binary | ||
#*.csproj merge=binary | ||
#*.vbproj merge=binary | ||
#*.vcxproj merge=binary | ||
#*.vcproj merge=binary | ||
#*.dbproj merge=binary | ||
#*.fsproj merge=binary | ||
#*.lsproj merge=binary | ||
#*.wixproj merge=binary | ||
#*.modelproj merge=binary | ||
#*.sqlproj merge=binary | ||
#*.wwaproj merge=binary | ||
|
||
############################################################################### | ||
# behavior for image files | ||
# | ||
# image files are treated as binary by default. | ||
############################################################################### | ||
#*.jpg binary | ||
#*.png binary | ||
#*.gif binary | ||
|
||
############################################################################### | ||
# diff behavior for common document formats | ||
# | ||
# Convert binary document formats to text before diffing them. This feature | ||
# is only available from the command line. Turn it on by uncommenting the | ||
# entries below. | ||
############################################################################### | ||
#*.doc diff=astextplain | ||
#*.DOC diff=astextplain | ||
#*.docx diff=astextplain | ||
#*.DOCX diff=astextplain | ||
#*.dot diff=astextplain | ||
#*.DOT diff=astextplain | ||
#*.pdf diff=astextplain | ||
#*.PDF diff=astextplain | ||
#*.rtf diff=astextplain | ||
#*.RTF diff=astextplain |
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,46 @@ | ||
# Folders | ||
artifacts/ | ||
bin/ | ||
obj/ | ||
.dotnet/ | ||
.nuget/ | ||
.packages/ | ||
.tools/ | ||
.vs/ | ||
.vscode/ | ||
node_modules/ | ||
BenchmarkDotNet.Artifacts/ | ||
.gradle/ | ||
src/SignalR/clients/**/dist/ | ||
modules/ | ||
dist/ | ||
.nuxt/ | ||
|
||
# File extensions | ||
*.aps | ||
*.binlog | ||
*.dll | ||
*.DS_Store | ||
*.exe | ||
*.idb | ||
*.lib | ||
*.log | ||
*.pch | ||
*.pdb | ||
*.pidb | ||
*.psess | ||
*.res | ||
*.snk | ||
*.so | ||
*.suo | ||
*.tlog | ||
*.user | ||
*.userprefs | ||
*.vspx | ||
|
||
# Specific files, typically generated by tools | ||
launchSettings.json | ||
msbuild.ProjectImports.zip | ||
StyleCop.Cache | ||
UpgradeLog.htm | ||
.idea |
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,18 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Todos | ||
{ | ||
class Program | ||
{ | ||
static async Task Main(string[] args) | ||
{ | ||
var app = WebApplication.Create(args); | ||
|
||
var todos = new TodoApi(); | ||
todos.MapRoutes(app); | ||
|
||
await app.RunAsync(); | ||
} | ||
} | ||
} |
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 @@ | ||
namespace Todos | ||
{ | ||
public class Todo | ||
{ | ||
public long Id { get; set; } | ||
public string Name { get; set; } | ||
public bool IsComplete { get; set; } | ||
} | ||
} |
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,86 @@ | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Todos | ||
{ | ||
public class TodoApi | ||
{ | ||
private readonly JsonSerializerOptions _options = new JsonSerializerOptions | ||
{ | ||
PropertyNameCaseInsensitive = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase | ||
}; | ||
|
||
public async Task GetAll(HttpContext context) | ||
{ | ||
using var db = new TodoDbContext(); | ||
var todos = await db.Todos.ToListAsync(); | ||
|
||
context.Response.ContentType = "application/json"; | ||
await JsonSerializer.SerializeAsync(context.Response.Body, todos, _options); | ||
} | ||
|
||
public async Task Get(HttpContext context) | ||
{ | ||
var id = (string)context.Request.RouteValues["id"]; | ||
if (id == null || !long.TryParse(id, out var todoId)) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
return; | ||
} | ||
|
||
using var db = new TodoDbContext(); | ||
var todo = await db.Todos.FindAsync(todoId); | ||
if (todo == null) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
return; | ||
} | ||
|
||
context.Response.ContentType = "application/json"; | ||
await JsonSerializer.SerializeAsync(context.Response.Body, todo, _options); | ||
} | ||
|
||
public async Task Post(HttpContext context) | ||
{ | ||
var todo = await JsonSerializer.DeserializeAsync<Todo>(context.Request.Body, _options); | ||
|
||
using var db = new TodoDbContext(); | ||
db.Todos.Add(todo); | ||
await db.SaveChangesAsync(); | ||
} | ||
|
||
public async Task Delete(HttpContext context) | ||
{ | ||
var id = (string)context.Request.RouteValues["id"]; | ||
if (id == null || !long.TryParse(id, out var todoId)) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
return; | ||
} | ||
|
||
using var db = new TodoDbContext(); | ||
var todo = await db.Todos.FindAsync(todoId); | ||
if (todo == null) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
return; | ||
} | ||
|
||
db.Todos.Remove(todo); | ||
await db.SaveChangesAsync(); | ||
} | ||
|
||
public void MapRoutes(IEndpointRouteBuilder endpoints) | ||
{ | ||
endpoints.MapGet("/api/todos", GetAll); | ||
endpoints.MapGet("/api/todos/{id}", Get); | ||
endpoints.MapPost("/api/todos", Post); | ||
endpoints.MapPost("/api/todos/{id}", Delete); | ||
} | ||
} | ||
} |
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,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Todos | ||
{ | ||
public class TodoDbContext : DbContext | ||
{ | ||
public DbSet<Todo> Todos { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseInMemoryDatabase("Todos"); | ||
} | ||
} | ||
} |
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,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="FeatherHttp" Version="0.1.52-alpha.g5f0ffe43df" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.1" /> | ||
</ItemGroup> | ||
</Project> |
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,34 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26124.0 | ||
MinimumVisualStudioVersion = 15.0.26124.0 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Todos", "TodoBasic\Todos.csproj", "{3F583E94-3C46-4727-A4DE-A4909D4BA094}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Debug|x64 = Debug|x64 | ||
Debug|x86 = Debug|x86 | ||
Release|Any CPU = Release|Any CPU | ||
Release|x64 = Release|x64 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Debug|x64.ActiveCfg = Debug|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Debug|x64.Build.0 = Debug|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Debug|x86.ActiveCfg = Debug|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Debug|x86.Build.0 = Debug|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Release|x64.ActiveCfg = Release|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Release|x64.Build.0 = Release|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Release|x86.ActiveCfg = Release|Any CPU | ||
{3F583E94-3C46-4727-A4DE-A4909D4BA094}.Release|x86.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
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,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
<add key="featherhttp" value="https://f.feedz.io/davidfowl/featherhttp/nuget/index.json" /> | ||
<add key="NuGet.org" value="https://api.nuget.org/v3/index.json" /> | ||
</packageSources> | ||
</configuration> |