-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added books module * added unit tests for books module, some other small improvements * removed redundant files * refreshed README * removed iis entry * removed books module from code coverage * refreshed README * clean-up around approval tests
- Loading branch information
1 parent
e56786d
commit 8e28f4d
Showing
45 changed files
with
730 additions
and
22 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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
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
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
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
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
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
42 changes: 42 additions & 0 deletions
42
src/HappyCode.NetCoreBoilerplate.BooksModule/BooksModuleConfigurations.cs
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,42 @@ | ||
using System.Data; | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Features.DeleteBook; | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBook; | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBooks; | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Features.UpsertBook; | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Infrastructure; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.BooksModule; | ||
|
||
public static class BooksModuleConfigurations | ||
{ | ||
public static IServiceCollection AddBooksModule(this IServiceCollection services, IConfiguration configuration) | ||
=> services | ||
.AddEndpointsApiExplorer() | ||
.AddSingleton<IDbConnection>(sp => new SqliteConnection(configuration.GetConnectionString("SqliteDb"))) | ||
.AddSingleton<DbInitializer>() | ||
; | ||
|
||
public static IEndpointRouteBuilder MapBooksModule(this IEndpointRouteBuilder endpoints) | ||
=> endpoints | ||
.MapGroup("/api/books") | ||
.AddEndpointFilter<AuthFilter>() | ||
.MapGetBooksEndpoint() | ||
.MapGetBookEndpoint() | ||
.MapUpsertBookEndpoint() | ||
.MapDeleteBookEndpoint() | ||
; | ||
|
||
public static IApplicationBuilder InitBooksModule(this IApplicationBuilder app) | ||
{ | ||
var initializer = app.ApplicationServices.GetRequiredService<DbInitializer>(); | ||
initializer.Init(); | ||
|
||
return app; | ||
} | ||
} |
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,3 @@ | ||
namespace HappyCode.NetCoreBoilerplate.BooksModule.Dtos; | ||
|
||
public record struct BookDto (int? Id, string Title); |
17 changes: 17 additions & 0 deletions
17
src/HappyCode.NetCoreBoilerplate.BooksModule/Features/DeleteBook/Command.cs
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,17 @@ | ||
using System.Data; | ||
using Dapper; | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Dtos; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.BooksModule.Features.DeleteBook; | ||
|
||
internal static class Command | ||
{ | ||
private static readonly string _deleteBook = @$" | ||
DELETE FROM Books | ||
WHERE | ||
{nameof(BookDto.Id)} = @id | ||
"; | ||
|
||
public static async Task<int> DeleteBookAsync(this IDbConnection db, int id, CancellationToken cancellationToken) | ||
=> await db.ExecuteAsync(new CommandDefinition(_deleteBook, new { id }, cancellationToken: cancellationToken)); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/HappyCode.NetCoreBoilerplate.BooksModule/Features/DeleteBook/Endpoint.cs
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,31 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using System.Data; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.BooksModule.Features.DeleteBook; | ||
|
||
internal static class Endpoint | ||
{ | ||
public static IEndpointRouteBuilder MapDeleteBookEndpoint(this IEndpointRouteBuilder endpoints) | ||
{ | ||
endpoints | ||
.MapDelete( | ||
"/{id:int}", | ||
async ( | ||
int id, | ||
IDbConnection db, | ||
CancellationToken ct | ||
) => | ||
{ | ||
var affected = await db.DeleteBookAsync(id, ct); | ||
return affected > 0 | ||
? Results.NoContent() | ||
: Results.NotFound(); | ||
}) | ||
.Produces(StatusCodes.Status204NoContent) | ||
.Produces(StatusCodes.Status404NotFound) | ||
.WithTags("Books"); | ||
return endpoints; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/HappyCode.NetCoreBoilerplate.BooksModule/Features/GetBook/Endpoint.cs
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,32 @@ | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Dtos; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using System.Data; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBook; | ||
|
||
internal static class Endpoint | ||
{ | ||
public static IEndpointRouteBuilder MapGetBookEndpoint(this IEndpointRouteBuilder endpoints) | ||
{ | ||
endpoints | ||
.MapGet( | ||
"/{id:int}", | ||
async ( | ||
int id, | ||
IDbConnection db, | ||
CancellationToken ct | ||
) => | ||
{ | ||
var book = await db.GetBookAsync(id, ct); | ||
return book.Id is not null | ||
? Results.Ok(book) | ||
: Results.NotFound(); | ||
}) | ||
.Produces<BookDto>() | ||
.Produces(StatusCodes.Status404NotFound) | ||
.WithTags("Books"); | ||
return endpoints; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/HappyCode.NetCoreBoilerplate.BooksModule/Features/GetBook/Query.cs
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,21 @@ | ||
using System.Data; | ||
using Dapper; | ||
using HappyCode.NetCoreBoilerplate.BooksModule.Dtos; | ||
|
||
namespace HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBook; | ||
|
||
internal static class Query | ||
{ | ||
private static readonly string _getBook = @$" | ||
SELECT | ||
{nameof(BookDto.Id)}, | ||
{nameof(BookDto.Title)} | ||
FROM | ||
Books | ||
WHERE | ||
{nameof(BookDto.Id)} = @id | ||
"; | ||
|
||
public static Task<BookDto> GetBookAsync(this IDbConnection db, int id, CancellationToken cancellationToken) | ||
=> db.QuerySingleOrDefaultAsync<BookDto>(new CommandDefinition(_getBook, new { id }, cancellationToken: cancellationToken)); | ||
} |
Oops, something went wrong.