Skip to content

Commit

Permalink
minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jchannon committed Jun 14, 2018
1 parent 88368e6 commit 59e3f4e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,33 @@ public FilmsModule() : base("/api/delegate/films") // --> REPLACE ATTRIBUTES WIT
this.Post("/", this.CreateFilm);
this.Delete("/{id:int}", this.DeleteFilm);
}

private async Task GetFilms(HttpContext context)
{
var handler = RouteHandlers.ListFilmsHandler;

private async Task CreateFilm(HttpContext context)
var films = handler();

await context.Response.AsJson(films);
}

private async Task GetFilmById(HttpContext context)
{
var handler = RouteHandlers.ListFilmByIdHandler;

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

if (film == null)
{
context.Response.StatusCode = 404;
return;
}

await context.Response.AsJson(film);
}

private async Task UpdateFilm(HttpContext context)
{
/*** REPLACE ATTRIBUTES WITH METHOD CALLS ***/
var result = context.Request.BindAndValidate<Film>();

if (!result.ValidationResult.IsValid)
Expand All @@ -42,20 +65,21 @@ private async Task CreateFilm(HttpContext context)

try
{
var handler = RouteHandlers.CreateFilmHandler;
var handler = RouteHandlers.UpdateFilmHandler;

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

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

private async Task UpdateFilm(HttpContext context)
private async Task CreateFilm(HttpContext context)
{
/*** REPLACE ATTRIBUTES WITH METHOD CALLS ***/
var result = context.Request.BindAndValidate<Film>();

if (!result.ValidationResult.IsValid)
Expand All @@ -67,42 +91,18 @@ private async Task UpdateFilm(HttpContext context)

try
{
var handler = RouteHandlers.UpdateFilmHandler;
var handler = RouteHandlers.CreateFilmHandler;

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

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

private async Task GetFilms(HttpContext context)
{
var handler = RouteHandlers.ListFilmsHandler;

var films = handler();

await context.Response.AsJson(films);
}

private async Task GetFilmById(HttpContext context)
{
var handler = RouteHandlers.ListFilmByIdHandler;

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

if (film == null)
{
context.Response.StatusCode = 404;
return;
}

await context.Response.AsJson(film);
}

private Task DeleteFilm(HttpContext context)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ public static class RouteHandlers

static RouteHandlers()
{
CreateFilmHandler = film => CreateFilmRoute.Handle(film, () => ValidUserQuery.Execute());

DeleteFilmHandler = id => DeleteFilmRoute.Handle(id, () => ValidUserQuery.Execute());
ListFilmsHandler = () => ListFilmsRoute.Handle();

ListFilmByIdHandler = id => ListFilmByIdRoute.Handle(
id,
filmId => ListFilmsByIdQuery.ListFilmsByIdQuery.Execute(id),
dirId => GetDirectorByIdQuery.Execute(dirId),
filmId => GetCastByFilmIdQuery.Execute(id)
);

ListFilmsHandler = () => ListFilmsRoute.Handle();


UpdateFilmHandler = (id, film) => UpdateFilmRoute.Handle(
id,
film,
() => ValidUserQuery.Execute(),
filmId => ListFilmsByIdQuery.ListFilmsByIdQuery.Execute(filmId));

CreateFilmHandler = film => CreateFilmRoute.Handle(film, () => ValidUserQuery.Execute());

DeleteFilmHandler = id => DeleteFilmRoute.Handle(id, () => ValidUserQuery.Execute());
}
}
}
18 changes: 8 additions & 10 deletions FunctionalProjectTests/Features/Films/FilmTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@

public class FilmTests
{
private TestServer server;

private HttpClient client;
private readonly HttpClient client;

public FilmTests()
{
server = new TestServer(WebHost.CreateDefaultBuilder()
var server = new TestServer(WebHost.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddMvc().AddFluentValidation();
Expand All @@ -35,7 +33,7 @@ public FilmTests()
.Configure(app => app.UseMvc())
);

client = server.CreateClient();
this.client = server.CreateClient();
}

[Fact]
Expand All @@ -50,7 +48,7 @@ public async Task Should_return_400_on_invalid_data_when_creating_film()
var newFilm = new Film { Name = "" };

//When
var response = await client.PostAsync("/api/delegate/films", new StringContent(JsonConvert.SerializeObject(newFilm), Encoding.UTF8, "application/json"));
var response = await this.client.PostAsync("/api/delegate/films", new StringContent(JsonConvert.SerializeObject(newFilm), Encoding.UTF8, "application/json"));

//Then
Assert.Equal(400, (int)response.StatusCode);
Expand All @@ -65,7 +63,7 @@ public async Task Should_return_403_on_invalid_user_when_creating_film()
var newFilm = new Film { Name = "Shrek" };

//When
var response = await client.PostAsync("/api/delegate/films", new StringContent(JsonConvert.SerializeObject(newFilm), Encoding.UTF8, "application/json"));
var response = await this.client.PostAsync("/api/delegate/films", new StringContent(JsonConvert.SerializeObject(newFilm), Encoding.UTF8, "application/json"));

//Then
Assert.Equal(403, (int)response.StatusCode);
Expand All @@ -80,7 +78,7 @@ public async Task Should_return_201_when_creating_film()
var newFilm = new Film { Name = "Shrek" };

//When
var response = await client.PostAsync("/api/delegate/films", new StringContent(JsonConvert.SerializeObject(newFilm), Encoding.UTF8, "application/json"));
var response = await this.client.PostAsync("/api/delegate/films", new StringContent(JsonConvert.SerializeObject(newFilm), Encoding.UTF8, "application/json"));

//Then
Assert.Equal(201, (int)response.StatusCode);
Expand All @@ -96,7 +94,7 @@ public async Task Should_get_film_by_id()
RouteHandlers.ListFilmByIdHandler = id => ListFilmByIdRoute.Handle(id, filmid => new Film { Name = "Blade Runner" }, i => new Director(), filmId => new[] { new CastMember() });

//When
var response = await client.GetAsync("/api/delegate/films/1");
var response = await this.client.GetAsync("/api/delegate/films/1");
var contents = await response.Content.ReadAsStringAsync();

//Then
Expand All @@ -113,7 +111,7 @@ public async Task Should_return_404_when_no_film_found_via_get_film_by_id()
RouteHandlers.ListFilmByIdHandler = id => ListFilmByIdRoute.Handle(id, filmid => null, i => new Director(), filmId => new[] { new CastMember() });

//When
var response = await client.GetAsync("/api/delegate/films/1");
var response = await this.client.GetAsync("/api/delegate/films/1");

//Then
Assert.Equal(404, (int)response.StatusCode);
Expand Down
Binary file modified Slides.pptx
Binary file not shown.
28 changes: 14 additions & 14 deletions TraditionalWebAPI.Tests/Controllers/FilmControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ public async Task Should_return_403_on_invalid_user_when_updating_film()
Assert.Equal(403, (int)response.StatusCode);
}

[Fact]
public async Task Should_return_204_when_updating_film()
{
//Given
var client = this.GetClient();
var film = new Film { Name = "Shrek" };

//When
var response = await client.PutAsync("/api/films/1", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json"));

//Then
Assert.Equal(204, (int)response.StatusCode);
}

[Fact]
public async Task Should_return_204_when_deleting_film()
{
Expand Down Expand Up @@ -170,20 +184,6 @@ public async Task Should_return_403_on_invalid_user_when_deleting_film()
//Then
Assert.Equal(403, (int)response.StatusCode);
}

[Fact]
public async Task Should_return_204_when_updating_film()
{
//Given
var client = this.GetClient();
var film = new Film { Name = "Shrek" };

//When
var response = await client.PutAsync("/api/films/1", new StringContent(JsonConvert.SerializeObject(film), Encoding.UTF8, "application/json"));

//Then
Assert.Equal(204, (int)response.StatusCode);
}

private HttpClient GetClient(IFilmService filmService = null)
{
Expand Down
14 changes: 7 additions & 7 deletions TraditionalWebAPI/Controllers/FilmsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public IActionResult Get(int id)
return this.NotFound();
}

return Ok(film);
return this.Ok(film);
}

// POST api/films
Expand All @@ -53,10 +53,10 @@ public IActionResult Post([FromBody] Film film)
}
catch (InvalidOperationException)
{
return StatusCode(403);
return this.StatusCode(403);
}

return StatusCode(201);
return this.StatusCode(201);
}

// PUT api/films/5
Expand All @@ -74,10 +74,10 @@ public IActionResult Put(int id, [FromBody] Film film)
}
catch (InvalidOperationException)
{
return StatusCode(403);
return this.StatusCode(403);
}

return StatusCode(204);
return this.StatusCode(204);
}

// DELETE api/films/5
Expand All @@ -90,10 +90,10 @@ public IActionResult Delete(int id)
}
catch (InvalidOperationException)
{
return StatusCode(403);
return this.StatusCode(403);
}

return StatusCode(204);
return this.StatusCode(204);
}
}
}
2 changes: 1 addition & 1 deletion TraditionalWebAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class Startup
{
public Startup(IConfiguration configuration) => Configuration = configuration;
public Startup(IConfiguration configuration) => this.Configuration = configuration;

public IConfiguration Configuration { get; }

Expand Down
Binary file added settings.jar
Binary file not shown.

0 comments on commit 59e3f4e

Please sign in to comment.