initial commit
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Currency.Cqrs;
|
||||
|
||||
public class UpdateCurrencyRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Symbol { get; set; }
|
||||
public string? CountryOwner { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCurrencyResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateCurrencyCommand(UpdateCurrencyRequest Data) : IRequest<UpdateCurrencyResponse>;
|
||||
|
||||
public class UpdateCurrencyHandler : IRequestHandler<UpdateCurrencyCommand, UpdateCurrencyResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateCurrencyHandler(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<UpdateCurrencyResponse> Handle(UpdateCurrencyCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Currency
|
||||
.AnyAsync(x => x.Code == request.Data.Code && x.Id != request.Data.Id, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Currency", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entity = await _context.Currency
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateCurrencyResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Code = request.Data.Code;
|
||||
entity.Name = request.Data.Name;
|
||||
entity.Symbol = request.Data.Symbol;
|
||||
entity.CountryOwner = request.Data.CountryOwner;
|
||||
entity.Description = request.Data.Description;
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateCurrencyResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user