initial commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Currency.Cqrs;
|
||||
|
||||
public class CreateCurrencyRequest
|
||||
{
|
||||
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 class CreateCurrencyResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Code { get; set; }
|
||||
}
|
||||
|
||||
public record CreateCurrencyCommand(CreateCurrencyRequest Data) : IRequest<CreateCurrencyResponse>;
|
||||
public class CreateCurrencyHandler : IRequestHandler<CreateCurrencyCommand, CreateCurrencyResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateCurrencyHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateCurrencyResponse> Handle(CreateCurrencyCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var isExists = await _context.Currency
|
||||
.AnyAsync(x => x.Code == request.Data.Code, cancellationToken);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
throw new AlreadyExistsException("Currency", request.Data.Code ?? string.Empty);
|
||||
}
|
||||
|
||||
var entityName = nameof(Currency);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Currency
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Code = request.Data.Code,
|
||||
Name = request.Data.Name,
|
||||
Symbol = request.Data.Symbol,
|
||||
CountryOwner = request.Data.CountryOwner,
|
||||
Description = request.Data.Description
|
||||
};
|
||||
|
||||
_context.Currency.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateCurrencyResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Code = entity.Code
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Setting.Currency.Cqrs;
|
||||
|
||||
public class CreateCurrencyValidator : AbstractValidator<CreateCurrencyRequest>
|
||||
{
|
||||
public CreateCurrencyValidator()
|
||||
{
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Currency Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Currency Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Symbol)
|
||||
.NotEmpty().WithMessage("Symbol is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CountryOwner)
|
||||
.NotEmpty().WithMessage("Country Owner is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Currency.Cqrs;
|
||||
|
||||
public record DeleteCurrencyByIdRequest(string Id);
|
||||
|
||||
public class DeleteCurrencyByIdHandler : IRequestHandler<DeleteCurrencyByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteCurrencyByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteCurrencyByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Currency
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Currency.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
public record DeleteCurrencyByIdCommand(DeleteCurrencyByIdRequest Data) : IRequest<bool>;
|
||||
@@ -0,0 +1,49 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Currency.Cqrs;
|
||||
|
||||
public class GetCurrencyByIdResponse
|
||||
{
|
||||
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 record GetCurrencyByIdQuery(string Id) : IRequest<GetCurrencyByIdResponse?>;
|
||||
|
||||
public class GetCurrencyByIdHandler : IRequestHandler<GetCurrencyByIdQuery, GetCurrencyByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCurrencyByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetCurrencyByIdResponse?> Handle(GetCurrencyByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Currency
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetCurrencyByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Symbol = x.Symbol,
|
||||
CountryOwner = x.CountryOwner,
|
||||
Description = x.Description,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Setting.Currency.Cqrs;
|
||||
|
||||
public class GetCurrencyListResponse
|
||||
{
|
||||
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 DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetCurrencyListQuery() : IRequest<List<GetCurrencyListResponse>>;
|
||||
|
||||
public class GetCurrencyListHandler : IRequestHandler<GetCurrencyListQuery, List<GetCurrencyListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCurrencyListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetCurrencyListResponse>> Handle(GetCurrencyListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Currency
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Code)
|
||||
.Select(x => new GetCurrencyListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
Code = x.Code,
|
||||
Name = x.Name,
|
||||
Symbol = x.Symbol,
|
||||
CountryOwner = x.CountryOwner,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Setting.Currency.Cqrs;
|
||||
|
||||
public class UpdateCurrencyValidator : AbstractValidator<UpdateCurrencyRequest>
|
||||
{
|
||||
public UpdateCurrencyValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Code)
|
||||
.NotEmpty().WithMessage("Currency Code is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Currency Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.Symbol)
|
||||
.NotEmpty().WithMessage("Symbol is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CountryOwner)
|
||||
.NotEmpty().WithMessage("Country Owner is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user