Refactor Catalogs to use MediatR and CQRS pattern

Replaced direct service usage in CatalogsController with MediatR-based commands and queries for all CRUD operations. Added command/query and handler classes for Catalog operations. Updated dependency injection to register MediatR and removed ICatalogService. Improved code maintainability and testability by adopting CQRS architecture.
This commit is contained in:
OlgunR
2026-01-19 09:00:06 +01:00
parent c8c75b1dc5
commit 870b10779e
15 changed files with 185 additions and 11 deletions

View File

@@ -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<ActionResult<IEnumerable<CatalogReadDto>>> 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<ActionResult<CatalogReadDto>> 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<ActionResult<CatalogReadDto>> 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<ActionResult<CatalogReadDto>> 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<IActionResult> 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();