diff --git a/DbFirst.API/Controllers/CatalogsController.cs b/DbFirst.API/Controllers/CatalogsController.cs index 086f418..2b13d46 100644 --- a/DbFirst.API/Controllers/CatalogsController.cs +++ b/DbFirst.API/Controllers/CatalogsController.cs @@ -1,4 +1,7 @@ using DbFirst.Application.Catalogs; +using DbFirst.Application.Catalogs.Commands; +using DbFirst.Application.Catalogs.Queries; +using MediatR; using Microsoft.AspNetCore.Mvc; namespace DbFirst.API.Controllers; @@ -7,24 +10,24 @@ namespace DbFirst.API.Controllers; [Route("api/[controller]")] public class CatalogsController : ControllerBase { - private readonly ICatalogService _service; + private readonly IMediator _mediator; - public CatalogsController(ICatalogService service) + public CatalogsController(IMediator mediator) { - _service = service; + _mediator = mediator; } [HttpGet] public async Task>> GetAll(CancellationToken cancellationToken) { - var result = await _service.GetAllAsync(cancellationToken); + var result = await _mediator.Send(new GetAllCatalogsQuery(), cancellationToken); return Ok(result); } [HttpGet("{id:int}")] public async Task> GetById(int id, CancellationToken cancellationToken) { - var result = await _service.GetByIdAsync(id, cancellationToken); + var result = await _mediator.Send(new GetCatalogByIdQuery(id), cancellationToken); if (result == null) { return NotFound(); @@ -35,7 +38,7 @@ public class CatalogsController : ControllerBase [HttpPost] public async Task> Create(CatalogWriteDto dto, CancellationToken cancellationToken) { - var created = await _service.CreateAsync(dto, cancellationToken); + var created = await _mediator.Send(new CreateCatalogCommand(dto), cancellationToken); if (created == null) { return Conflict(); @@ -46,7 +49,7 @@ public class CatalogsController : ControllerBase [HttpPut("{id:int}")] public async Task> Update(int id, CatalogWriteDto dto, CancellationToken cancellationToken) { - var current = await _service.GetByIdAsync(id, cancellationToken); + var current = await _mediator.Send(new GetCatalogByIdQuery(id), cancellationToken); if (current == null) { return NotFound(); @@ -56,7 +59,7 @@ public class CatalogsController : ControllerBase return BadRequest("CatTitle cannot be changed."); } - var updated = await _service.UpdateAsync(id, dto, cancellationToken); + var updated = await _mediator.Send(new UpdateCatalogCommand(id, dto), cancellationToken); if (updated == null) { return NotFound(); @@ -67,7 +70,7 @@ public class CatalogsController : ControllerBase [HttpDelete("{id:int}")] public async Task Delete(int id, CancellationToken cancellationToken) { - var deleted = await _service.DeleteAsync(id, cancellationToken); + var deleted = await _mediator.Send(new DeleteCatalogCommand(id), cancellationToken); if (!deleted) { return NotFound(); diff --git a/DbFirst.API/DbFirst.API.csproj b/DbFirst.API/DbFirst.API.csproj index 3e25a22..433fe77 100644 --- a/DbFirst.API/DbFirst.API.csproj +++ b/DbFirst.API/DbFirst.API.csproj @@ -16,6 +16,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DbFirst.API/Program.cs b/DbFirst.API/Program.cs index 1cb9c61..12ce93e 100644 --- a/DbFirst.API/Program.cs +++ b/DbFirst.API/Program.cs @@ -3,7 +3,7 @@ using DbFirst.Application.Catalogs; using DbFirst.Domain.Repositories; using DbFirst.Infrastructure; using DbFirst.Infrastructure.Repositories; -using Microsoft.EntityFrameworkCore; +using MediatR; using DbFirst.API.Middleware; var builder = WebApplication.CreateBuilder(args); @@ -38,7 +38,6 @@ builder.Services.AddInfrastructure(builder.Configuration); builder.Services.AddApplication(); builder.Services.AddScoped(); -builder.Services.AddScoped(); var app = builder.Build(); diff --git a/DbFirst.Application/Catalogs/Commands/CreateCatalogCommand.cs b/DbFirst.Application/Catalogs/Commands/CreateCatalogCommand.cs new file mode 100644 index 0000000..6653794 --- /dev/null +++ b/DbFirst.Application/Catalogs/Commands/CreateCatalogCommand.cs @@ -0,0 +1,6 @@ +using DbFirst.Application.Catalogs; +using MediatR; + +namespace DbFirst.Application.Catalogs.Commands; + +public record CreateCatalogCommand(CatalogWriteDto Dto) : IRequest; diff --git a/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs b/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs new file mode 100644 index 0000000..3be85da --- /dev/null +++ b/DbFirst.Application/Catalogs/Commands/CreateCatalogHandler.cs @@ -0,0 +1,36 @@ +using AutoMapper; +using DbFirst.Domain.Entities; +using DbFirst.Domain.Repositories; +using MediatR; + +namespace DbFirst.Application.Catalogs.Commands; + +public class CreateCatalogHandler : IRequestHandler +{ + private readonly ICatalogRepository _repository; + private readonly IMapper _mapper; + + public CreateCatalogHandler(ICatalogRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + public async Task Handle(CreateCatalogCommand request, CancellationToken cancellationToken) + { + var existing = await _repository.GetByTitleAsync(request.Dto.CatTitle, cancellationToken); + if (existing != null) + { + return null; + } + + var entity = _mapper.Map(request.Dto); + entity.AddedWho = "system"; + entity.AddedWhen = DateTime.UtcNow; + entity.ChangedWho = "system"; + entity.ChangedWhen = DateTime.UtcNow; + + var created = await _repository.InsertAsync(entity, cancellationToken); + return _mapper.Map(created); + } +} diff --git a/DbFirst.Application/Catalogs/Commands/DeleteCatalogCommand.cs b/DbFirst.Application/Catalogs/Commands/DeleteCatalogCommand.cs new file mode 100644 index 0000000..72892db --- /dev/null +++ b/DbFirst.Application/Catalogs/Commands/DeleteCatalogCommand.cs @@ -0,0 +1,5 @@ +using MediatR; + +namespace DbFirst.Application.Catalogs.Commands; + +public record DeleteCatalogCommand(int Id) : IRequest; diff --git a/DbFirst.Application/Catalogs/Commands/DeleteCatalogHandler.cs b/DbFirst.Application/Catalogs/Commands/DeleteCatalogHandler.cs new file mode 100644 index 0000000..8c5e033 --- /dev/null +++ b/DbFirst.Application/Catalogs/Commands/DeleteCatalogHandler.cs @@ -0,0 +1,19 @@ +using DbFirst.Domain.Repositories; +using MediatR; + +namespace DbFirst.Application.Catalogs.Commands; + +public class DeleteCatalogHandler : IRequestHandler +{ + private readonly ICatalogRepository _repository; + + public DeleteCatalogHandler(ICatalogRepository repository) + { + _repository = repository; + } + + public async Task Handle(DeleteCatalogCommand request, CancellationToken cancellationToken) + { + return await _repository.DeleteAsync(request.Id, cancellationToken); + } +} diff --git a/DbFirst.Application/Catalogs/Commands/UpdateCatalogCommand.cs b/DbFirst.Application/Catalogs/Commands/UpdateCatalogCommand.cs new file mode 100644 index 0000000..ab8f272 --- /dev/null +++ b/DbFirst.Application/Catalogs/Commands/UpdateCatalogCommand.cs @@ -0,0 +1,6 @@ +using DbFirst.Application.Catalogs; +using MediatR; + +namespace DbFirst.Application.Catalogs.Commands; + +public record UpdateCatalogCommand(int Id, CatalogWriteDto Dto) : IRequest; diff --git a/DbFirst.Application/Catalogs/Commands/UpdateCatalogHandler.cs b/DbFirst.Application/Catalogs/Commands/UpdateCatalogHandler.cs new file mode 100644 index 0000000..7f54733 --- /dev/null +++ b/DbFirst.Application/Catalogs/Commands/UpdateCatalogHandler.cs @@ -0,0 +1,38 @@ +using AutoMapper; +using DbFirst.Domain.Entities; +using DbFirst.Domain.Repositories; +using MediatR; + +namespace DbFirst.Application.Catalogs.Commands; + +public class UpdateCatalogHandler : IRequestHandler +{ + private readonly ICatalogRepository _repository; + private readonly IMapper _mapper; + + public UpdateCatalogHandler(ICatalogRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + public async Task Handle(UpdateCatalogCommand request, CancellationToken cancellationToken) + { + var existing = await _repository.GetByIdAsync(request.Id, cancellationToken); + if (existing == null) + { + return null; + } + + var entity = _mapper.Map(request.Dto); + entity.Guid = request.Id; + entity.CatTitle = existing.CatTitle; + entity.AddedWho = existing.AddedWho; + entity.AddedWhen = existing.AddedWhen; + entity.ChangedWho = "system"; + entity.ChangedWhen = DateTime.UtcNow; + + var updated = await _repository.UpdateAsync(request.Id, entity, cancellationToken); + return updated == null ? null : _mapper.Map(updated); + } +} diff --git a/DbFirst.Application/Catalogs/Queries/GetAllCatalogsHandler.cs b/DbFirst.Application/Catalogs/Queries/GetAllCatalogsHandler.cs new file mode 100644 index 0000000..b824f66 --- /dev/null +++ b/DbFirst.Application/Catalogs/Queries/GetAllCatalogsHandler.cs @@ -0,0 +1,23 @@ +using AutoMapper; +using DbFirst.Domain.Repositories; +using MediatR; + +namespace DbFirst.Application.Catalogs.Queries; + +public class GetAllCatalogsHandler : IRequestHandler> +{ + private readonly ICatalogRepository _repository; + private readonly IMapper _mapper; + + public GetAllCatalogsHandler(ICatalogRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + public async Task> Handle(GetAllCatalogsQuery request, CancellationToken cancellationToken) + { + var items = await _repository.GetAllAsync(cancellationToken); + return _mapper.Map>(items); + } +} diff --git a/DbFirst.Application/Catalogs/Queries/GetAllCatalogsQuery.cs b/DbFirst.Application/Catalogs/Queries/GetAllCatalogsQuery.cs new file mode 100644 index 0000000..234e7e8 --- /dev/null +++ b/DbFirst.Application/Catalogs/Queries/GetAllCatalogsQuery.cs @@ -0,0 +1,6 @@ +using DbFirst.Application.Catalogs; +using MediatR; + +namespace DbFirst.Application.Catalogs.Queries; + +public record GetAllCatalogsQuery : IRequest>; diff --git a/DbFirst.Application/Catalogs/Queries/GetCatalogByIdHandler.cs b/DbFirst.Application/Catalogs/Queries/GetCatalogByIdHandler.cs new file mode 100644 index 0000000..f61aed3 --- /dev/null +++ b/DbFirst.Application/Catalogs/Queries/GetCatalogByIdHandler.cs @@ -0,0 +1,23 @@ +using AutoMapper; +using DbFirst.Domain.Repositories; +using MediatR; + +namespace DbFirst.Application.Catalogs.Queries; + +public class GetCatalogByIdHandler : IRequestHandler +{ + private readonly ICatalogRepository _repository; + private readonly IMapper _mapper; + + public GetCatalogByIdHandler(ICatalogRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + public async Task Handle(GetCatalogByIdQuery request, CancellationToken cancellationToken) + { + var item = await _repository.GetByIdAsync(request.Id, cancellationToken); + return item == null ? null : _mapper.Map(item); + } +} diff --git a/DbFirst.Application/Catalogs/Queries/GetCatalogByIdQuery.cs b/DbFirst.Application/Catalogs/Queries/GetCatalogByIdQuery.cs new file mode 100644 index 0000000..7fbb7e4 --- /dev/null +++ b/DbFirst.Application/Catalogs/Queries/GetCatalogByIdQuery.cs @@ -0,0 +1,6 @@ +using DbFirst.Application.Catalogs; +using MediatR; + +namespace DbFirst.Application.Catalogs.Queries; + +public record GetCatalogByIdQuery(int Id) : IRequest; diff --git a/DbFirst.Application/DbFirst.Application.csproj b/DbFirst.Application/DbFirst.Application.csproj index ed9e08e..ec03168 100644 --- a/DbFirst.Application/DbFirst.Application.csproj +++ b/DbFirst.Application/DbFirst.Application.csproj @@ -10,6 +10,7 @@ + diff --git a/DbFirst.Application/DependencyInjection.cs b/DbFirst.Application/DependencyInjection.cs index 1089776..e02bea1 100644 --- a/DbFirst.Application/DependencyInjection.cs +++ b/DbFirst.Application/DependencyInjection.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using MediatR; namespace DbFirst.Application; @@ -7,6 +8,7 @@ public static class DependencyInjection public static IServiceCollection AddApplication(this IServiceCollection services) { services.AddAutoMapper(typeof(DependencyInjection).Assembly); + services.AddMediatR(typeof(DependencyInjection).Assembly); return services; } }