Skip to content

Commit

Permalink
Implement MediatR in GetAllStock endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorNull committed Jun 6, 2024
1 parent a7063b4 commit 6dc9dfa
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
15 changes: 9 additions & 6 deletions api/Controllers/StockController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using api.Helpers;
using api.Interfaces;
using api.Mappers;
using api.Queries;
using MediatR;
using MethodTimer;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -12,17 +14,19 @@ namespace api.Controllers
[ApiController]
public class StockController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IStockRepo _stockRepo;

//* Used for unit testing
public StockController(IStockRepo stockRepo)
public StockController(IStockRepo stockRepo, IMediator mediator)
{
_stockRepo = stockRepo;
_mediator = mediator;
}

[Time]
[HttpGet]
public async Task<IActionResult> GetAll([FromQuery] QueryObject query)
public async Task<IActionResult> GetAll([FromQuery] QueryObject queryObject)
{

//! Uncomment if ever passing dto to method. Validation performed in class.
Expand All @@ -31,10 +35,9 @@ public async Task<IActionResult> GetAll([FromQuery] QueryObject query)
// return BadRequest(ModelState);
// }

var stocks = await _stockRepo.GetAllAsync(query);
var stockDTOs = stocks.Select(s => s.ToStockDto());

return Ok(stockDTOs);
var query = new GetAllStocksQuery(queryObject);
var result = await _mediator.Send(query);
return Ok(result);
}

[HttpGet("{id:int}")]
Expand Down
23 changes: 23 additions & 0 deletions api/Handlers/GetAllStocksHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using api.DTOs.Stock;
using api.Interfaces;
using api.Mappers;
using api.Queries;
using MediatR;

namespace api.Handlers
{
public class GetAllStocksHandler : IRequestHandler<GetAllStocksQuery, List<StockDto>>
{
private readonly IStockRepo _stockRepo;
public GetAllStocksHandler(IStockRepo stockRepo)
{
_stockRepo = stockRepo;
}

public async Task<List<StockDto>> Handle(GetAllStocksQuery request, CancellationToken cancellationToken)
{
var stocks = await _stockRepo.GetAllAsync(request.QueryObject);
return stocks.Select(s => s.ToStockDto()).ToList();
}
}
}
2 changes: 2 additions & 0 deletions api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using api.Data;
using api.Interfaces;
using api.Models;
Expand Down Expand Up @@ -59,6 +60,7 @@

builder.Services.AddScoped<IStockRepo, StockRepo>();
builder.Services.AddScoped<ICommentRepo, CommentRepo>();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

var app = builder.Build();

Expand Down
17 changes: 17 additions & 0 deletions api/Queries/GetAllStocksQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using api.DTOs.Stock;
using api.Helpers;
using api.Models;
using MediatR;

namespace api.Queries
{
public class GetAllStocksQuery : IRequest<List<StockDto>>
{
public QueryObject QueryObject { get; }

public GetAllStocksQuery(QueryObject queryObject)
{
QueryObject = queryObject;
}
}
}
1 change: 1 addition & 0 deletions api/api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="MethodTimer.Fody" Version="3.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.6" />
Expand Down

0 comments on commit 6dc9dfa

Please sign in to comment.