initial commit
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CampaignManager.Commands;
|
||||
|
||||
public class CreateCampaignResult
|
||||
{
|
||||
public Campaign? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCampaignRequest : IRequest<CreateCampaignResult>
|
||||
{
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public double? TargetRevenueAmount { get; init; }
|
||||
public DateTime? CampaignDateStart { get; init; }
|
||||
public DateTime? CampaignDateFinish { get; init; }
|
||||
public string? SalesTeamId { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateCampaignValidator : AbstractValidator<CreateCampaignRequest>
|
||||
{
|
||||
public CreateCampaignValidator()
|
||||
{
|
||||
RuleFor(x => x.Title).NotEmpty();
|
||||
RuleFor(x => x.SalesTeamId).NotEmpty();
|
||||
RuleFor(x => x.CampaignDateStart).NotEmpty();
|
||||
RuleFor(x => x.CampaignDateFinish).NotEmpty();
|
||||
RuleFor(x => x.TargetRevenueAmount).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateCampaignHandler : IRequestHandler<CreateCampaignRequest, CreateCampaignResult>
|
||||
{
|
||||
private readonly ICommandRepository<Campaign> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
|
||||
public CreateCampaignHandler(
|
||||
ICommandRepository<Campaign> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
}
|
||||
|
||||
public async Task<CreateCampaignResult> Handle(CreateCampaignRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Campaign();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Number = _numberSequenceService.GenerateNumber(nameof(Campaign), "", "CAM");
|
||||
entity.Title = request.Title;
|
||||
entity.SalesTeamId = request.SalesTeamId;
|
||||
entity.Description = request.Description;
|
||||
entity.TargetRevenueAmount = request.TargetRevenueAmount;
|
||||
entity.CampaignDateStart = request.CampaignDateStart;
|
||||
entity.CampaignDateFinish = request.CampaignDateFinish;
|
||||
entity.Status = (CampaignStatus)int.Parse(request.Status!);
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateCampaignResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CampaignManager.Commands;
|
||||
|
||||
public class DeleteCampaignResult
|
||||
{
|
||||
public Campaign? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteCampaignRequest : IRequest<DeleteCampaignResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteCampaignValidator : AbstractValidator<DeleteCampaignRequest>
|
||||
{
|
||||
public DeleteCampaignValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteCampaignHandler : IRequestHandler<DeleteCampaignRequest, DeleteCampaignResult>
|
||||
{
|
||||
private readonly ICommandRepository<Campaign> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteCampaignHandler(
|
||||
ICommandRepository<Campaign> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteCampaignResult> Handle(DeleteCampaignRequest 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 DeleteCampaignResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using Domain.Enums;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CampaignManager.Commands;
|
||||
|
||||
public class UpdateCampaignResult
|
||||
{
|
||||
public Campaign? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCampaignRequest : IRequest<UpdateCampaignResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public double? TargetRevenueAmount { get; init; }
|
||||
public DateTime? CampaignDateStart { get; init; }
|
||||
public DateTime? CampaignDateFinish { get; init; }
|
||||
public string? SalesTeamId { get; init; }
|
||||
public string? Status { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateCampaignValidator : AbstractValidator<UpdateCampaignRequest>
|
||||
{
|
||||
public UpdateCampaignValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Title).NotEmpty();
|
||||
RuleFor(x => x.SalesTeamId).NotEmpty();
|
||||
RuleFor(x => x.CampaignDateStart).NotEmpty();
|
||||
RuleFor(x => x.CampaignDateFinish).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
RuleFor(x => x.Status).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateCampaignHandler : IRequestHandler<UpdateCampaignRequest, UpdateCampaignResult>
|
||||
{
|
||||
private readonly ICommandRepository<Campaign> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateCampaignHandler(
|
||||
ICommandRepository<Campaign> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateCampaignResult> Handle(UpdateCampaignRequest 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.SalesTeamId = request.SalesTeamId;
|
||||
entity.Description = request.Description;
|
||||
entity.TargetRevenueAmount = request.TargetRevenueAmount;
|
||||
entity.CampaignDateStart = request.CampaignDateStart;
|
||||
entity.CampaignDateFinish = request.CampaignDateFinish;
|
||||
entity.Status = (CampaignStatus)int.Parse(request.Status!);
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateCampaignResult
|
||||
{
|
||||
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.CampaignManager.Queries;
|
||||
|
||||
public record GetCampaignListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Number { get; init; }
|
||||
public string? Title { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public double? TargetRevenueAmount { get; init; }
|
||||
public DateTime? CampaignDateStart { get; init; }
|
||||
public DateTime? CampaignDateFinish { get; init; }
|
||||
public CampaignStatus? Status { get; init; }
|
||||
public string? StatusName { get; init; }
|
||||
public string? SalesTeamId { get; init; }
|
||||
public string? SalesTeamName { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetCampaignListProfile : Profile
|
||||
{
|
||||
public GetCampaignListProfile()
|
||||
{
|
||||
CreateMap<Campaign, GetCampaignListDto>()
|
||||
.ForMember(
|
||||
dest => dest.StatusName,
|
||||
opt => opt.MapFrom(src => src.Status.HasValue ? src.Status.Value.ToFriendlyName() : string.Empty)
|
||||
)
|
||||
.ForMember(
|
||||
dest => dest.SalesTeamName,
|
||||
opt => opt.MapFrom(src => src.SalesTeam != null ? src.SalesTeam.Name : string.Empty)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCampaignListResult
|
||||
{
|
||||
public List<GetCampaignListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCampaignListRequest : IRequest<GetCampaignListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
public class GetCampaignListHandler : IRequestHandler<GetCampaignListRequest, GetCampaignListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetCampaignListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCampaignListResult> Handle(GetCampaignListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Campaign
|
||||
.AsNoTracking()
|
||||
.IsDeletedEqualTo(request.IsDeleted)
|
||||
.Include(x => x.SalesTeam)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetCampaignListDto>>(entities);
|
||||
|
||||
return new GetCampaignListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.CampaignManager.Queries;
|
||||
|
||||
public class GetCampaignSingleProfile : Profile
|
||||
{
|
||||
public GetCampaignSingleProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCampaignSingleResult
|
||||
{
|
||||
public Campaign? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCampaignSingleRequest : IRequest<GetCampaignSingleResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
}
|
||||
|
||||
public class GetCampaignSingleValidator : AbstractValidator<GetCampaignSingleRequest>
|
||||
{
|
||||
public GetCampaignSingleValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCampaignSingleHandler : IRequestHandler<GetCampaignSingleRequest, GetCampaignSingleResult>
|
||||
{
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetCampaignSingleHandler(
|
||||
IQueryContext context
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCampaignSingleResult> Handle(GetCampaignSingleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Campaign
|
||||
.AsNoTracking()
|
||||
.Include(x => x.CampaignBudgetList.Where(budget => !budget.IsDeleted))
|
||||
.Include(x => x.CampaignExpenseList.Where(expense => !expense.IsDeleted))
|
||||
.Include(x => x.CampaignLeadList.Where(lead => !lead.IsDeleted))
|
||||
.Where(x => x.Id == request.Id)
|
||||
.AsQueryable();
|
||||
|
||||
var entity = await query.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
return new GetCampaignSingleResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Enums;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CampaignManager.Queries;
|
||||
|
||||
public record GetCampaignStatusListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
}
|
||||
|
||||
public class GetCampaignStatusListProfile : Profile
|
||||
{
|
||||
public GetCampaignStatusListProfile()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCampaignStatusListResult
|
||||
{
|
||||
public List<GetCampaignStatusListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCampaignStatusListRequest : IRequest<GetCampaignStatusListResult>
|
||||
{
|
||||
}
|
||||
|
||||
public class GetCampaignStatusListHandler : IRequestHandler<GetCampaignStatusListRequest, GetCampaignStatusListResult>
|
||||
{
|
||||
public GetCampaignStatusListHandler()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<GetCampaignStatusListResult> Handle(GetCampaignStatusListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var statuses = Enum.GetValues(typeof(CampaignStatus))
|
||||
.Cast<CampaignStatus>()
|
||||
.Select(status => new GetCampaignStatusListDto
|
||||
{
|
||||
Id = ((int)status).ToString(),
|
||||
Name = status.ToFriendlyName()
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await Task.CompletedTask;
|
||||
|
||||
return new GetCampaignStatusListResult
|
||||
{
|
||||
Data = statuses
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user