ref: project structure
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.application.MasterData;
|
||||
|
||||
public record CreateMasterDataCommand(SaveMasterDataRequest Request) : IRequest<MasterDataDto>;
|
||||
|
||||
public class CreateMasterDataHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<CreateMasterDataCommand, MasterDataDto>
|
||||
{
|
||||
public async Task<MasterDataDto> Handle(CreateMasterDataCommand command, CancellationToken ct)
|
||||
{
|
||||
var entry = await MasterDataValidation.ValidateAsync(uow, command.Request, null, ct);
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
entry.Id = Guid.NewGuid();
|
||||
entry.CreatedAt = now;
|
||||
entry.UpdatedAt = now;
|
||||
|
||||
uow.MasterData.Add(entry);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return mapper.Map<MasterDataDto>(entry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.application.MasterData;
|
||||
|
||||
public record DeleteMasterDataCommand(Guid Id) : IRequest;
|
||||
|
||||
public class DeleteMasterDataHandler(IUnitOfWork uow) : IRequestHandler<DeleteMasterDataCommand>
|
||||
{
|
||||
public async Task Handle(DeleteMasterDataCommand command, CancellationToken ct)
|
||||
{
|
||||
var entry = await uow.MasterData.GetByIdAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("Master data entry not found");
|
||||
|
||||
uow.MasterData.Remove(entry);
|
||||
await uow.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.application.MasterData;
|
||||
|
||||
public record UpdateMasterDataCommand(Guid Id, SaveMasterDataRequest Request) : IRequest<MasterDataDto>;
|
||||
|
||||
public class UpdateMasterDataHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<UpdateMasterDataCommand, MasterDataDto>
|
||||
{
|
||||
public async Task<MasterDataDto> Handle(UpdateMasterDataCommand command, CancellationToken ct)
|
||||
{
|
||||
var existing = await uow.MasterData.GetByIdAsync(command.Id, ct)
|
||||
?? throw new NotFoundException("Master data entry not found");
|
||||
|
||||
var updated = await MasterDataValidation.ValidateAsync(uow, command.Request, command.Id, ct);
|
||||
|
||||
existing.Group = updated.Group;
|
||||
existing.Label = updated.Label;
|
||||
existing.Value = updated.Value;
|
||||
existing.SortOrder = updated.SortOrder;
|
||||
existing.IsActive = updated.IsActive;
|
||||
existing.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await uow.SaveChangesAsync(ct);
|
||||
return mapper.Map<MasterDataDto>(existing);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace mws.backend.dotnet.application.MasterData;
|
||||
|
||||
public class MasterDataDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Group { get; set; } = string.Empty;
|
||||
public string Label { get; set; } = string.Empty;
|
||||
public string Value { get; set; } = string.Empty;
|
||||
public int SortOrder { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
|
||||
public class SaveMasterDataRequest
|
||||
{
|
||||
public string Group { get; set; } = string.Empty;
|
||||
public string Label { get; set; } = string.Empty;
|
||||
public string Value { get; set; } = string.Empty;
|
||||
public int SortOrder { get; set; }
|
||||
public bool IsActive { get; set; } = true;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.domain.MasterData;
|
||||
|
||||
namespace mws.backend.dotnet.application.MasterData;
|
||||
|
||||
internal static class MasterDataValidation
|
||||
{
|
||||
public static async Task<MasterDataEntry> ValidateAsync(IUnitOfWork uow, SaveMasterDataRequest request, Guid? excludeId, CancellationToken ct)
|
||||
{
|
||||
var group = request.Group.Trim();
|
||||
var label = request.Label.Trim();
|
||||
var value = request.Value.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(group) || string.IsNullOrWhiteSpace(label) || string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
throw new BadRequestException("Group, label and value are required");
|
||||
}
|
||||
|
||||
if (await uow.MasterData.ExistsAsync(group, value, excludeId, ct))
|
||||
{
|
||||
throw new BadRequestException("An entry with this group and value already exists");
|
||||
}
|
||||
|
||||
return new MasterDataEntry
|
||||
{
|
||||
Group = group,
|
||||
Label = label,
|
||||
Value = value,
|
||||
SortOrder = request.SortOrder,
|
||||
IsActive = request.IsActive,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.application.MasterData;
|
||||
|
||||
public record GetMasterDataByGroupQuery(string Group) : IRequest<List<MasterDataDto>>;
|
||||
|
||||
public class GetMasterDataByGroupHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetMasterDataByGroupQuery, List<MasterDataDto>>
|
||||
{
|
||||
public async Task<List<MasterDataDto>> Handle(GetMasterDataByGroupQuery query, CancellationToken ct)
|
||||
{
|
||||
var entries = await uow.MasterData.GetActiveByGroupAsync(query.Group, ct);
|
||||
return mapper.Map<List<MasterDataDto>>(entries);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using AutoMapper;
|
||||
using MediatR;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.application.MasterData;
|
||||
|
||||
public record GetMasterDataListQuery(string? Group, int Page, int PageSize) : IRequest<PagedResult<MasterDataDto>>;
|
||||
|
||||
public class GetMasterDataListHandler(IUnitOfWork uow, IMapper mapper) : IRequestHandler<GetMasterDataListQuery, PagedResult<MasterDataDto>>
|
||||
{
|
||||
public async Task<PagedResult<MasterDataDto>> Handle(GetMasterDataListQuery query, CancellationToken ct)
|
||||
{
|
||||
var entries = await uow.MasterData.GetAllAsync(query.Group, query.Page, query.PageSize, ct);
|
||||
return new PagedResult<MasterDataDto>
|
||||
{
|
||||
Items = mapper.Map<List<MasterDataDto>>(entries.Items),
|
||||
TotalCount = entries.TotalCount,
|
||||
Page = entries.Page,
|
||||
PageSize = entries.PageSize,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user