initial commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public class CreateCustomerContactRequest
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? CustomerId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateCustomerContactResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record CreateCustomerContactCommand(CreateCustomerContactRequest Data) : IRequest<CreateCustomerContactResponse>;
|
||||
|
||||
public class CreateCustomerContactHandler : IRequestHandler<CreateCustomerContactCommand, CreateCustomerContactResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public CreateCustomerContactHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateCustomerContactResponse> Handle(CreateCustomerContactCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.CustomerContact);
|
||||
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.CustomerContact
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
Name = request.Data.Name,
|
||||
JobTitle = request.Data.JobTitle,
|
||||
PhoneNumber = request.Data.PhoneNumber,
|
||||
EmailAddress = request.Data.EmailAddress,
|
||||
Description = request.Data.Description,
|
||||
CustomerId = request.Data.CustomerId
|
||||
};
|
||||
|
||||
_context.CustomerContact.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateCustomerContactResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public class CreateCustomerContactValidator : AbstractValidator<CreateCustomerContactRequest>
|
||||
{
|
||||
public CreateCustomerContactValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Contact Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CustomerId)
|
||||
.NotEmpty().WithMessage("Customer 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.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public record DeleteCustomerContactByIdRequest(string Id);
|
||||
|
||||
public record DeleteCustomerContactByIdCommand(DeleteCustomerContactByIdRequest Data) : IRequest<bool>;
|
||||
|
||||
public class DeleteCustomerContactByIdHandler : IRequestHandler<DeleteCustomerContactByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public DeleteCustomerContactByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteCustomerContactByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.CustomerContact
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.CustomerContact.Remove(entity);
|
||||
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public class GetCustomerContactByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? CustomerId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record GetCustomerContactByIdQuery(string Id) : IRequest<GetCustomerContactByIdResponse?>;
|
||||
|
||||
public class GetCustomerContactByIdHandler : IRequestHandler<GetCustomerContactByIdQuery, GetCustomerContactByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCustomerContactByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetCustomerContactByIdResponse?> Handle(GetCustomerContactByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.CustomerContact
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetCustomerContactByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Name = x.Name,
|
||||
JobTitle = x.JobTitle,
|
||||
PhoneNumber = x.PhoneNumber,
|
||||
EmailAddress = x.EmailAddress,
|
||||
Description = x.Description,
|
||||
CustomerId = x.CustomerId,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public class GetCustomerContactListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? CustomerName { get; set; }
|
||||
}
|
||||
|
||||
public record GetCustomerContactListQuery() : IRequest<List<GetCustomerContactListResponse>>;
|
||||
|
||||
public class GetCustomerContactListHandler : IRequestHandler<GetCustomerContactListQuery, List<GetCustomerContactListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCustomerContactListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetCustomerContactListResponse>> Handle(GetCustomerContactListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.CustomerContact
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Customer)
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new GetCustomerContactListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
Name = x.Name,
|
||||
JobTitle = x.JobTitle,
|
||||
PhoneNumber = x.PhoneNumber,
|
||||
EmailAddress = x.EmailAddress,
|
||||
CustomerName = x.Customer != null ? x.Customer.Name : string.Empty
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public class LookupCustomerContactResponse
|
||||
{
|
||||
public List<LookupItem> Customers { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record LookupCustomerContactQuery() : IRequest<LookupCustomerContactResponse>;
|
||||
|
||||
public class LookupCustomerContactHandler : IRequestHandler<LookupCustomerContactQuery, LookupCustomerContactResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public LookupCustomerContactHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<LookupCustomerContactResponse> Handle(LookupCustomerContactQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new LookupCustomerContactResponse();
|
||||
|
||||
result.Customers = await _context.Customer
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Indotalent.ConfigBackEnd.Exceptions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public class UpdateCustomerContactRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? CustomerId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCustomerContactResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public bool Success { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateCustomerContactCommand(UpdateCustomerContactRequest Data) : IRequest<UpdateCustomerContactResponse>;
|
||||
|
||||
public class UpdateCustomerContactHandler : IRequestHandler<UpdateCustomerContactCommand, UpdateCustomerContactResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateCustomerContactHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateCustomerContactResponse> Handle(UpdateCustomerContactCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.CustomerContact
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return new UpdateCustomerContactResponse { Id = request.Data.Id, Success = false };
|
||||
}
|
||||
|
||||
entity.Name = request.Data.Name;
|
||||
entity.JobTitle = request.Data.JobTitle;
|
||||
entity.PhoneNumber = request.Data.PhoneNumber;
|
||||
entity.EmailAddress = request.Data.EmailAddress;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.CustomerId = request.Data.CustomerId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateCustomerContactResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
Success = true
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Thirdparty.CustomerContact.Cqrs;
|
||||
|
||||
public class UpdateCustomerContactValidator : AbstractValidator<UpdateCustomerContactRequest>
|
||||
{
|
||||
public UpdateCustomerContactValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("ID is required for update");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Contact Name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort);
|
||||
|
||||
RuleFor(x => x.CustomerId)
|
||||
.NotEmpty().WithMessage("Customer is required");
|
||||
|
||||
RuleFor(x => x.EmailAddress)
|
||||
.EmailAddress().When(x => !string.IsNullOrEmpty(x.EmailAddress));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user