initial commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public class CreateSalesRepresentativeRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? EmployeeNumber { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesTeamId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateSalesRepresentativeResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateSalesRepresentativeCommand(CreateSalesRepresentativeRequest Data) : IRequest<CreateSalesRepresentativeResponse>;
|
||||
|
||||
public class CreateSalesRepresentativeHandler : IRequestHandler<CreateSalesRepresentativeCommand, CreateSalesRepresentativeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateSalesRepresentativeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateSalesRepresentativeResponse> Handle(CreateSalesRepresentativeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.SalesRepresentative);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.SalesRepresentative
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Name = request.Data.Name,
|
||||
JobTitle = request.Data.JobTitle,
|
||||
EmployeeNumber = request.Data.EmployeeNumber,
|
||||
PhoneNumber = request.Data.PhoneNumber,
|
||||
EmailAddress = request.Data.EmailAddress,
|
||||
Description = request.Data.Description,
|
||||
SalesTeamId = request.Data.SalesTeamId
|
||||
};
|
||||
|
||||
_context.SalesRepresentative.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateSalesRepresentativeResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public class CreateSalesRepresentativeValidator : AbstractValidator<CreateSalesRepresentativeRequest>
|
||||
{
|
||||
public CreateSalesRepresentativeValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Representative Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.SalesTeamId)
|
||||
.NotEmpty().WithMessage("Sales Team is required");
|
||||
|
||||
RuleFor(x => x.EmailAddress)
|
||||
.EmailAddress().When(x => !string.IsNullOrEmpty(x.EmailAddress));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public record DeleteSalesRepresentativeByIdRequest(string Id);
|
||||
|
||||
public record DeleteSalesRepresentativeByIdCommand(DeleteSalesRepresentativeByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteSalesRepresentativeByIdHandler : IRequestHandler<DeleteSalesRepresentativeByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteSalesRepresentativeByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteSalesRepresentativeByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.SalesRepresentative
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.SalesRepresentative.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public class GetSalesRepresentativeByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? EmployeeNumber { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesTeamId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetSalesRepresentativeByIdQuery(string Id) : IRequest<GetSalesRepresentativeByIdResponse?>;
|
||||
|
||||
public class GetSalesRepresentativeByIdHandler : IRequestHandler<GetSalesRepresentativeByIdQuery, GetSalesRepresentativeByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetSalesRepresentativeByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetSalesRepresentativeByIdResponse?> Handle(GetSalesRepresentativeByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.SalesRepresentative
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetSalesRepresentativeByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Name = x.Name,
|
||||
JobTitle = x.JobTitle,
|
||||
EmployeeNumber = x.EmployeeNumber,
|
||||
PhoneNumber = x.PhoneNumber,
|
||||
EmailAddress = x.EmailAddress,
|
||||
Description = x.Description,
|
||||
SalesTeamId = x.SalesTeamId,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public class GetSalesRepresentativeListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? EmployeeNumber { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? SalesTeamName { get; set; }
|
||||
}
|
||||
|
||||
public record GetSalesRepresentativeListQuery() : IRequest<List<GetSalesRepresentativeListResponse>>;
|
||||
|
||||
public class GetSalesRepresentativeListHandler : IRequestHandler<GetSalesRepresentativeListQuery, List<GetSalesRepresentativeListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetSalesRepresentativeListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetSalesRepresentativeListResponse>> Handle(GetSalesRepresentativeListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.SalesRepresentative
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesTeam)
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetSalesRepresentativeListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Name = x.Name,
|
||||
JobTitle = x.JobTitle,
|
||||
EmployeeNumber = x.EmployeeNumber,
|
||||
PhoneNumber = x.PhoneNumber,
|
||||
EmailAddress = x.EmailAddress,
|
||||
SalesTeamName = x.SalesTeam != null ? x.SalesTeam.Name : string.Empty
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public class LookupSalesRepresentativeResponse
|
||||
{
|
||||
public List<LookupItem> SalesTeams { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record LookupSalesRepresentativeQuery() : IRequest<LookupSalesRepresentativeResponse>;
|
||||
|
||||
public class LookupSalesRepresentativeHandler : IRequestHandler<LookupSalesRepresentativeQuery, LookupSalesRepresentativeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public LookupSalesRepresentativeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<LookupSalesRepresentativeResponse> Handle(LookupSalesRepresentativeQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new LookupSalesRepresentativeResponse();
|
||||
|
||||
result.SalesTeams = await _context.SalesTeam
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public class UpdateSalesRepresentativeRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? EmployeeNumber { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesTeamId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateSalesRepresentativeResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateSalesRepresentativeCommand(UpdateSalesRepresentativeRequest Data) : IRequest<UpdateSalesRepresentativeResponse>;
|
||||
|
||||
public class UpdateSalesRepresentativeHandler : IRequestHandler<UpdateSalesRepresentativeCommand, UpdateSalesRepresentativeResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateSalesRepresentativeHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateSalesRepresentativeResponse> Handle(UpdateSalesRepresentativeCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.SalesRepresentative
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateSalesRepresentativeResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.JobTitle = request.Data.JobTitle;
|
||||
entity.EmployeeNumber = request.Data.EmployeeNumber;
|
||||
entity.PhoneNumber = request.Data.PhoneNumber;
|
||||
entity.EmailAddress = request.Data.EmailAddress;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.SalesTeamId = request.Data.SalesTeamId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateSalesRepresentativeResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Pipeline.SalesRepresentative.Cqrs;
|
||||
|
||||
public class UpdateSalesRepresentativeValidator : AbstractValidator<UpdateSalesRepresentativeRequest>
|
||||
{
|
||||
public UpdateSalesRepresentativeValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Representative Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.SalesTeamId)
|
||||
.NotEmpty().WithMessage("Sales Team is required");
|
||||
|
||||
RuleFor(x => x.EmailAddress)
|
||||
.EmailAddress().When(x => !string.IsNullOrEmpty(x.EmailAddress));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user