From 247bfba368a5746e122b364dce92f166ea54d0fd Mon Sep 17 00:00:00 2001 From: lumijiez <59575049+lumijiez@users.noreply.github.com> Date: Sun, 4 May 2025 15:40:57 +0300 Subject: [PATCH] Remove AutoMapper for now --- .../Common/Behaviors/LoggingBehavior.cs | 35 --------- .../Common/Behaviors/ValidationBehavior.cs | 31 -------- .../Mappings/ApplicationMappingProfile.cs | 72 ------------------- .../Common/Models/ApiResponse.cs | 43 ----------- src/Printbase.WebApi/Program.cs | 5 -- 5 files changed, 186 deletions(-) delete mode 100644 src/Printbase.Application/Common/Behaviors/LoggingBehavior.cs delete mode 100644 src/Printbase.Application/Common/Behaviors/ValidationBehavior.cs delete mode 100644 src/Printbase.Application/Common/Mappings/ApplicationMappingProfile.cs delete mode 100644 src/Printbase.Application/Common/Models/ApiResponse.cs diff --git a/src/Printbase.Application/Common/Behaviors/LoggingBehavior.cs b/src/Printbase.Application/Common/Behaviors/LoggingBehavior.cs deleted file mode 100644 index 16017eb..0000000 --- a/src/Printbase.Application/Common/Behaviors/LoggingBehavior.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Diagnostics; -using MediatR; -using Microsoft.Extensions.Logging; - -namespace Printbase.Application.Common.Behaviors; - -public class LoggingBehavior(ILogger> logger) - : IPipelineBehavior - where TRequest : IRequest -{ - public async Task Handle(TRequest request, RequestHandlerDelegate 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; - } - } -} \ No newline at end of file diff --git a/src/Printbase.Application/Common/Behaviors/ValidationBehavior.cs b/src/Printbase.Application/Common/Behaviors/ValidationBehavior.cs deleted file mode 100644 index d152073..0000000 --- a/src/Printbase.Application/Common/Behaviors/ValidationBehavior.cs +++ /dev/null @@ -1,31 +0,0 @@ -using FluentValidation; -using MediatR; -using ValidationException = System.ComponentModel.DataAnnotations.ValidationException; - -namespace Printbase.Application.Common.Behaviors; - -public class ValidationBehavior(IEnumerable> validators) - : IPipelineBehavior - where TRequest : IRequest -{ - public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) - { - if (!validators.Any()) - return await next(cancellationToken); - - var context = new ValidationContext(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); - } -} \ No newline at end of file diff --git a/src/Printbase.Application/Common/Mappings/ApplicationMappingProfile.cs b/src/Printbase.Application/Common/Mappings/ApplicationMappingProfile.cs deleted file mode 100644 index aaa2d2c..0000000 --- a/src/Printbase.Application/Common/Mappings/ApplicationMappingProfile.cs +++ /dev/null @@ -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() - .ForMember(dest => dest.TypeName, opt => opt.Ignore()) - .ForMember(dest => dest.Variants, opt => opt.MapFrom(src => src.Variants)); - - CreateMap() - .ConstructUsing((src, ctx) => new Product( - Guid.NewGuid(), - src.Name, - src.TypeId, - src.Description, - src.Discount)) - .ForMember(dest => dest.Variants, opt => opt.Ignore()); - - CreateMap() - .ForMember(dest => dest.Id, opt => opt.Ignore()) - .ForMember(dest => dest.Variants, opt => opt.Ignore()); - - CreateMap() - .ForMember(dest => dest.EffectivePrice, opt => opt.MapFrom(src => src.GetEffectivePrice(src.Discount))); - - CreateMap() - .ConstructUsing((src, ctx) => new ProductVariant( - Guid.NewGuid(), - Guid.Empty, - src.Price, - src.Color, - src.Size, - src.Discount, - src.Stock)); - - CreateMap() - .ForMember(dest => dest.ProductId, opt => opt.Ignore()); - - CreateMap() - .ForMember(dest => dest.GroupName, opt => opt.Ignore()); - - CreateMap() - .ConstructUsing((src, ctx) => new Domain.Entities.Products.ProductType( - Guid.NewGuid(), - src.Name, - src.GroupId, - src.Description)); - - CreateMap() - .ForMember(dest => dest.Id, opt => opt.Ignore()) - .ForMember(dest => dest.GroupId, opt => opt.Ignore()); - - CreateMap(); - - CreateMap() - .ConstructUsing((src, ctx) => new Domain.Entities.Products.ProductGroup( - Guid.NewGuid(), - src.Name, - src.Description)); - - CreateMap() - .ForMember(dest => dest.Id, opt => opt.Ignore()) - .ForMember(dest => dest.Types, opt => opt.Ignore()); - } -} \ No newline at end of file diff --git a/src/Printbase.Application/Common/Models/ApiResponse.cs b/src/Printbase.Application/Common/Models/ApiResponse.cs deleted file mode 100644 index d3fd7cc..0000000 --- a/src/Printbase.Application/Common/Models/ApiResponse.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Net; - -namespace Printbase.Application.Common.Models; - -public class ApiResponse -{ - public bool Succeeded { get; protected set; } - public string Message { get; protected set; } = string.Empty; - public T? Data { get; protected set; } - public IList Errors { get; protected set; } = new List(); - public HttpStatusCode StatusCode { get; protected set; } - - public static ApiResponse Success(T data, HttpStatusCode statusCode = HttpStatusCode.OK, string message = "") - { - return new ApiResponse - { - Succeeded = true, - Data = data, - StatusCode = statusCode, - Message = message - }; - } - - public static ApiResponse Fail(string error, HttpStatusCode statusCode = HttpStatusCode.BadRequest) - { - return new ApiResponse - { - Succeeded = false, - Errors = new List { error }, - StatusCode = statusCode - }; - } - - public static ApiResponse Fail(IList errors, HttpStatusCode statusCode = HttpStatusCode.BadRequest) - { - return new ApiResponse - { - Succeeded = false, - Errors = errors, - StatusCode = statusCode - }; - } -} \ No newline at end of file diff --git a/src/Printbase.WebApi/Program.cs b/src/Printbase.WebApi/Program.cs index 124f84f..2690657 100644 --- a/src/Printbase.WebApi/Program.cs +++ b/src/Printbase.WebApi/Program.cs @@ -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(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(); services.AddScoped();