Remove AutoMapper for now
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
using System.Diagnostics;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Printbase.Application.Common.Behaviors;
|
||||
|
||||
public class LoggingBehavior<TRequest, TResponse>(ILogger<LoggingBehavior<TRequest, TResponse>> logger)
|
||||
: IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
|
||||
{
|
||||
var requestName = typeof(TRequest).Name;
|
||||
|
||||
logger.LogInformation($"Handling {requestName}");
|
||||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
|
||||
try
|
||||
{
|
||||
var response = await next(cancellationToken);
|
||||
stopwatch.Stop();
|
||||
|
||||
logger.LogInformation($"Handling {requestName} in {stopwatch.ElapsedMilliseconds}ms");
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
stopwatch.Stop();
|
||||
logger.LogError(ex, $"Error handling {requestName} after {stopwatch.ElapsedMilliseconds}ms");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using ValidationException = System.ComponentModel.DataAnnotations.ValidationException;
|
||||
|
||||
namespace Printbase.Application.Common.Behaviors;
|
||||
|
||||
public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators)
|
||||
: IPipelineBehavior<TRequest, TResponse>
|
||||
where TRequest : IRequest<TResponse>
|
||||
{
|
||||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!validators.Any())
|
||||
return await next(cancellationToken);
|
||||
|
||||
var context = new ValidationContext<TRequest>(request);
|
||||
|
||||
var validationResults = await Task.WhenAll(
|
||||
validators.Select(v => v.ValidateAsync(context, cancellationToken)));
|
||||
|
||||
var failures = validationResults
|
||||
.SelectMany(r => r.Errors)
|
||||
.Where(f => f != null)
|
||||
.ToList();
|
||||
|
||||
if (failures.Count != 0)
|
||||
throw new ValidationException(failures.ToString());
|
||||
|
||||
return await next(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using AutoMapper;
|
||||
using Printbase.Application.ProductGroup;
|
||||
using Printbase.Application.Products;
|
||||
using Printbase.Application.ProductType;
|
||||
using Printbase.Domain.Entities.Products;
|
||||
|
||||
namespace Printbase.Application.Common.Mappings;
|
||||
|
||||
public class ApplicationMappingProfile : Profile
|
||||
{
|
||||
public ApplicationMappingProfile()
|
||||
{
|
||||
CreateMap<Product, ProductDto>()
|
||||
.ForMember(dest => dest.TypeName, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.Variants, opt => opt.MapFrom(src => src.Variants));
|
||||
|
||||
CreateMap<ProductCreateDto, Product>()
|
||||
.ConstructUsing((src, ctx) => new Product(
|
||||
Guid.NewGuid(),
|
||||
src.Name,
|
||||
src.TypeId,
|
||||
src.Description,
|
||||
src.Discount))
|
||||
.ForMember(dest => dest.Variants, opt => opt.Ignore());
|
||||
|
||||
CreateMap<ProductUpdateDto, Product>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.Variants, opt => opt.Ignore());
|
||||
|
||||
CreateMap<ProductVariant, ProductVariantDto>()
|
||||
.ForMember(dest => dest.EffectivePrice, opt => opt.MapFrom(src => src.GetEffectivePrice(src.Discount)));
|
||||
|
||||
CreateMap<ProductVariantCreateDto, ProductVariant>()
|
||||
.ConstructUsing((src, ctx) => new ProductVariant(
|
||||
Guid.NewGuid(),
|
||||
Guid.Empty,
|
||||
src.Price,
|
||||
src.Color,
|
||||
src.Size,
|
||||
src.Discount,
|
||||
src.Stock));
|
||||
|
||||
CreateMap<ProductVariantUpdateDto, ProductVariant>()
|
||||
.ForMember(dest => dest.ProductId, opt => opt.Ignore());
|
||||
|
||||
CreateMap<Domain.Entities.Products.ProductType, ProductTypeDto>()
|
||||
.ForMember(dest => dest.GroupName, opt => opt.Ignore());
|
||||
|
||||
CreateMap<ProductTypeCreateDto, Domain.Entities.Products.ProductType>()
|
||||
.ConstructUsing((src, ctx) => new Domain.Entities.Products.ProductType(
|
||||
Guid.NewGuid(),
|
||||
src.Name,
|
||||
src.GroupId,
|
||||
src.Description));
|
||||
|
||||
CreateMap<ProductTypeUpdateDto, Domain.Entities.Products.ProductType>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.GroupId, opt => opt.Ignore());
|
||||
|
||||
CreateMap<Domain.Entities.Products.ProductGroup, ProductGroupDto>();
|
||||
|
||||
CreateMap<ProductGroupCreateDto, Domain.Entities.Products.ProductGroup>()
|
||||
.ConstructUsing((src, ctx) => new Domain.Entities.Products.ProductGroup(
|
||||
Guid.NewGuid(),
|
||||
src.Name,
|
||||
src.Description));
|
||||
|
||||
CreateMap<ProductGroupUpdateDto, Domain.Entities.Products.ProductGroup>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.Types, opt => opt.Ignore());
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using System.Net;
|
||||
|
||||
namespace Printbase.Application.Common.Models;
|
||||
|
||||
public class ApiResponse<T>
|
||||
{
|
||||
public bool Succeeded { get; protected set; }
|
||||
public string Message { get; protected set; } = string.Empty;
|
||||
public T? Data { get; protected set; }
|
||||
public IList<string> Errors { get; protected set; } = new List<string>();
|
||||
public HttpStatusCode StatusCode { get; protected set; }
|
||||
|
||||
public static ApiResponse<T> Success(T data, HttpStatusCode statusCode = HttpStatusCode.OK, string message = "")
|
||||
{
|
||||
return new ApiResponse<T>
|
||||
{
|
||||
Succeeded = true,
|
||||
Data = data,
|
||||
StatusCode = statusCode,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
public static ApiResponse<T> Fail(string error, HttpStatusCode statusCode = HttpStatusCode.BadRequest)
|
||||
{
|
||||
return new ApiResponse<T>
|
||||
{
|
||||
Succeeded = false,
|
||||
Errors = new List<string> { error },
|
||||
StatusCode = statusCode
|
||||
};
|
||||
}
|
||||
|
||||
public static ApiResponse<T> Fail(IList<string> errors, HttpStatusCode statusCode = HttpStatusCode.BadRequest)
|
||||
{
|
||||
return new ApiResponse<T>
|
||||
{
|
||||
Succeeded = false,
|
||||
Errors = errors,
|
||||
StatusCode = statusCode
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
using System.Reflection;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Printbase.Application.Common.Behaviors;
|
||||
using Printbase.Domain.Repositories;
|
||||
using Printbase.Infrastructure.Database;
|
||||
using Printbase.Infrastructure.Mapping;
|
||||
using Printbase.Infrastructure.Repositories;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -16,11 +14,8 @@ services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseSqlServer(
|
||||
configuration.GetConnectionString("DefaultConnection"),
|
||||
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));
|
||||
services.AddAutoMapper(typeof(ProductMappingProfile).Assembly);
|
||||
|
||||
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
|
||||
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
|
||||
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
|
||||
|
||||
services.AddScoped<IProductRepository, ProductRepository>();
|
||||
services.AddScoped<IProductVariantRepository, ProductVariantRepository>();
|
||||
|
||||
Reference in New Issue
Block a user