Adapted Deliverer endpoints with repository spec and automapper
This commit is contained in:
@@ -2,11 +2,12 @@ using FastEndpoints;
|
||||
using PyroFetes.DTO.Deliverer.Request;
|
||||
using PyroFetes.DTO.Deliverer.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
|
||||
namespace PyroFetes.Endpoints.Deliverers;
|
||||
|
||||
public class CreateDelivererEndpoint(
|
||||
PyroFetesDbContext database,
|
||||
DeliverersRepository deliverersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<CreateDelivererDto, GetDelivererDto>
|
||||
{
|
||||
public override void Configure()
|
||||
@@ -23,9 +24,7 @@ public class CreateDelivererEndpoint(
|
||||
Transporter = req.Transporter,
|
||||
};
|
||||
|
||||
database.Deliverers.Add(newDeliverer);
|
||||
|
||||
await database.SaveChangesAsync(ct);
|
||||
await deliverersRepository.AddAsync(newDeliverer, ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetDelivererDto>(newDeliverer), ct);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Deliverers;
|
||||
|
||||
namespace PyroFetes.Endpoints.Deliverers;
|
||||
|
||||
@@ -8,7 +9,7 @@ public class DeleteDelivererRequest
|
||||
{
|
||||
public int DelivererId { get; set; }
|
||||
}
|
||||
public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpoint<DeleteDelivererRequest>
|
||||
public class DeleteDelivererEndpoint(DeliverersRepository deliverersRepository) : Endpoint<DeleteDelivererRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -19,7 +20,7 @@ public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpoint<Del
|
||||
|
||||
public override async Task HandleAsync(DeleteDelivererRequest req, CancellationToken ct)
|
||||
{
|
||||
Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.DelivererId, ct);
|
||||
Deliverer? deliverer = await deliverersRepository.FirstOrDefaultAsync(new GetDelivererByIdSpec(req.DelivererId), ct);
|
||||
|
||||
if (deliverer == null)
|
||||
{
|
||||
@@ -27,9 +28,7 @@ public class DeleteDelivererEndpoint(PyroFetesDbContext database) : Endpoint<Del
|
||||
return;
|
||||
}
|
||||
|
||||
database.Remove(deliverer);
|
||||
|
||||
await database.SaveChangesAsync(ct);
|
||||
await deliverersRepository.DeleteAsync(deliverer, ct);
|
||||
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ using AutoMapper.QueryableExtensions;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Deliverer.Response;
|
||||
using PyroFetes.Repositories;
|
||||
|
||||
namespace PyroFetes.Endpoints.Deliverers;
|
||||
|
||||
public class GetAllDelivererEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetDelivererDto>>
|
||||
public class GetAllDelivererEndpoint(DeliverersRepository deliverersRepository) : EndpointWithoutRequest<List<GetDelivererDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
@@ -16,13 +17,7 @@ public class GetAllDelivererEndpoint(PyroFetesDbContext database) : EndpointWith
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetDelivererDto> deliverers = await database.Deliverers.Select(x => new GetDelivererDto()
|
||||
{
|
||||
Id = x.Id,
|
||||
Transporter = x.Transporter,
|
||||
}).ToListAsync(ct);
|
||||
|
||||
await Send.OkAsync(deliverers, ct);
|
||||
await Send.OkAsync(await deliverersRepository.ProjectToListAsync<GetDelivererDto>(ct), ct);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,8 @@ using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Deliverer.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Deliverers;
|
||||
|
||||
namespace PyroFetes.Endpoints.Deliverers;
|
||||
|
||||
@@ -11,7 +13,7 @@ public class GetDelivererRequest
|
||||
}
|
||||
|
||||
public class GetDelivererEndpoint(
|
||||
PyroFetesDbContext database,
|
||||
DeliverersRepository deliverersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<GetDelivererRequest, GetDelivererDto>
|
||||
{
|
||||
public override void Configure()
|
||||
@@ -23,7 +25,7 @@ public class GetDelivererEndpoint(
|
||||
|
||||
public override async Task HandleAsync(GetDelivererRequest req, CancellationToken ct)
|
||||
{
|
||||
Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.DelivererId, ct);
|
||||
Deliverer? deliverer = await deliverersRepository.FirstOrDefaultAsync(new GetDelivererByIdSpec(req.DelivererId), ct);
|
||||
|
||||
if (deliverer == null)
|
||||
{
|
||||
|
||||
@@ -3,11 +3,13 @@ using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Deliverer.Request;
|
||||
using PyroFetes.DTO.Deliverer.Response;
|
||||
using PyroFetes.Models;
|
||||
using PyroFetes.Repositories;
|
||||
using PyroFetes.Specifications.Deliverers;
|
||||
|
||||
namespace PyroFetes.Endpoints.Deliverers;
|
||||
|
||||
public class UpdateDelivererEndpoint(
|
||||
PyroFetesDbContext database,
|
||||
DeliverersRepository deliverersRepository,
|
||||
AutoMapper.IMapper mapper) : Endpoint<UpdateDelivererDto, GetDelivererDto>
|
||||
{
|
||||
public override void Configure()
|
||||
@@ -19,7 +21,7 @@ public class UpdateDelivererEndpoint(
|
||||
|
||||
public override async Task HandleAsync(UpdateDelivererDto req, CancellationToken ct)
|
||||
{
|
||||
Deliverer? deliverer = await database.Deliverers.SingleOrDefaultAsync(x=>x.Id == req.Id, ct);
|
||||
Deliverer? deliverer = await deliverersRepository.FirstOrDefaultAsync(new GetDelivererByIdSpec(req.Id), ct);
|
||||
|
||||
if (deliverer == null)
|
||||
{
|
||||
@@ -29,7 +31,7 @@ public class UpdateDelivererEndpoint(
|
||||
|
||||
deliverer.Transporter = req.Transporter;
|
||||
|
||||
await database.SaveChangesAsync(ct);
|
||||
await deliverersRepository.UpdateAsync(deliverer,ct);
|
||||
|
||||
await Send.OkAsync(mapper.Map<GetDelivererDto>(deliverer), ct);
|
||||
}
|
||||
|
||||
5
PyroFetes/Repositories/DeliverersRepository.cs
Normal file
5
PyroFetes/Repositories/DeliverersRepository.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Repositories;
|
||||
|
||||
public class DeliverersRepository(PyroFetesDbContext lmdContext, AutoMapper.IMapper mapper) : PyrofetesRepository<Deliverer>(lmdContext, mapper);
|
||||
13
PyroFetes/Specifications/Deliverers/GetDelivererByIdSpec.cs
Normal file
13
PyroFetes/Specifications/Deliverers/GetDelivererByIdSpec.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Ardalis.Specification;
|
||||
using PyroFetes.Models;
|
||||
|
||||
namespace PyroFetes.Specifications.Deliverers;
|
||||
|
||||
public sealed class GetDelivererByIdSpec : Specification<Deliverer>
|
||||
{
|
||||
public GetDelivererByIdSpec(int delivererId)
|
||||
{
|
||||
Query
|
||||
.Where(x => x.Id == delivererId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user