-
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
Showing
25 changed files
with
141 additions
and
98 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,19 @@ | |
* Created: 2022-12-21-10:02:09 | ||
* Modified: 2022-12-21-10:02:09 | ||
* | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* | ||
* Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved | ||
* License: MIT (https://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
namespace Dgmjr.MediatR.Commands; | ||
|
||
#if NETSTANDARD2_0_OR_GREATER | ||
using Microsoft.AspNetCore.JsonPatch; | ||
|
||
public record struct PatchCommand<TId, TPatchDto, TDto>(JsonPatchDocument<TDto> Patch, TId Id) : IPatchCommand<TId, TPatchDto, TDto> | ||
public record struct PatchCommand<TId, TPatchDto, TDto>(JsonPatchDocument<TDto> Patch, TId Id) | ||
: IPatchCommand<TId, TPatchDto, TDto> | ||
where TId : IComparable, IEquatable<TId> | ||
where TDto : class | ||
{ | ||
|
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 |
---|---|---|
|
@@ -4,15 +4,16 @@ | |
* Created: 2022-12-21-10:01:45 | ||
* Modified: 2022-12-21-10:02:43 | ||
* | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* | ||
* Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved | ||
* License: MIT (https://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
namespace Dgmjr.MediatR.Commands; | ||
|
||
public record struct UpdateCommand<TModel, TId, TUpdateDto, TDto>(TUpdateDto Update) : IUpdateCommand<TModel, TId, TUpdateDto, TDto> | ||
public record struct UpdateCommand<TModel, TId, TUpdateDto, TDto>(TUpdateDto Update) | ||
: IUpdateCommand<TModel, TId, TUpdateDto, TDto> | ||
where TModel : class, IIdentifiable<TId> | ||
where TId : IComparable, IEquatable<TId> | ||
{ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* Created: 2022-12-21-10:51:28 | ||
* Modified: 2022-12-21-10:51:28 | ||
* | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* Author: David G. Moore, Jr. <[email protected]> | ||
* | ||
* Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved | ||
* License: MIT (https://opensource.org/licenses/MIT) | ||
|
@@ -20,7 +20,13 @@ namespace Dgmjr.MediatR; | |
public class CrudHandler<TModel, TDbContext, TInsertDto, TUpdateDto, TViewDto, TId> | ||
: IHaveADbContext<TDbContext>, | ||
ICreateHandler<CreateCommand<TId, TInsertDto, TViewDto>, TId, TInsertDto, TViewDto>, | ||
IUpdateHandler<UpdateCommand<TModel, TId, TUpdateDto, TViewDto>, TId, TModel, TUpdateDto, TViewDto>, | ||
IUpdateHandler< | ||
UpdateCommand<TModel, TId, TUpdateDto, TViewDto>, | ||
TId, | ||
TModel, | ||
TUpdateDto, | ||
TViewDto | ||
>, | ||
IDeleteHandler<DeleteCommand<TId>, TId>, | ||
IDeleteHandler<DeleteCommand<TId, TViewDto>, TId, TViewDto>, | ||
IQueryHandler<Query<TId, TViewDto>, TId, TModel, TViewDto>, | ||
|
@@ -42,17 +48,26 @@ public CrudHandler(IMediator mediator, TDbContext dbContext, IMapper mapper) | |
Mapper = mapper; | ||
} | ||
|
||
public async Task<TViewDto> Handle(CreateCommand<TId, TInsertDto, TViewDto> request, CancellationToken cancellationToken) | ||
public async Task<TViewDto> Handle( | ||
CreateCommand<TId, TInsertDto, TViewDto> request, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var model = Mapper.Map<TModel>(request.Create); | ||
Db.Set<TModel>().Add(model); | ||
await Db.SaveChangesAsync(); | ||
var dto = Mapper.Map<TViewDto>(model); | ||
await Mediator.Publish(new CreatedNotification<TId, TInsertDto, TViewDto>(model.Id, dto), cancellationToken); | ||
await Mediator.Publish( | ||
new CreatedNotification<TId, TInsertDto, TViewDto>(model.Id, dto), | ||
cancellationToken | ||
); | ||
return dto; | ||
} | ||
|
||
public async Task<TViewDto> Handle(UpdateCommand<TModel, TId, TUpdateDto, TViewDto> request, CancellationToken cancellationToken) | ||
public async Task<TViewDto> Handle( | ||
UpdateCommand<TModel, TId, TUpdateDto, TViewDto> request, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var model = Db.Set<TModel>().Find(request.Update); | ||
model = Mapper.Map(request.Update, model); | ||
|
@@ -69,11 +84,17 @@ public Task<Unit> Handle(DeleteCommand<TId> request, CancellationToken cancellat | |
Db.Set<TModel>().Remove(model); | ||
Db.SaveChanges(); | ||
var dto = Mapper.Map<TViewDto>(model); | ||
Mediator.Publish(new DeletedNotification<TId, TViewDto>(dto, request.Id), cancellationToken); | ||
Mediator.Publish( | ||
new DeletedNotification<TId, TViewDto>(dto, request.Id), | ||
cancellationToken | ||
); | ||
return Task.FromResult(Unit.Value); | ||
} | ||
|
||
public async Task<Unit> Handle(DeleteCommand<TId, TViewDto> request, CancellationToken cancellationToken) | ||
public async Task<Unit> Handle( | ||
DeleteCommand<TId, TViewDto> request, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
if (!request.Id.Equals(request.Dto.Id)) | ||
throw new ArgumentException("The Id and Dto.Id must match."); | ||
|
@@ -82,22 +103,44 @@ public async Task<Unit> Handle(DeleteCommand<TId, TViewDto> request, Cancellatio | |
Db.Set<TModel>().Remove(model); | ||
Db.SaveChanges(); | ||
var dto = Mapper.Map<TViewDto>(model); | ||
await Mediator.Publish(new DeletedNotification<TId, TViewDto>(dto, request.Id), cancellationToken); | ||
await Mediator.Publish( | ||
new DeletedNotification<TId, TViewDto>(dto, request.Id), | ||
cancellationToken | ||
); | ||
return Unit.Value; | ||
} | ||
|
||
public async Task<TViewDto> Handle(Query<TId, TViewDto> request, CancellationToken cancellationToken) | ||
public async Task<TViewDto> Handle( | ||
Query<TId, TViewDto> request, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var model = await Db.Set<TModel>().FirstOrDefaultAsync(Mapper.MapExpression<Expression<Func<TViewDto, bool>>, Expression<Func<TModel, bool>>>(request.Predicate)); | ||
var model = await Db.Set<TModel>() | ||
.FirstOrDefaultAsync( | ||
Mapper.MapExpression< | ||
Expression<Func<TViewDto, bool>>, | ||
Expression<Func<TModel, bool>> | ||
>(request.Predicate) | ||
); | ||
if (model is null) | ||
throw new KeyNotFoundException($"A(n) {typeof(TModel).Name} meeting the requested criteria was not found."); | ||
throw new KeyNotFoundException( | ||
$"A(n) {typeof(TModel).Name} meeting the requested criteria was not found." | ||
); | ||
var dto = Mapper.Map<TViewDto>(model); | ||
return dto; | ||
} | ||
|
||
public async Task<IEnumerable<TViewDto>> Handle(Query<TViewDto> request, CancellationToken cancellationToken) | ||
=> await Db.Set<TModel>() | ||
.Where(Mapper.MapExpression<Expression<Func<TViewDto, bool>>, Expression<Func<TModel, bool>>>(request.Predicate)) | ||
.ProjectTo<TViewDto>(Mapper.ConfigurationProvider) | ||
.ToListAsync(); | ||
public async Task<IEnumerable<TViewDto>> Handle( | ||
Query<TViewDto> request, | ||
CancellationToken cancellationToken | ||
) => | ||
await Db.Set<TModel>() | ||
.Where( | ||
Mapper.MapExpression< | ||
Expression<Func<TViewDto, bool>>, | ||
Expression<Func<TModel, bool>> | ||
>(request.Predicate) | ||
) | ||
.ProjectTo<TViewDto>(Mapper.ConfigurationProvider) | ||
.ToListAsync(); | ||
} |
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
Oops, something went wrong.