initial commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.BudgetManager.Commands;
|
||||
|
||||
public class CreateBudgetResult
|
||||
{
|
||||
public Budget? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateBudgetRequest : IRequest<CreateBudgetResult>
|
||||
{
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? BudgetDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public double? Amount { get; init; }
|
||||
public string? CampaignId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateBudgetValidator : AbstractValidator<CreateBudgetRequest>
|
||||
{
|
||||
public CreateBudgetValidator()
|
||||
{
|
||||
RuleFor(x => x.Title).NotEmpty();
|
||||
RuleFor(x => x.BudgetDate).NotNull();
|
||||
RuleFor(x => x.Amount).NotNull();
|
||||
RuleFor(x => x.CampaignId).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateBudgetHandler : IRequestHandler<CreateBudgetRequest, CreateBudgetResult>
|
||||
{
|
||||
private readonly ICommandRepository<Budget> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
|
||||
public CreateBudgetHandler(
|
||||
ICommandRepository<Budget> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
}
|
||||
|
||||
public async Task<CreateBudgetResult> Handle(CreateBudgetRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Budget
|
||||
{
|
||||
CreatedById = request.CreatedById,
|
||||
Number = _numberSequenceService.GenerateNumber(nameof(Budget), "", "BUD"),
|
||||
Title = request.Title,
|
||||
Description = request.Description,
|
||||
BudgetDate = request.BudgetDate,
|
||||
Status = (BudgetStatus)int.Parse(request.Status!),
|
||||
Amount = request.Amount,
|
||||
CampaignId = request.CampaignId
|
||||
};
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateBudgetResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.BudgetManager.Commands;
|
||||
|
||||
public class DeleteBudgetResult
|
||||
{
|
||||
public Budget? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteBudgetRequest : IRequest<DeleteBudgetResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteBudgetValidator : AbstractValidator<DeleteBudgetRequest>
|
||||
{
|
||||
public DeleteBudgetValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteBudgetHandler : IRequestHandler<DeleteBudgetRequest, DeleteBudgetResult>
|
||||
{
|
||||
private readonly ICommandRepository<Budget> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteBudgetHandler(
|
||||
ICommandRepository<Budget> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteBudgetResult> Handle(DeleteBudgetRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.DeletedById;
|
||||
|
||||
_repository.Delete(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new DeleteBudgetResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.BudgetManager.Commands;
|
||||
|
||||
public class UpdateBudgetResult
|
||||
{
|
||||
public Budget? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateBudgetRequest : IRequest<UpdateBudgetResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? BudgetDate { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public double? Amount { get; init; }
|
||||
public string? CampaignId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateBudgetValidator : AbstractValidator<UpdateBudgetRequest>
|
||||
{
|
||||
public UpdateBudgetValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Title).NotEmpty();
|
||||
RuleFor(x => x.BudgetDate).NotNull();
|
||||
RuleFor(x => x.Amount).NotNull();
|
||||
RuleFor(x => x.CampaignId).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateBudgetHandler : IRequestHandler<UpdateBudgetRequest, UpdateBudgetResult>
|
||||
{
|
||||
private readonly ICommandRepository<Budget> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateBudgetHandler(
|
||||
ICommandRepository<Budget> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateBudgetResult> Handle(UpdateBudgetRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.UpdatedById;
|
||||
entity.Title = request.Title;
|
||||
entity.Description = request.Description;
|
||||
entity.BudgetDate = request.BudgetDate;
|
||||
entity.Status = (BudgetStatus)int.Parse(request.Status!);
|
||||
entity.Amount = request.Amount;
|
||||
entity.CampaignId = request.CampaignId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateBudgetResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.BudgetManager.Queries;
|
||||
|
||||
public record GetBudgetByCampaignIdListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Number { get; init; }
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? BudgetDate { get; init; }
|
||||
public BudgetStatus? Status { get; init; }
|
||||
public string? StatusName { get; init; }
|
||||
public double? Amount { get; init; }
|
||||
public string? CampaignId { get; init; }
|
||||
public string? CampaignName { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetByCampaignIdListProfile : Profile
|
||||
{
|
||||
public GetBudgetByCampaignIdListProfile()
|
||||
{
|
||||
CreateMap<Budget, GetBudgetByCampaignIdListDto>()
|
||||
.ForMember(
|
||||
dest => dest.CampaignName,
|
||||
opt => opt.MapFrom(src => src.Campaign != null ? src.Campaign.Title : string.Empty)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.StatusName,
|
||||
opt => opt.MapFrom(src => src.Status.HasValue ? src.Status.Value.ToFriendlyName() : string.Empty)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public class GetBudgetByCampaignIdListResult
|
||||
{
|
||||
public List<GetBudgetByCampaignIdListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetByCampaignIdListRequest : IRequest<GetBudgetByCampaignIdListResult>
|
||||
{
|
||||
public string? CampaignId { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetByCampaignIdListHandler : IRequestHandler<GetBudgetByCampaignIdListRequest, GetBudgetByCampaignIdListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetBudgetByCampaignIdListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetBudgetByCampaignIdListResult> Handle(GetBudgetByCampaignIdListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Budget
|
||||
.AsNoTracking()
|
||||
.IsDeletedEqualTo(false)
|
||||
.Include(x => x.Campaign)
|
||||
.Where(x => x.CampaignId == request.CampaignId)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetBudgetByCampaignIdListDto>>(entities);
|
||||
|
||||
return new GetBudgetByCampaignIdListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.BudgetManager.Queries;
|
||||
|
||||
public record GetBudgetListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Number { get; init; }
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? BudgetDate { get; init; }
|
||||
public BudgetStatus? Status { get; init; }
|
||||
public string? StatusName { get; init; }
|
||||
public double? Amount { get; init; }
|
||||
public string? CampaignId { get; init; }
|
||||
public string? CampaignName { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetListProfile : Profile
|
||||
{
|
||||
public GetBudgetListProfile()
|
||||
{
|
||||
CreateMap<Budget, GetBudgetListDto>()
|
||||
.ForMember(
|
||||
dest => dest.CampaignName,
|
||||
opt => opt.MapFrom(src => src.Campaign != null ? src.Campaign.Title : string.Empty)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.StatusName,
|
||||
opt => opt.MapFrom(src => src.Status.HasValue ? src.Status.Value.ToFriendlyName() : string.Empty)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public class GetBudgetListResult
|
||||
{
|
||||
public List<GetBudgetListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetListRequest : IRequest<GetBudgetListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
public class GetBudgetListHandler : IRequestHandler<GetBudgetListRequest, GetBudgetListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetBudgetListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetBudgetListResult> Handle(GetBudgetListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Budget
|
||||
.AsNoTracking()
|
||||
.IsDeletedEqualTo(request.IsDeleted)
|
||||
.Include(x => x.Campaign)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetBudgetListDto>>(entities);
|
||||
|
||||
return new GetBudgetListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.BudgetManager.Queries;
|
||||
|
||||
public class GetBudgetSingleProfile : Profile
|
||||
{
|
||||
public GetBudgetSingleProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetBudgetSingleResult
|
||||
{
|
||||
public Budget? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetSingleRequest : IRequest<GetBudgetSingleResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetSingleValidator : AbstractValidator<GetBudgetSingleRequest>
|
||||
{
|
||||
public GetBudgetSingleValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetBudgetSingleHandler : IRequestHandler<GetBudgetSingleRequest, GetBudgetSingleResult>
|
||||
{
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetBudgetSingleHandler(
|
||||
IQueryContext context
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetBudgetSingleResult> Handle(GetBudgetSingleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Budget
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Campaign)
|
||||
.Where(x => x.Id == request.Id)
|
||||
.AsQueryable();
|
||||
|
||||
var entity = await query.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
return new GetBudgetSingleResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Enums;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.BudgetManager.Queries;
|
||||
|
||||
public record GetBudgetStatusListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetStatusListProfile : Profile
|
||||
{
|
||||
public GetBudgetStatusListProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetBudgetStatusListResult
|
||||
{
|
||||
public List<GetBudgetStatusListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetBudgetStatusListRequest : IRequest<GetBudgetStatusListResult>
|
||||
{
|
||||
}
|
||||
|
||||
public class GetBudgetStatusListHandler : IRequestHandler<GetBudgetStatusListRequest, GetBudgetStatusListResult>
|
||||
{
|
||||
|
||||
public GetBudgetStatusListHandler()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<GetBudgetStatusListResult> Handle(GetBudgetStatusListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var statuses = Enum.GetValues(typeof(BudgetStatus))
|
||||
.Cast<BudgetStatus>()
|
||||
.Select(status => new GetBudgetStatusListDto
|
||||
{
|
||||
Id = ((int)status).ToString(),
|
||||
Name = status.ToFriendlyName()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await Task.CompletedTask;
|
||||
|
||||
return new GetBudgetStatusListResult
|
||||
{
|
||||
Data = statuses
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user