initial commit
This commit is contained in:
+78
@@ -0,0 +1,78 @@
|
||||
using Application.Common.Repositories;
|
||||
using Application.Features.NumberSequenceManager;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.SalesRepresentativeManager.Commands;
|
||||
|
||||
public class CreateSalesRepresentativeResult
|
||||
{
|
||||
public SalesRepresentative? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateSalesRepresentativeRequest : IRequest<CreateSalesRepresentativeResult>
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public string? JobTitle { get; init; }
|
||||
public string? EmployeeNumber { get; init; }
|
||||
public string? PhoneNumber { get; init; }
|
||||
public string? EmailAddress { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? SalesTeamId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateSalesRepresentativeValidator : AbstractValidator<CreateSalesRepresentativeRequest>
|
||||
{
|
||||
public CreateSalesRepresentativeValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.JobTitle).NotEmpty();
|
||||
RuleFor(x => x.PhoneNumber).NotEmpty();
|
||||
RuleFor(x => x.EmailAddress).NotEmpty();
|
||||
RuleFor(x => x.SalesTeamId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateSalesRepresentativeHandler : IRequestHandler<CreateSalesRepresentativeRequest, CreateSalesRepresentativeResult>
|
||||
{
|
||||
private readonly ICommandRepository<SalesRepresentative> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly NumberSequenceService _numberSequenceService;
|
||||
|
||||
public CreateSalesRepresentativeHandler(
|
||||
ICommandRepository<SalesRepresentative> repository,
|
||||
IUnitOfWork unitOfWork,
|
||||
NumberSequenceService numberSequenceService
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
_numberSequenceService = numberSequenceService;
|
||||
}
|
||||
|
||||
public async Task<CreateSalesRepresentativeResult> Handle(CreateSalesRepresentativeRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new SalesRepresentative
|
||||
{
|
||||
CreatedById = request.CreatedById,
|
||||
Number = _numberSequenceService.GenerateNumber(nameof(SalesRepresentative), "", "SR"),
|
||||
Name = request.Name,
|
||||
JobTitle = request.JobTitle,
|
||||
EmployeeNumber = request.EmployeeNumber,
|
||||
PhoneNumber = request.PhoneNumber,
|
||||
EmailAddress = request.EmailAddress,
|
||||
Description = request.Description,
|
||||
SalesTeamId = request.SalesTeamId
|
||||
};
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateSalesRepresentativeResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.SalesRepresentativeManager.Commands;
|
||||
|
||||
public class DeleteSalesRepresentativeResult
|
||||
{
|
||||
public SalesRepresentative? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteSalesRepresentativeRequest : IRequest<DeleteSalesRepresentativeResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteSalesRepresentativeValidator : AbstractValidator<DeleteSalesRepresentativeRequest>
|
||||
{
|
||||
public DeleteSalesRepresentativeValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteSalesRepresentativeHandler : IRequestHandler<DeleteSalesRepresentativeRequest, DeleteSalesRepresentativeResult>
|
||||
{
|
||||
private readonly ICommandRepository<SalesRepresentative> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteSalesRepresentativeHandler(
|
||||
ICommandRepository<SalesRepresentative> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteSalesRepresentativeResult> Handle(DeleteSalesRepresentativeRequest 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 DeleteSalesRepresentativeResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.SalesRepresentativeManager.Commands;
|
||||
|
||||
public class UpdateSalesRepresentativeResult
|
||||
{
|
||||
public SalesRepresentative? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateSalesRepresentativeRequest : IRequest<UpdateSalesRepresentativeResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? JobTitle { get; init; }
|
||||
public string? EmployeeNumber { get; init; }
|
||||
public string? PhoneNumber { get; init; }
|
||||
public string? EmailAddress { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? SalesTeamId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateSalesRepresentativeValidator : AbstractValidator<UpdateSalesRepresentativeRequest>
|
||||
{
|
||||
public UpdateSalesRepresentativeValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.JobTitle).NotEmpty();
|
||||
RuleFor(x => x.PhoneNumber).NotEmpty();
|
||||
RuleFor(x => x.EmailAddress).NotEmpty();
|
||||
RuleFor(x => x.SalesTeamId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateSalesRepresentativeHandler : IRequestHandler<UpdateSalesRepresentativeRequest, UpdateSalesRepresentativeResult>
|
||||
{
|
||||
private readonly ICommandRepository<SalesRepresentative> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateSalesRepresentativeHandler(
|
||||
ICommandRepository<SalesRepresentative> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateSalesRepresentativeResult> Handle(UpdateSalesRepresentativeRequest 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.Name = request.Name;
|
||||
entity.JobTitle = request.JobTitle;
|
||||
entity.EmployeeNumber = request.EmployeeNumber;
|
||||
entity.PhoneNumber = request.PhoneNumber;
|
||||
entity.EmailAddress = request.EmailAddress;
|
||||
entity.Description = request.Description;
|
||||
entity.SalesTeamId = request.SalesTeamId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateSalesRepresentativeResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user