play with storing valueobjects

This commit is contained in:
Claudiu Farcas
2022-10-18 00:03:51 +03:00
parent 80ccd22d9c
commit b31cbcc15d
36 changed files with 250 additions and 81 deletions
@@ -3,7 +3,10 @@ using MapsterMapper;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using PracticeCalendar.Application.PracticeEvents.Queries;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Application.Products.Queries;
using PracticeCalendar.Domain.Entities.PracticeEvent;
using PracticeCalendar.Domain.Entities.Product;
using PracticeCalendar.Domain.ValueObjects;
using System.Reflection;
namespace PracticeCalendar.Application
@@ -23,11 +26,35 @@ namespace PracticeCalendar.Application
TypeAdapterConfig.GlobalSettings.Default.MapToConstructor(true);
TypeAdapterConfig.GlobalSettings.NewConfig<PracticeEventDto, PracticeEvent>()
.ConstructUsing(src => new PracticeEvent(src.Title, src.Description, src.StartTime, src.EndTime));
var mapsterConfig = new TypeAdapterConfig();
mapsterConfig.NewConfig<PracticeEventDto, PracticeEvent>()
.MapToConstructor(true)
.ConstructUsing(src => new PracticeEvent(src.Title, src.Description, src.StartTime, src.EndTime));
mapsterConfig.NewConfig<ProductDto, Product>()
.MapToConstructor(true)
.ConstructUsing(src => new Product()
{
Id = src.Id,
Category = src.Category,
Name = src.Name,
UnitPrice = new Price
{
Value = src.UnitPrice,
Currency = src.UnitPriceCurrency
}
});
mapsterConfig
.ForType<Product, ProductDto>()
.MapWith(src => new ProductDto
{
Id = src.Id,
Name = src.Name,
Category = src.Category,
UnitPrice = src.UnitPrice.Value,
UnitPriceCurrency = src.UnitPrice.Currency
})
;
services.AddSingleton<IMapper>(new Mapper(mapsterConfig));
return services;
@@ -1,7 +1,7 @@
using MediatR;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Domain.Entities.Specifications;
using PracticeCalendar.Domain.Entities.PracticeEvent;
using PracticeCalendar.Domain.Entities.PracticeEvent.Specifications;
using PracticeCalendar.Domain.Exceptions;
namespace PracticeCalendar.Application.PracticeEvents.Commands
@@ -29,7 +29,7 @@ namespace PracticeCalendar.Application.PracticeEvents.Commands
public async Task<Unit> Handle(AttendeeAcceptEventCommand request, CancellationToken cancellationToken)
{
var spec = new PracticeEventByIdWithAttendees(request.EventId);
var spec = new PracticeEventByIdWithAttendeesSpecification(request.EventId);
var practiceEvent = await eventsRepo.FirstOrDefaultAsync(spec, cancellationToken);
if (practiceEvent == null)
{
@@ -37,7 +37,7 @@ namespace PracticeCalendar.Application.PracticeEvents.Commands
}
practiceEvent.AttendeeAcceptEvent(request.AttendeeId);
await eventsRepo.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -1,7 +1,7 @@
using MediatR;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Domain.Entities.Specifications;
using PracticeCalendar.Domain.Entities.PracticeEvent;
using PracticeCalendar.Domain.Entities.PracticeEvent.Specifications;
using PracticeCalendar.Domain.Exceptions;
namespace PracticeCalendar.Application.PracticeEvents.Commands
@@ -29,14 +29,14 @@ namespace PracticeCalendar.Application.PracticeEvents.Commands
public async Task<Unit> Handle(AttendeeDeclineEventCommand request, CancellationToken cancellationToken)
{
var spec = new PracticeEventByIdWithAttendees(request.EventId);
var spec = new PracticeEventByIdWithAttendeesSpecification(request.EventId);
var practiceEvent = await eventsRepo.FirstOrDefaultAsync(spec, cancellationToken);
if (practiceEvent == null)
{
throw new PracticeEventNotFoundException();
}
practiceEvent.AttendeeDeclineEvent(request.AttendeeId);
return Unit.Value;
}
}
@@ -2,7 +2,7 @@
using MediatR;
using PracticeCalendar.Application.PracticeEvents.Queries;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Domain.Entities.PracticeEvent;
namespace PracticeCalendar.Application.PracticeEvents.Commands
{
@@ -1,6 +1,6 @@
using MediatR;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Domain.Entities.PracticeEvent;
using PracticeCalendar.Domain.Exceptions;
namespace PracticeCalendar.Application.PracticeEvents.Commands
@@ -32,7 +32,7 @@ namespace PracticeCalendar.Application.PracticeEvents.Commands
}
await eventsRepo.DeleteAsync(org, cancellationToken);
await eventsRepo.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -2,7 +2,7 @@
using MediatR;
using PracticeCalendar.Application.PracticeEvents.Queries;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Domain.Entities.PracticeEvent;
using PracticeCalendar.Domain.Exceptions;
namespace PracticeCalendar.Application.PracticeEvents.Commands
@@ -3,8 +3,8 @@ using MapsterMapper;
using MediatR;
using Microsoft.Extensions.Logging;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities;
using PracticeCalendar.Domain.Entities.Specifications;
using PracticeCalendar.Domain.Entities.PracticeEvent;
using PracticeCalendar.Domain.Entities.PracticeEvent.Specifications;
namespace PracticeCalendar.Application.PracticeEvents.Queries.GetPracticeEvents
{
@@ -29,7 +29,7 @@ namespace PracticeCalendar.Application.PracticeEvents.Queries.GetPracticeEvents
public async Task<List<PracticeEventDto>> Handle(GetPracticeEventsQuery request, CancellationToken cancellationToken)
{
var spec = new PracticeEventsWithAttendees();
var spec = new PracticeEventsWithAttendeesSpecification();
var evList = await eventsRepo.ListAsync(spec, cancellationToken);
var lst = evList.Adapt<List<PracticeEventDto>>(mapper.Config);
return lst;
@@ -0,0 +1,39 @@
using Mapster;
using MapsterMapper;
using MediatR;
using Microsoft.Extensions.Logging;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities.Product;
using PracticeCalendar.Domain.Entities.Product.Specifications;
namespace PracticeCalendar.Application.Products.Queries.GetProducts
{
public class GetProductsQuery : IRequest<List<ProductDto>>
{
}
public class GetProductsQueryHandler : IRequestHandler<GetProductsQuery, List<ProductDto>>
{
private readonly ILogger<GetProductsQueryHandler> logger;
private readonly IRepository<Product> eventsRepo;
private readonly IMapper mapper;
public GetProductsQueryHandler(IRepository<Product> eventsRepo,
ILogger<GetProductsQueryHandler> logger,
IMapper mapper)
{
this.eventsRepo = eventsRepo;
this.logger = logger;
this.mapper = mapper;
}
public async Task<List<ProductDto>> Handle(GetProductsQuery request, CancellationToken cancellationToken)
{
var spec = new AllProductsSpecification();
var evList = await eventsRepo.ListAsync(spec, cancellationToken);
var lst = evList.Adapt<List<ProductDto>>(mapper.Config);
return lst;
}
}
}
@@ -0,0 +1,13 @@
using PracticeCalendar.Domain.ValueObjects;
namespace PracticeCalendar.Application.Products.Queries
{
public class ProductDto
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Category { get; set; } = string.Empty;
public decimal UnitPrice { get; set; }
public string UnitPriceCurrency { get; set; } = Price.DEFAULT_CURRENCY;
}
}