Add MassData API with CQRS, repository, and DbContext
Introduce MassData feature with new API endpoints for querying and upserting records by customer name. Add DTOs, AutoMapper profile, MediatR CQRS handlers, repository pattern, and MassDataDbContext. Register new services in DI and add MassDataConnection to configuration. Upsert uses stored procedure. Enables full CRUD for Massdata via dedicated API.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.MassData.Commands;
|
||||
|
||||
public record UpsertMassDataByCustomerNameCommand(MassDataWriteDto Dto) : IRequest<MassDataReadDto>;
|
||||
@@ -0,0 +1,24 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Application.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.MassData.Commands;
|
||||
|
||||
public class UpsertMassDataByCustomerNameHandler : IRequestHandler<UpsertMassDataByCustomerNameCommand, MassDataReadDto>
|
||||
{
|
||||
private readonly IMassDataRepository _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public UpsertMassDataByCustomerNameHandler(IMassDataRepository repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<MassDataReadDto> Handle(UpsertMassDataByCustomerNameCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var dto = request.Dto;
|
||||
var updated = await _repository.UpsertByCustomerNameAsync(dto.CustomerName, dto.Amount, dto.StatusFlag, dto.Category, cancellationToken);
|
||||
return _mapper.Map<MassDataReadDto>(updated);
|
||||
}
|
||||
}
|
||||
12
DbFirst.Application/MassData/MassDataProfile.cs
Normal file
12
DbFirst.Application/MassData/MassDataProfile.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.Entities;
|
||||
|
||||
namespace DbFirst.Application.MassData;
|
||||
|
||||
public class MassDataProfile : Profile
|
||||
{
|
||||
public MassDataProfile()
|
||||
{
|
||||
CreateMap<Massdata, MassDataReadDto>();
|
||||
}
|
||||
}
|
||||
12
DbFirst.Application/MassData/MassDataReadDto.cs
Normal file
12
DbFirst.Application/MassData/MassDataReadDto.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace DbFirst.Application.MassData;
|
||||
|
||||
public class MassDataReadDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string CustomerName { get; set; } = string.Empty;
|
||||
public decimal Amount { get; set; }
|
||||
public string Category { get; set; } = string.Empty;
|
||||
public bool StatusFlag { get; set; }
|
||||
public DateTime AddedWhen { get; set; }
|
||||
public DateTime? ChangedWhen { get; set; }
|
||||
}
|
||||
9
DbFirst.Application/MassData/MassDataWriteDto.cs
Normal file
9
DbFirst.Application/MassData/MassDataWriteDto.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace DbFirst.Application.MassData;
|
||||
|
||||
public class MassDataWriteDto
|
||||
{
|
||||
public string CustomerName { get; set; } = string.Empty;
|
||||
public decimal Amount { get; set; }
|
||||
public string Category { get; set; } = string.Empty;
|
||||
public bool StatusFlag { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Application.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.MassData.Queries;
|
||||
|
||||
public class GetAllMassDataHandler : IRequestHandler<GetAllMassDataQuery, List<MassDataReadDto>>
|
||||
{
|
||||
private readonly IMassDataRepository _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetAllMassDataHandler(IMassDataRepository repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<List<MassDataReadDto>> Handle(GetAllMassDataQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await _repository.GetAllAsync(request.Skip, request.Take, cancellationToken);
|
||||
return _mapper.Map<List<MassDataReadDto>>(items);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.MassData.Queries;
|
||||
|
||||
public record GetAllMassDataQuery(int? Skip, int? Take) : IRequest<List<MassDataReadDto>>;
|
||||
@@ -0,0 +1,23 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Application.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.MassData.Queries;
|
||||
|
||||
public class GetMassDataByCustomerNameHandler : IRequestHandler<GetMassDataByCustomerNameQuery, MassDataReadDto?>
|
||||
{
|
||||
private readonly IMassDataRepository _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetMassDataByCustomerNameHandler(IMassDataRepository repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<MassDataReadDto?> Handle(GetMassDataByCustomerNameQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var item = await _repository.GetByCustomerNameAsync(request.CustomerName, cancellationToken);
|
||||
return item == null ? null : _mapper.Map<MassDataReadDto>(item);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.MassData.Queries;
|
||||
|
||||
public record GetMassDataByCustomerNameQuery(string CustomerName) : IRequest<MassDataReadDto?>;
|
||||
10
DbFirst.Application/Repositories/IMassDataRepository.cs
Normal file
10
DbFirst.Application/Repositories/IMassDataRepository.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using DbFirst.Domain.Entities;
|
||||
|
||||
namespace DbFirst.Application.Repositories;
|
||||
|
||||
public interface IMassDataRepository
|
||||
{
|
||||
Task<List<Massdata>> GetAllAsync(int? skip = null, int? take = null, CancellationToken cancellationToken = default);
|
||||
Task<Massdata?> GetByCustomerNameAsync(string customerName, CancellationToken cancellationToken = default);
|
||||
Task<Massdata> UpsertByCustomerNameAsync(string customerName, decimal amount, bool statusFlag, string category, CancellationToken cancellationToken = default);
|
||||
}
|
||||
Reference in New Issue
Block a user