initial commit
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.CompanyManager.Commands;
|
||||
|
||||
public class UpdateCompanyResult
|
||||
{
|
||||
public Company? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateCompanyRequest : IRequest<UpdateCompanyResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? Currency { get; init; }
|
||||
public string? Street { get; init; }
|
||||
public string? City { get; init; }
|
||||
public string? State { get; init; }
|
||||
public string? ZipCode { get; init; }
|
||||
public string? Country { get; init; }
|
||||
public string? PhoneNumber { get; init; }
|
||||
public string? FaxNumber { get; init; }
|
||||
public string? EmailAddress { get; init; }
|
||||
public string? Website { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateCompanyValidator : AbstractValidator<UpdateCompanyRequest>
|
||||
{
|
||||
public UpdateCompanyValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.Currency).NotEmpty();
|
||||
RuleFor(x => x.Street).NotEmpty();
|
||||
RuleFor(x => x.City).NotEmpty();
|
||||
RuleFor(x => x.State).NotEmpty();
|
||||
RuleFor(x => x.ZipCode).NotEmpty();
|
||||
RuleFor(x => x.PhoneNumber).NotEmpty();
|
||||
RuleFor(x => x.EmailAddress).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateCompanyHandler : IRequestHandler<UpdateCompanyRequest, UpdateCompanyResult>
|
||||
{
|
||||
private readonly ICommandRepository<Company> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateCompanyHandler(
|
||||
ICommandRepository<Company> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateCompanyResult> Handle(UpdateCompanyRequest 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.Description = request.Description;
|
||||
entity.Currency = request.Currency;
|
||||
entity.Street = request.Street;
|
||||
entity.City = request.City;
|
||||
entity.State = request.State;
|
||||
entity.ZipCode = request.ZipCode;
|
||||
entity.Country = request.Country;
|
||||
entity.PhoneNumber = request.PhoneNumber;
|
||||
entity.FaxNumber = request.FaxNumber;
|
||||
entity.EmailAddress = request.EmailAddress;
|
||||
entity.Website = request.Website;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateCompanyResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.CompanyManager.Queries;
|
||||
|
||||
public record GetCompanyListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? Currency { get; init; }
|
||||
public string? Street { get; init; }
|
||||
public string? City { get; init; }
|
||||
public string? State { get; init; }
|
||||
public string? ZipCode { get; init; }
|
||||
public string? Country { get; init; }
|
||||
public string? PhoneNumber { get; init; }
|
||||
public string? FaxNumber { get; init; }
|
||||
public string? EmailAddress { get; init; }
|
||||
public string? Website { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetCompanyListProfile : Profile
|
||||
{
|
||||
public GetCompanyListProfile()
|
||||
{
|
||||
CreateMap<Company, GetCompanyListDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCompanyListResult
|
||||
{
|
||||
public List<GetCompanyListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCompanyListRequest : IRequest<GetCompanyListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetCompanyListHandler : IRequestHandler<GetCompanyListRequest, GetCompanyListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetCompanyListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCompanyListResult> Handle(GetCompanyListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Company
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetCompanyListDto>>(entities);
|
||||
|
||||
return new GetCompanyListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.CompanyManager.Queries;
|
||||
|
||||
public record GetCompanySingleDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? Currency { get; init; }
|
||||
public string? Street { get; init; }
|
||||
public string? City { get; init; }
|
||||
public string? State { get; init; }
|
||||
public string? ZipCode { get; init; }
|
||||
public string? Country { get; init; }
|
||||
public string? PhoneNumber { get; init; }
|
||||
public string? FaxNumber { get; init; }
|
||||
public string? EmailAddress { get; init; }
|
||||
public string? Website { get; init; }
|
||||
}
|
||||
|
||||
public class GetCompanySingleProfile : Profile
|
||||
{
|
||||
public GetCompanySingleProfile()
|
||||
{
|
||||
CreateMap<Company, GetCompanySingleDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCompanySingleResult
|
||||
{
|
||||
public GetCompanySingleDto? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetCompanySingleRequest : IRequest<GetCompanySingleResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
}
|
||||
|
||||
public class GetCompanySingleValidator : AbstractValidator<GetCompanySingleRequest>
|
||||
{
|
||||
public GetCompanySingleValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetCompanySingleHandler : IRequestHandler<GetCompanySingleRequest, GetCompanySingleResult>
|
||||
{
|
||||
private readonly IQueryContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetCompanySingleHandler(
|
||||
IQueryContext context,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<GetCompanySingleResult> Handle(GetCompanySingleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Company
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
query = query
|
||||
.Where(x => x.Id == request.Id);
|
||||
|
||||
var entity = await query.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
var dto = _mapper.Map<GetCompanySingleDto>(entity);
|
||||
|
||||
return new GetCompanySingleResult
|
||||
{
|
||||
Data = dto
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user