Skip to content

Commit

Permalink
func and named delegates and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jchannon committed Jan 11, 2018
1 parent 7c70fed commit 14e526a
Show file tree
Hide file tree
Showing 41 changed files with 768 additions and 62 deletions.
1 change: 1 addition & 0 deletions BotwinMediator/BotwinMediator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.0" />
<PackageReference Include="Botwin" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Models\Models.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion BotwinMediator/Features/Films/FilmsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public FilmsModule(Handler handler) : base("/api/films")
{
var command = new CreateFilmCommand(result.Data);
this.handler.Handle(command);
res.StatusCode = 204;
res.StatusCode = 201;
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ protected override object Execute(ListFilmsByIdCommand command)

//Use shared query to get film
var film = this.listFilmByIdQuery.Execute(command.Id);

if (film == null)
{
return null;
}

var director = this.getDirectorByIdQuery.Execute(film.DirectorId);
film.Director = director;
Expand Down
11 changes: 4 additions & 7 deletions BotwinMediator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
namespace BotwinMediator
{
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();

host.Run();
.Build()
.Run();
}
}
}
2 changes: 1 addition & 1 deletion BotwinMediator/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void ConfigureServices(IServiceCollection services)
new UpdateFilmCommandHandler(s.GetRequiredService<IListFilmByIdQuery>(),s.GetRequiredService<IValidUserQuery>())
}));

services.AddBotwin(Assembly.GetEntryAssembly(), typeof(FilmValidator).Assembly);
services.AddBotwin(Assembly.GetCallingAssembly(), typeof(FilmValidator).Assembly);
}

public void Configure(IApplicationBuilder app)
Expand Down
18 changes: 18 additions & 0 deletions DDDScot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2. MediatR Project Tests",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediatRWebAPI.Tests", "MediatRWebAPI.Tests\MediatRWebAPI.Tests.csproj", "{E9EA372B-B08D-4C2E-B78A-948810D79F02}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3. Functional Project Tests", "3. Functional Project Tests", "{F03EA84D-15EF-4E1D-8FBE-79FFC0FDC277}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionalProjectTests", "FunctionalProjectTests\FunctionalProjectTests.csproj", "{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -101,6 +105,18 @@ Global
{E9EA372B-B08D-4C2E-B78A-948810D79F02}.Release|x64.Build.0 = Release|Any CPU
{E9EA372B-B08D-4C2E-B78A-948810D79F02}.Release|x86.ActiveCfg = Release|Any CPU
{E9EA372B-B08D-4C2E-B78A-948810D79F02}.Release|x86.Build.0 = Release|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Release|Any CPU.Build.0 = Release|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Debug|x64.ActiveCfg = Debug|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Debug|x64.Build.0 = Debug|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Debug|x86.ActiveCfg = Debug|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Debug|x86.Build.0 = Debug|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Release|x64.ActiveCfg = Release|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Release|x64.Build.0 = Release|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Release|x86.ActiveCfg = Release|Any CPU
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{626AD192-F5D0-4AE1-AEEC-7B8FE40E927F} = {66D3DCED-78BD-4DB8-8C8D-3A5FA09543C6}
Expand All @@ -115,5 +131,7 @@ Global
{2974A21D-76F8-4248-BF01-E7B5BDA6A6FF} = {DD7F5385-6594-4AF5-ADE9-ABC65C2A29BC}
{694570A7-D60F-4058-A86F-B146204891B7} = {44C1DD68-1371-456C-9541-BCC3CEE8A2BE}
{E9EA372B-B08D-4C2E-B78A-948810D79F02} = {694570A7-D60F-4058-A86F-B146204891B7}
{F03EA84D-15EF-4E1D-8FBE-79FFC0FDC277} = {44C1DD68-1371-456C-9541-BCC3CEE8A2BE}
{0A4CAD93-00B1-4ED6-9C92-3DB4C4F8509A} = {F03EA84D-15EF-4E1D-8FBE-79FFC0FDC277}
EndGlobalSection
EndGlobal
20 changes: 0 additions & 20 deletions FunctionalProject/Features/Films/FilmsModule.cs

This file was deleted.

7 changes: 0 additions & 7 deletions FunctionalProject/Features/Films/RouteHandlers.cs

This file was deleted.

20 changes: 20 additions & 0 deletions FunctionalProject/Features/FuncFilms/CreateFilm/CreateFilmRoute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace FunctionalProject.Features.FuncFilms.CreateFilm
{
using System;
using Models;

public static class CreateFilmRoute
{
public static void Handle(Film film, Func<bool> validUserQuery)
{
if (!validUserQuery())
{
throw new InvalidOperationException();
}

//Do some special MEGA CORP business validation

//Save to database by writing SQL here
}
}
}
17 changes: 17 additions & 0 deletions FunctionalProject/Features/FuncFilms/DeleteFilm/DeleteFilmRoute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace FunctionalProject.Features.FuncFilms.DeleteFilm
{
using System;

public static class DeleteFilmRoute
{
public static void Handle(int id, Func<bool> validUserQuery)
{
if (!validUserQuery())
{
throw new InvalidOperationException();
}

//Write some SQL to delete from DB
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FunctionalProject.Features.DelegateFilms
namespace FunctionalProject.Features.FuncFilms
{
using System;
using System.Threading.Tasks;
Expand All @@ -12,11 +12,38 @@

public class FilmsModule : BotwinModule
{
public FilmsModule() : base("/api/films")
public FilmsModule() : base("/api/funcy/films")
{
this.Get("/", this.GetFilms);
this.Get("/{id:int}", this.GetFilmById);
this.Put("/{id:int}", this.UpdateFilm);
this.Post("/", this.CreateFilm);
this.Delete("/{id:int}", this.DeleteFilm);
}

private async Task CreateFilm(HttpContext context)
{
var result = context.Request.BindAndValidate<Film>();

if (!result.ValidationResult.IsValid)
{
context.Response.StatusCode = 422;
await context.Response.Negotiate(result.ValidationResult.GetFormattedErrors());
return;
}

try
{
var handler = RouteHandlers.CreateFilmHandler;

handler(result.Data);

context.Response.StatusCode = 201;
}
catch (Exception)
{
context.Response.StatusCode = 403;
}
}

private async Task UpdateFilm(HttpContext context)
Expand All @@ -35,7 +62,7 @@ private async Task UpdateFilm(HttpContext context)
var handler = RouteHandlers.UpdateFilmHandler;

handler(context.GetRouteData().As<int>("id"), result.Data);

context.Response.StatusCode = 204;
}
catch (Exception)
Expand Down Expand Up @@ -67,5 +94,23 @@ private async Task GetFilmById(HttpContext context)

await context.Response.AsJson(film);
}

private Task DeleteFilm(HttpContext context)
{
try
{
var handler = RouteHandlers.DeleteFilmHandler;

handler(context.GetRouteData().As<int>("id"));

context.Response.StatusCode = 204;
}
catch (Exception)
{
context.Response.StatusCode = 403;
}

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FunctionalProject.Features.DelegateFilms.ListFilmById
namespace FunctionalProject.Features.FuncFilms.ListFilmById
{
using System;
using System.Collections.Generic;
Expand All @@ -9,6 +9,11 @@ public static class ListFilmByIdRoute
public static Film Handle(int id, Func<int,Film> listFilmById, Func<int,Director> getDirectorById, Func<int,IEnumerable<CastMember>> getCastByFilmIdQuery)
{
var film = listFilmById(id);

if (film == null)
{
return null;
}

var director = getDirectorById(film.DirectorId);
film.Director = director;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FunctionalProject.Features.DelegateFilms.ListFilms
namespace FunctionalProject.Features.FuncFilms.ListFilms
{
using System.Collections.Generic;
using Models;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
namespace FunctionalProject.Features.DelegateFilms
namespace FunctionalProject.Features.FuncFilms
{
using System;
using System.Collections.Generic;
using FunctionalProject.Features.DelegateFilms.ListFilmById;
using FunctionalProject.Features.DelegateFilms.ListFilms;
using FunctionalProject.Features.DelegateFilms.UpdateFilm;
using FunctionalProject.Features.FuncFilms.CreateFilm;
using FunctionalProject.Features.FuncFilms.DeleteFilm;
using FunctionalProject.Features.FuncFilms.ListFilmById;
using FunctionalProject.Features.FuncFilms.ListFilms;
using FunctionalProject.Features.FuncFilms.UpdateFilm;
using Models;

public static class RouteHandlers
Expand All @@ -15,6 +17,10 @@ public static class RouteHandlers

private static Action<int, Film> updateFilm;

private static Action<Film> createFilm;

private static Action<int> deleteFilm;

//private static Func<int,Film> getFilmyById = i => new Film { Id = 1, Name = "Pulp Fiction", DirectorId = 1 };

public static Func<IEnumerable<Film>> ListFilmsHandler
Expand Down Expand Up @@ -51,5 +57,17 @@ public static Action<int, Film> UpdateFilmHandler

set => updateFilm = value;
}

public static Action<Film> CreateFilmHandler
{
get => createFilm ?? (film => CreateFilmRoute.Handle(film, () => new Random().Next() % 2 == 0));
set => createFilm = value;
}

public static Action<int> DeleteFilmHandler
{
get => deleteFilm ?? (id => DeleteFilmRoute.Handle(id, () => new Random().Next() % 2 == 0));
set => deleteFilm = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FunctionalProject.Features.DelegateFilms.UpdateFilm
namespace FunctionalProject.Features.FuncFilms.UpdateFilm
{
using System;
using Models;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace FunctionalProject.Features.NamedDelegatesFilms.CastMembers
{
using System.Collections.Generic;
using Models;

public static class GetCastByFilmIdQuery
{
public static IEnumerable<CastMember> Execute(int filmId)
{
//Do some SQL

return new[] { new CastMember { Name = "John Travolta" }, new CastMember { Name = "Samuel L Jackson" } };
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace FunctionalProject.Features.NamedDelegatesFilms.Directors
{
using Models;

public static class GetDirectorByIdQuery
{
public static Director Execute(int id)
{
//Do some SQL

return new Director { Name = "Steven Spielberg" };
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace FunctionalProject.Features.NamedDelegatesFilms.Films.CreateFilm
{
using System;
using Models;

public static class CreateFilmRoute
{
public static void Handle(Film film, ValidUserDelegate validUserQuery)
{
if (!validUserQuery())
{
throw new InvalidOperationException();
}

//Do some special MEGA CORP business validation

//Save to database by writing SQL here
}
}
}
21 changes: 21 additions & 0 deletions FunctionalProject/Features/NamedDelegatesFilms/Films/Delegates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace FunctionalProject.Features.NamedDelegatesFilms.Films
{
using System.Collections.Generic;
using Models;

public delegate Film ListFilmByIdDelegate(int id);

public delegate void CreateFilmDelegate(Film film);

public delegate void DeleteFilmDelegate(int id);

public delegate IEnumerable<Film> ListFilmsDelegate();

public delegate void UpdateFilmDelegate(int id, Film film);

public delegate bool ValidUserDelegate();

public delegate Director GetDirectorByIdDelegate(int id);

public delegate IEnumerable<CastMember> GetCastByFilmIdDelegate(int filmId);
}
Loading

0 comments on commit 14e526a

Please sign in to comment.