Compare commits
1 Commits
Task_7_Loc
...
Task_8_Map
| Author | SHA1 | Date | |
|---|---|---|---|
| af440f46e0 |
@@ -17,6 +17,8 @@ internal class BuyerDataModel(string id, string fio, string phoneNumber, double
|
|||||||
|
|
||||||
public double DiscountSize { get; private set; } = discountSize;
|
public double DiscountSize { get; private set; } = discountSize;
|
||||||
|
|
||||||
|
public BuyerDataModel() : this(string.Empty, string.Empty, string.Empty, 0) { }
|
||||||
|
|
||||||
public void Validate(IStringLocalizer<Messages> localizer)
|
public void Validate(IStringLocalizer<Messages> localizer)
|
||||||
{
|
{
|
||||||
if (Id.IsEmpty())
|
if (Id.IsEmpty())
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ using CatHasPawsContratcs.Exceptions;
|
|||||||
using CatHasPawsContratcs.Extensions;
|
using CatHasPawsContratcs.Extensions;
|
||||||
using CatHasPawsContratcs.Infrastructure;
|
using CatHasPawsContratcs.Infrastructure;
|
||||||
using CatHasPawsContratcs.Infrastructure.PostConfigurations;
|
using CatHasPawsContratcs.Infrastructure.PostConfigurations;
|
||||||
using Newtonsoft.Json.Linq;
|
using CatHasPawsContratcs.Mapper;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using CatHasPawsContratcs.Resources;
|
using CatHasPawsContratcs.Resources;
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace CatHasPawsContratcs.DataModels;
|
namespace CatHasPawsContratcs.DataModels;
|
||||||
|
|
||||||
@@ -18,21 +19,12 @@ internal class PostDataModel(string postId, string postName, PostType postType,
|
|||||||
|
|
||||||
public PostType PostType { get; private set; } = postType;
|
public PostType PostType { get; private set; } = postType;
|
||||||
|
|
||||||
|
[AlternativeName("ConfigurationJson")]
|
||||||
|
[AlternativeName("Configuration")]
|
||||||
|
[PostProcessing(MappingCallMethodName = "ParseJson")]
|
||||||
public PostConfiguration ConfigurationModel { get; private set; } = configuration;
|
public PostConfiguration ConfigurationModel { get; private set; } = configuration;
|
||||||
|
|
||||||
public PostDataModel(string postId, string postName, PostType postType, string configurationJson) : this(postId, postName, postType, (PostConfiguration)null)
|
public PostDataModel() : this(string.Empty, string.Empty, PostType.None, null) { }
|
||||||
{
|
|
||||||
var obj = JToken.Parse(configurationJson);
|
|
||||||
if (obj is not null)
|
|
||||||
{
|
|
||||||
ConfigurationModel = obj.Value<string>("Type") switch
|
|
||||||
{
|
|
||||||
nameof(CashierPostConfiguration) => JsonConvert.DeserializeObject<CashierPostConfiguration>(configurationJson)!,
|
|
||||||
nameof(SupervisorPostConfiguration) => JsonConvert.DeserializeObject<SupervisorPostConfiguration>(configurationJson)!,
|
|
||||||
_ => JsonConvert.DeserializeObject<PostConfiguration>(configurationJson)!,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Validate(IStringLocalizer<Messages> localizer)
|
public void Validate(IStringLocalizer<Messages> localizer)
|
||||||
{
|
{
|
||||||
@@ -54,4 +46,19 @@ internal class PostDataModel(string postId, string postName, PostType postType,
|
|||||||
if (ConfigurationModel!.Rate <= 0)
|
if (ConfigurationModel!.Rate <= 0)
|
||||||
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Rate"));
|
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Rate"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private PostConfiguration? ParseJson(string json)
|
||||||
|
{
|
||||||
|
var obj = JToken.Parse(json);
|
||||||
|
if (obj is not null)
|
||||||
|
{
|
||||||
|
return obj.Value<string>("Type") switch
|
||||||
|
{
|
||||||
|
nameof(CashierPostConfiguration) => JsonConvert.DeserializeObject<CashierPostConfiguration>(json)!,
|
||||||
|
nameof(SupervisorPostConfiguration) => JsonConvert.DeserializeObject<SupervisorPostConfiguration>(json)!,
|
||||||
|
_ => JsonConvert.DeserializeObject<PostConfiguration>(json)!,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace CatHasPawsContratcs.Mapper;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
|
||||||
|
class AlternativeNameAttribute(string alternativeName) : Attribute
|
||||||
|
{
|
||||||
|
public string AlternativeName { get; set; } = alternativeName;
|
||||||
|
}
|
||||||
159
TheCatHasPawsProject/CatHasPawsContratcs/Mapper/CustomMapper.cs
Normal file
159
TheCatHasPawsProject/CatHasPawsContratcs/Mapper/CustomMapper.cs
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace CatHasPawsContratcs.Mapper;
|
||||||
|
|
||||||
|
internal static class CustomMapper
|
||||||
|
{
|
||||||
|
public static To MapObject<To>(object obj, To newObject)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(obj);
|
||||||
|
ArgumentNullException.ThrowIfNull(newObject);
|
||||||
|
var typeFrom = obj.GetType();
|
||||||
|
var typeTo = newObject.GetType();
|
||||||
|
var propertiesFrom = typeFrom.GetProperties().Where(x => x.CanRead).ToArray();
|
||||||
|
foreach (var property in typeTo.GetProperties().Where(x => x.CanWrite))
|
||||||
|
{
|
||||||
|
//ignore
|
||||||
|
|
||||||
|
var propertyFrom = TryGetPropertyFrom(property, propertiesFrom);
|
||||||
|
if (propertyFrom is null)
|
||||||
|
{
|
||||||
|
FindAndMapDefaultValue(property, newObject);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fromValue = propertyFrom.GetValue(obj);
|
||||||
|
var postProcessingAttribute = property.GetCustomAttribute<PostProcessingAttribute>();
|
||||||
|
if (postProcessingAttribute is not null)
|
||||||
|
{
|
||||||
|
var value = PostProcessing(fromValue, postProcessingAttribute, newObject);
|
||||||
|
if (value is not null)
|
||||||
|
{
|
||||||
|
property.SetValue(newObject, value);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (propertyFrom.PropertyType.IsGenericType && propertyFrom.PropertyType.Name.StartsWith("List") && fromValue is not null)
|
||||||
|
{
|
||||||
|
fromValue = MapListOfObjects(property, fromValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fromValue is not null)
|
||||||
|
{
|
||||||
|
// propertyFrom is enum && property is not enum ToString
|
||||||
|
// property is enum Enum.TryParse
|
||||||
|
property.SetValue(newObject, fromValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fields
|
||||||
|
|
||||||
|
var classPostProcessing = typeTo.GetCustomAttribute<PostProcessingAttribute>();
|
||||||
|
if (classPostProcessing is not null && classPostProcessing.MappingCallMethodName is not null)
|
||||||
|
{
|
||||||
|
var methodInfo = typeTo.GetMethod(classPostProcessing.MappingCallMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
methodInfo?.Invoke(newObject, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static To MapObject<To>(object obj) => MapObject(obj, Activator.CreateInstance<To>()!);
|
||||||
|
|
||||||
|
public static To? MapObjectWithNull<To>(object? obj) => obj is null ? default : MapObject(obj, Activator.CreateInstance<To>());
|
||||||
|
|
||||||
|
private static PropertyInfo? TryGetPropertyFrom(PropertyInfo propertyTo, PropertyInfo[] propertiesFrom)
|
||||||
|
{
|
||||||
|
var customAttribute = propertyTo.GetCustomAttributes<AlternativeNameAttribute>()?
|
||||||
|
.ToArray()
|
||||||
|
.FirstOrDefault(x => propertiesFrom.Any(y => y.Name == x.AlternativeName));
|
||||||
|
if (customAttribute is not null)
|
||||||
|
{
|
||||||
|
return propertiesFrom.FirstOrDefault(x => x.Name == customAttribute.AlternativeName);
|
||||||
|
}
|
||||||
|
return propertiesFrom.FirstOrDefault(x => x.Name == propertyTo.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object? PostProcessing<T>(object? value, PostProcessingAttribute postProcessingAttribute, T newObject)
|
||||||
|
{
|
||||||
|
if (value is null || newObject is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrEmpty(postProcessingAttribute.MappingCallMethodName))
|
||||||
|
{
|
||||||
|
var methodInfo =
|
||||||
|
newObject.GetType().GetMethod(postProcessingAttribute.MappingCallMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
if (methodInfo is not null)
|
||||||
|
{
|
||||||
|
return methodInfo.Invoke(newObject, [value]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (postProcessingAttribute.ActionType != PostProcessingType.None)
|
||||||
|
{
|
||||||
|
switch (postProcessingAttribute.ActionType)
|
||||||
|
{
|
||||||
|
case PostProcessingType.ToUniversalTime:
|
||||||
|
return ToUniversalTime(value);
|
||||||
|
case PostProcessingType.ToLocalTime:
|
||||||
|
return ToLocalTime(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object? ToLocalTime(object? obj)
|
||||||
|
{
|
||||||
|
if (obj is DateTime date)
|
||||||
|
return date.ToLocalTime();
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object? ToUniversalTime(object? obj)
|
||||||
|
{
|
||||||
|
if (obj is DateTime date)
|
||||||
|
return date.ToUniversalTime();
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FindAndMapDefaultValue<T>(PropertyInfo property, T newObject)
|
||||||
|
{
|
||||||
|
var defaultValueAttribute = property.GetCustomAttribute<DefaultValueAttribute>();
|
||||||
|
if (defaultValueAttribute is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (defaultValueAttribute.DefaultValue is not null)
|
||||||
|
{
|
||||||
|
property.SetValue(newObject, defaultValueAttribute.DefaultValue);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var value = defaultValueAttribute.FuncName switch
|
||||||
|
{
|
||||||
|
"UtcNow" => DateTime.UtcNow,
|
||||||
|
_ => (object?)null,
|
||||||
|
};
|
||||||
|
if (value is not null)
|
||||||
|
{
|
||||||
|
property.SetValue(newObject, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object? MapListOfObjects(PropertyInfo propertyTo, object list)
|
||||||
|
{
|
||||||
|
var listResult = Activator.CreateInstance(propertyTo.PropertyType);
|
||||||
|
foreach (var elem in (IEnumerable)list)
|
||||||
|
{
|
||||||
|
var newElem = MapObject(elem, Activator.CreateInstance(propertyTo.PropertyType.GenericTypeArguments[0])!);
|
||||||
|
if (newElem is not null)
|
||||||
|
{
|
||||||
|
propertyTo.PropertyType.GetMethod("Add")!.Invoke(listResult, [newElem]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return listResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace CatHasPawsContratcs.Mapper;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
|
class DefaultValueAttribute : Attribute
|
||||||
|
{
|
||||||
|
public object? DefaultValue { get; set; }
|
||||||
|
|
||||||
|
public string? FuncName { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace CatHasPawsContratcs.Mapper;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
|
||||||
|
class PostProcessingAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string? MappingCallMethodName { get; set; }
|
||||||
|
|
||||||
|
public PostProcessingType ActionType { get; set; } = PostProcessingType.None;
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace CatHasPawsContratcs.Mapper;
|
||||||
|
|
||||||
|
enum PostProcessingType
|
||||||
|
{
|
||||||
|
None = -1,
|
||||||
|
|
||||||
|
ToUniversalTime = 1,
|
||||||
|
|
||||||
|
ToLocalTime = 2
|
||||||
|
}
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
namespace CatHasPawsContratcs.ViewModels;
|
using CatHasPawsContratcs.Infrastructure.PostConfigurations;
|
||||||
|
using CatHasPawsContratcs.Mapper;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace CatHasPawsContratcs.ViewModels;
|
||||||
|
|
||||||
public class PostViewModel
|
public class PostViewModel
|
||||||
{
|
{
|
||||||
@@ -8,5 +12,10 @@ public class PostViewModel
|
|||||||
|
|
||||||
public required string PostType { get; set; }
|
public required string PostType { get; set; }
|
||||||
|
|
||||||
|
[AlternativeName("ConfigurationModel")]
|
||||||
|
[PostProcessing(MappingCallMethodName = "ParseConfiguration")]
|
||||||
public required string Configuration { get; set; }
|
public required string Configuration { get; set; }
|
||||||
|
|
||||||
|
private string ParseConfiguration(PostConfiguration model) =>
|
||||||
|
JsonSerializer.Serialize(model, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using AutoMapper;
|
using CatHasPawsContratcs.DataModels;
|
||||||
using CatHasPawsContratcs.DataModels;
|
|
||||||
using CatHasPawsContratcs.Exceptions;
|
using CatHasPawsContratcs.Exceptions;
|
||||||
|
using CatHasPawsContratcs.Mapper;
|
||||||
using CatHasPawsContratcs.Resources;
|
using CatHasPawsContratcs.Resources;
|
||||||
using CatHasPawsContratcs.StoragesContracts;
|
using CatHasPawsContratcs.StoragesContracts;
|
||||||
using CatHasPawsDatabase.Models;
|
using CatHasPawsDatabase.Models;
|
||||||
@@ -10,29 +10,16 @@ using Npgsql;
|
|||||||
|
|
||||||
namespace CatHasPawsDatabase.Implementations;
|
namespace CatHasPawsDatabase.Implementations;
|
||||||
|
|
||||||
internal class BuyerStorageContract : IBuyerStorageContract
|
internal class BuyerStorageContract(CatHasPawsDbContext catHasPawsDbContext, IStringLocalizer<Messages> localizer) : IBuyerStorageContract
|
||||||
{
|
{
|
||||||
private readonly CatHasPawsDbContext _dbContext;
|
private readonly CatHasPawsDbContext _dbContext = catHasPawsDbContext;
|
||||||
private readonly Mapper _mapper;
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
||||||
private readonly IStringLocalizer<Messages> _localizer;
|
|
||||||
|
|
||||||
public BuyerStorageContract(CatHasPawsDbContext catHasPawsDbContext, IStringLocalizer<Messages> localizer)
|
|
||||||
{
|
|
||||||
_dbContext = catHasPawsDbContext;
|
|
||||||
var config = new MapperConfiguration(cfg =>
|
|
||||||
{
|
|
||||||
cfg.CreateMap<Buyer, BuyerDataModel>();
|
|
||||||
cfg.CreateMap<BuyerDataModel, Buyer>();
|
|
||||||
});
|
|
||||||
_mapper = new Mapper(config);
|
|
||||||
_localizer = localizer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<BuyerDataModel> GetList()
|
public List<BuyerDataModel> GetList()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return [.. _dbContext.Buyers.Select(x => _mapper.Map<BuyerDataModel>(x))];
|
return [.. _dbContext.Buyers.Select(x => CustomMapper.MapObject<BuyerDataModel>(x))];
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -45,7 +32,7 @@ internal class BuyerStorageContract : IBuyerStorageContract
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return _mapper.Map<BuyerDataModel>(GetBuyerById(id));
|
return CustomMapper.MapObjectWithNull<BuyerDataModel>(GetBuyerById(id));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -58,7 +45,7 @@ internal class BuyerStorageContract : IBuyerStorageContract
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return _mapper.Map<BuyerDataModel>(_dbContext.Buyers.FirstOrDefault(x => x.FIO == fio));
|
return CustomMapper.MapObjectWithNull<BuyerDataModel>(_dbContext.Buyers.FirstOrDefault(x => x.FIO == fio));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -71,7 +58,7 @@ internal class BuyerStorageContract : IBuyerStorageContract
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return _mapper.Map<BuyerDataModel>(_dbContext.Buyers.FirstOrDefault(x => x.PhoneNumber == phoneNumber));
|
return CustomMapper.MapObjectWithNull<BuyerDataModel>(_dbContext.Buyers.FirstOrDefault(x => x.PhoneNumber == phoneNumber));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -84,7 +71,7 @@ internal class BuyerStorageContract : IBuyerStorageContract
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_dbContext.Buyers.Add(_mapper.Map<Buyer>(buyerDataModel));
|
_dbContext.Buyers.Add(CustomMapper.MapObject<Buyer>(buyerDataModel));
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
catch (InvalidOperationException ex) when (ex.TargetSite?.Name == "ThrowIdentityConflict")
|
||||||
@@ -114,7 +101,7 @@ internal class BuyerStorageContract : IBuyerStorageContract
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var element = GetBuyerById(buyerDataModel.Id) ?? throw new ElementNotFoundException(buyerDataModel.Id, _localizer);
|
var element = GetBuyerById(buyerDataModel.Id) ?? throw new ElementNotFoundException(buyerDataModel.Id, _localizer);
|
||||||
_dbContext.Buyers.Update(_mapper.Map(buyerDataModel, element));
|
_dbContext.Buyers.Update(CustomMapper.MapObject(buyerDataModel, element));
|
||||||
_dbContext.SaveChanges();
|
_dbContext.SaveChanges();
|
||||||
}
|
}
|
||||||
catch (ElementNotFoundException)
|
catch (ElementNotFoundException)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using CatHasPawsContratcs.Enums;
|
using CatHasPawsContratcs.Enums;
|
||||||
using CatHasPawsContratcs.Infrastructure.PostConfigurations;
|
using CatHasPawsContratcs.Infrastructure.PostConfigurations;
|
||||||
|
using CatHasPawsContratcs.Mapper;
|
||||||
|
|
||||||
namespace CatHasPawsDatabase.Models;
|
namespace CatHasPawsDatabase.Models;
|
||||||
|
|
||||||
@@ -13,9 +14,12 @@ internal class Post
|
|||||||
|
|
||||||
public PostType PostType { get; set; }
|
public PostType PostType { get; set; }
|
||||||
|
|
||||||
|
[AlternativeName("ConfigurationModel")]
|
||||||
public required PostConfiguration Configuration { get; set; }
|
public required PostConfiguration Configuration { get; set; }
|
||||||
|
|
||||||
|
[DefaultValue(DefaultValue = true)]
|
||||||
public bool IsActual { get; set; }
|
public bool IsActual { get; set; }
|
||||||
|
|
||||||
|
[DefaultValue(FuncName = "UtcNow")]
|
||||||
public DateTime ChangeDate { get; set; }
|
public DateTime ChangeDate { get; set; }
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,29 @@
|
|||||||
using AutoMapper;
|
using CatHasPawsContratcs.AdapterContracts;
|
||||||
using CatHasPawsContratcs.AdapterContracts;
|
|
||||||
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
|
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
|
||||||
using CatHasPawsContratcs.BindingModels;
|
using CatHasPawsContratcs.BindingModels;
|
||||||
using CatHasPawsContratcs.BusinessLogicsContracts;
|
using CatHasPawsContratcs.BusinessLogicsContracts;
|
||||||
using CatHasPawsContratcs.DataModels;
|
using CatHasPawsContratcs.DataModels;
|
||||||
using CatHasPawsContratcs.Exceptions;
|
using CatHasPawsContratcs.Exceptions;
|
||||||
|
using CatHasPawsContratcs.Mapper;
|
||||||
using CatHasPawsContratcs.Resources;
|
using CatHasPawsContratcs.Resources;
|
||||||
using CatHasPawsContratcs.ViewModels;
|
using CatHasPawsContratcs.ViewModels;
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
|
|
||||||
namespace CatHasPawsWebApi.Adapters;
|
namespace CatHasPawsWebApi.Adapters;
|
||||||
|
|
||||||
internal class BuyerAdapter : IBuyerAdapter
|
internal class BuyerAdapter(IBuyerBusinessLogicContract buyerBusinessLogicContract, IStringLocalizer<Messages> localizer, ILogger<BuyerAdapter> logger) : IBuyerAdapter
|
||||||
{
|
{
|
||||||
private readonly IBuyerBusinessLogicContract _buyerBusinessLogicContract;
|
private readonly IBuyerBusinessLogicContract _buyerBusinessLogicContract = buyerBusinessLogicContract;
|
||||||
|
|
||||||
private readonly IStringLocalizer<Messages> _localizer;
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
||||||
|
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger = logger;
|
||||||
|
|
||||||
private readonly Mapper _mapper;
|
|
||||||
|
|
||||||
public BuyerAdapter(IBuyerBusinessLogicContract buyerBusinessLogicContract, IStringLocalizer<Messages> localizer, ILogger<BuyerAdapter> logger)
|
|
||||||
{
|
|
||||||
_buyerBusinessLogicContract = buyerBusinessLogicContract;
|
|
||||||
_logger = logger;
|
|
||||||
var config = new MapperConfiguration(cfg =>
|
|
||||||
{
|
|
||||||
cfg.CreateMap<BuyerBindingModel, BuyerDataModel>();
|
|
||||||
cfg.CreateMap<BuyerDataModel, BuyerViewModel>();
|
|
||||||
});
|
|
||||||
_mapper = new Mapper(config);
|
|
||||||
_localizer = localizer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BuyerOperationResponse GetList()
|
public BuyerOperationResponse GetList()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return BuyerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllBuyers().Select(x => _mapper.Map<BuyerViewModel>(x))]);
|
return BuyerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllBuyers().Select(x => CustomMapper.MapObject<BuyerViewModel>(x))]);
|
||||||
}
|
}
|
||||||
catch (StorageException ex)
|
catch (StorageException ex)
|
||||||
{
|
{
|
||||||
@@ -56,7 +41,7 @@ internal class BuyerAdapter : IBuyerAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return BuyerOperationResponse.OK(_mapper.Map<BuyerViewModel>(_buyerBusinessLogicContract.GetBuyerByData(data)));
|
return BuyerOperationResponse.OK(CustomMapper.MapObject<BuyerViewModel>(_buyerBusinessLogicContract.GetBuyerByData(data)));
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
@@ -84,7 +69,7 @@ internal class BuyerAdapter : IBuyerAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_buyerBusinessLogicContract.InsertBuyer(_mapper.Map<BuyerDataModel>(buyerModel));
|
_buyerBusinessLogicContract.InsertBuyer(CustomMapper.MapObject<BuyerDataModel>(buyerModel));
|
||||||
return BuyerOperationResponse.NoContent();
|
return BuyerOperationResponse.NoContent();
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
@@ -118,7 +103,7 @@ internal class BuyerAdapter : IBuyerAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_buyerBusinessLogicContract.UpdateBuyer(_mapper.Map<BuyerDataModel>(buyerModel));
|
_buyerBusinessLogicContract.UpdateBuyer(CustomMapper.MapObject<BuyerDataModel>(buyerModel));
|
||||||
return BuyerOperationResponse.NoContent();
|
return BuyerOperationResponse.NoContent();
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
|
|||||||
@@ -1,48 +1,32 @@
|
|||||||
using AutoMapper;
|
using CatHasPawsContratcs.AdapterContracts;
|
||||||
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
|
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
|
||||||
using CatHasPawsContratcs.AdapterContracts;
|
|
||||||
using CatHasPawsContratcs.BindingModels;
|
using CatHasPawsContratcs.BindingModels;
|
||||||
using CatHasPawsContratcs.BusinessLogicsContracts;
|
using CatHasPawsContratcs.BusinessLogicsContracts;
|
||||||
using CatHasPawsContratcs.DataModels;
|
using CatHasPawsContratcs.DataModels;
|
||||||
using CatHasPawsContratcs.Exceptions;
|
using CatHasPawsContratcs.Exceptions;
|
||||||
using CatHasPawsContratcs.ViewModels;
|
using CatHasPawsContratcs.Mapper;
|
||||||
using System.Text.Json;
|
|
||||||
using CatHasPawsContratcs.Resources;
|
using CatHasPawsContratcs.Resources;
|
||||||
|
using CatHasPawsContratcs.ViewModels;
|
||||||
using Microsoft.Extensions.Localization;
|
using Microsoft.Extensions.Localization;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace CatHasPawsWebApi.Adapters;
|
namespace CatHasPawsWebApi.Adapters;
|
||||||
|
|
||||||
internal class PostAdapter : IPostAdapter
|
internal class PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, IStringLocalizer<Messages> localizer, ILogger<PostAdapter> logger) : IPostAdapter
|
||||||
{
|
{
|
||||||
private readonly IPostBusinessLogicContract _postBusinessLogicContract;
|
private readonly IPostBusinessLogicContract _postBusinessLogicContract = postBusinessLogicContract;
|
||||||
|
|
||||||
private readonly IStringLocalizer<Messages> _localizer;
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
||||||
|
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger = logger;
|
||||||
|
|
||||||
private readonly Mapper _mapper;
|
|
||||||
|
|
||||||
private readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
|
private readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
|
||||||
|
|
||||||
public PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, IStringLocalizer<Messages> localizer, ILogger<PostAdapter> logger)
|
|
||||||
{
|
|
||||||
_postBusinessLogicContract = postBusinessLogicContract;
|
|
||||||
_logger = logger;
|
|
||||||
var config = new MapperConfiguration(cfg =>
|
|
||||||
{
|
|
||||||
cfg.CreateMap<PostBindingModel, PostDataModel>();
|
|
||||||
cfg.CreateMap<PostDataModel, PostViewModel>()
|
|
||||||
.ForMember(x => x.Configuration, x => x.MapFrom(src => JsonSerializer.Serialize(src.ConfigurationModel, JsonSerializerOptions)));
|
|
||||||
});
|
|
||||||
_mapper = new Mapper(config);
|
|
||||||
_localizer = localizer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PostOperationResponse GetList()
|
public PostOperationResponse GetList()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllPosts().Select(x => _mapper.Map<PostViewModel>(x))]);
|
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllPosts().Select(x => CustomMapper.MapObject<PostViewModel>(x))]);
|
||||||
}
|
}
|
||||||
catch (StorageException ex)
|
catch (StorageException ex)
|
||||||
{
|
{
|
||||||
@@ -60,7 +44,7 @@ internal class PostAdapter : IPostAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllDataOfPost(id).Select(x => _mapper.Map<PostViewModel>(x))]);
|
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllDataOfPost(id).Select(x => CustomMapper.MapObject<PostViewModel>(x))]);
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
@@ -88,7 +72,7 @@ internal class PostAdapter : IPostAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return PostOperationResponse.OK(_mapper.Map<PostViewModel>(_postBusinessLogicContract.GetPostByData(data)));
|
return PostOperationResponse.OK(CustomMapper.MapObject<PostViewModel>(_postBusinessLogicContract.GetPostByData(data)));
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
{
|
{
|
||||||
@@ -121,7 +105,7 @@ internal class PostAdapter : IPostAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_postBusinessLogicContract.InsertPost(_mapper.Map<PostDataModel>(postModel));
|
_postBusinessLogicContract.InsertPost(CustomMapper.MapObject<PostDataModel>(postModel));
|
||||||
return PostOperationResponse.NoContent();
|
return PostOperationResponse.NoContent();
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
@@ -155,7 +139,7 @@ internal class PostAdapter : IPostAdapter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_postBusinessLogicContract.UpdatePost(_mapper.Map<PostDataModel>(postModel));
|
_postBusinessLogicContract.UpdatePost(CustomMapper.MapObject<PostDataModel>(postModel));
|
||||||
return PostOperationResponse.NoContent();
|
return PostOperationResponse.NoContent();
|
||||||
}
|
}
|
||||||
catch (ArgumentNullException ex)
|
catch (ArgumentNullException ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user