Skip to content

Commit

Permalink
Initial commit and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl committed Feb 8, 2020
0 parents commit 93cad98
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
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
68 changes: 68 additions & 0 deletions .gitattributes
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
46 changes: 46 additions & 0 deletions .gitignore
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
18 changes: 18 additions & 0 deletions TodoBasic/Program.cs
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();
}
}
}
9 changes: 9 additions & 0 deletions TodoBasic/Todo.cs
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; }
}
}
86 changes: 86 additions & 0 deletions TodoBasic/TodoApi.cs
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);
}
}
}
14 changes: 14 additions & 0 deletions TodoBasic/TodoDbContext.cs
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");
}
}
}
11 changes: 11 additions & 0 deletions TodoBasic/Todos.csproj
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>
34 changes: 34 additions & 0 deletions Todos.sln
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
8 changes: 8 additions & 0 deletions nuget.config
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>

0 comments on commit 93cad98

Please sign in to comment.