в сущности подобавлял
This commit is contained in:
@@ -17,6 +17,7 @@ internal class IngredientDataModel(string id, string NameIngredients, string siz
|
||||
public string SizeInit { get; private set; } = sizeInit;
|
||||
public double InitPrice { get; private set; } = initPrice;
|
||||
|
||||
public IngredientDataModel(): this(string.Empty, string.Empty, string.Empty, 0) { }
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
|
||||
@@ -15,6 +15,8 @@ internal class IngredientHistoryDataModel(string productId, double oldPrice) : I
|
||||
public string IngredienId { get; private set; } = productId;
|
||||
public double OldPrice { get; private set; } = oldPrice;
|
||||
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
|
||||
|
||||
public IngredientHistoryDataModel():this(string.Empty, 0) { }
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
{
|
||||
if (IngredienId.IsEmpty())
|
||||
|
||||
@@ -52,4 +52,18 @@ internal class PostDataModel(string postId, string postName, PostType postType,
|
||||
if (ConfigurationModel!.Rate <= 0)
|
||||
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(ManufacturerPostConfiguration) => JsonConvert.DeserializeObject<ManufacturerPostConfiguration>(json)!,
|
||||
nameof(PackerPostConfiguration) => JsonConvert.DeserializeObject<PackerPostConfiguration>(json)!,
|
||||
_ => JsonConvert.DeserializeObject<PostConfiguration>(json)!,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using SladkieBulkiContrakts.Enums;
|
||||
using SladkieBulkiContrakts.Exceptions;
|
||||
using SladkieBulkiContrakts.Extensions;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.Mapper;
|
||||
using SladkieBulkiContrakts.Resources;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
@@ -17,14 +18,14 @@ internal class ProductDataModel : IValidation
|
||||
public string Name { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public ProductType ProductType { get; private set; }
|
||||
|
||||
[AlternativeName("ProductIngredients")]
|
||||
public List<ProductIngredientDataModel> Ingredients { get; private set; }
|
||||
public double UnitPrice { get; private set; }
|
||||
public bool IsDeleted { get; private set; }
|
||||
|
||||
public ProductDataModel()
|
||||
{
|
||||
Ingredients = new List<ProductIngredientDataModel>();
|
||||
}
|
||||
public ProductDataModel(): this(string.Empty, string.Empty, string.Empty, ProductType.None, new List<ProductIngredientDataModel>(), 0, false) { }
|
||||
|
||||
|
||||
public ProductDataModel(string id, string name, string description, ProductType productType, List<ProductIngredientDataModel> ingredients, double unitPrice, bool isDeleted)
|
||||
{
|
||||
|
||||
@@ -15,6 +15,8 @@ internal class ProductHistoryDataModel(string productId, double oldPrice): IVali
|
||||
public string ProductId { get; private set; } = productId;
|
||||
public double OldPrice { get; private set; } = oldPrice;
|
||||
public DateTime ChangeDate { get; private set; } = DateTime.UtcNow;
|
||||
|
||||
public ProductHistoryDataModel(): this(string.Empty, 0) { }
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
{
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ internal class ProductIngredientDataModel(string productId, string ingredientId,
|
||||
public string ProductId { get; private set; } = productId;
|
||||
public string IngredientId { get; private set; } = ingredientId;
|
||||
public int Count { get; private set; } = count;
|
||||
|
||||
public ProductIngredientDataModel(): this(string.Empty, string.Empty, 1) { }
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
{
|
||||
if (ProductId.IsEmpty())
|
||||
|
||||
@@ -2,15 +2,18 @@
|
||||
using SladkieBulkiContrakts.Extensions;
|
||||
using SladkieBulkiContrakts.Infrastructure;
|
||||
using SladkieBulkiContrakts.Resources;
|
||||
using SladkieBulkiContrakts.Mapper;
|
||||
|
||||
internal class ProductionDataModel(string id, DateTime productionDate, int count, double sum, string workerId, string productId) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
[IgnoreValue]
|
||||
public DateTime ProductionDate { get; private set; } = productionDate.ToUniversalTime();
|
||||
public int Count { get; private set; } = count;
|
||||
public double Sum { get; private set; } = sum;
|
||||
public string WorkerId { get; private set; } = workerId;
|
||||
public string ProductId { get; private set; } = productId;
|
||||
public ProductionDataModel(): this(string.Empty, DateTime.UtcNow, 1, 10, string.Empty, string.Empty) { }
|
||||
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
{
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
internal class SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary) : IValidation
|
||||
{
|
||||
[SourceFromMember(SourcePropertyName = "Worker")]
|
||||
private readonly WorkerDataModel? _worker;
|
||||
public string WorkerId { get; private set; } = workerId;
|
||||
public DateTime SalaryDate { get; private set; } = salaryDate.ToUniversalTime();
|
||||
@@ -21,6 +22,9 @@ internal class SalaryDataModel(string workerId, DateTime salaryDate, double work
|
||||
{
|
||||
_worker = worker;
|
||||
}
|
||||
|
||||
public SalaryDataModel(): this(string.Empty, DateTime.UtcNow, 100) { }
|
||||
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
{
|
||||
if (WorkerId.IsEmpty())
|
||||
|
||||
@@ -12,11 +12,13 @@ using System.Numerics;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using SladkieBulkiContrakts.Resources;
|
||||
using SladkieBulkiContrakts.Infrastructure.PostConfigurations;
|
||||
|
||||
namespace SladkieBulkiContrakts.DataModels;
|
||||
|
||||
internal class WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted, string email) : IValidation
|
||||
{
|
||||
[SourceFromMember(SourcePropertyName = "Post")]
|
||||
private readonly PostDataModel? _post;
|
||||
public string Id { get; private set; } = id;
|
||||
public string FIO { get; private set; } = fio;
|
||||
@@ -25,13 +27,13 @@ internal class WorkerDataModel(string id, string fio, string postId, DateTime bi
|
||||
public DateTime EmploymentDate { get; private set; } = employmentDate.ToUniversalTime();
|
||||
public bool IsDeleted { get; private set; } = isDeleted;
|
||||
public string Email { get; private set; } = email;
|
||||
|
||||
public PostConfiguration? PostConfiguration => _post?.ConfigurationModel ?? null;
|
||||
public string PostName => _post?.PostName ?? string.Empty;
|
||||
public WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted, string email, PostDataModel post) : this(id, fio, postId, birthDate, employmentDate, isDeleted, email)
|
||||
{
|
||||
_post = post;
|
||||
}
|
||||
|
||||
public WorkerDataModel() : this(string.Empty, string.Empty, string.Empty, DateTime.MinValue, DateTime.MinValue, false, string.Empty) { }
|
||||
public WorkerDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, string email) : this(id, fio, postId, birthDate, employmentDate, false, email) { }
|
||||
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
|
||||
@@ -48,10 +48,50 @@ internal static class CustomMapper
|
||||
|
||||
if (fromValue is not null)
|
||||
{
|
||||
// propertyFrom is enum && property is not enum ToString
|
||||
// property is enum Enum.TryParse
|
||||
property.SetValue(newObject, fromValue);
|
||||
// Enum -> string
|
||||
if (property.PropertyType == typeof(string) && propertyFrom.PropertyType.IsEnum)
|
||||
{
|
||||
property.SetValue(newObject, fromValue.ToString());
|
||||
}
|
||||
// string -> Enum
|
||||
else if (property.PropertyType.IsEnum && propertyFrom.PropertyType == typeof(string))
|
||||
{
|
||||
if (Enum.TryParse(property.PropertyType, fromValue.ToString(), out var enumValue))
|
||||
{
|
||||
property.SetValue(newObject, enumValue);
|
||||
}
|
||||
}
|
||||
// int -> Enum
|
||||
else if (property.PropertyType.IsEnum && propertyFrom.PropertyType == typeof(int))
|
||||
{
|
||||
property.SetValue(newObject, Enum.ToObject(property.PropertyType, fromValue));
|
||||
}
|
||||
// Enum -> int
|
||||
else if (property.PropertyType == typeof(int) && propertyFrom.PropertyType.IsEnum)
|
||||
{
|
||||
property.SetValue(newObject, (int)fromValue);
|
||||
}
|
||||
// Чистое совпадение типов
|
||||
else if (property.PropertyType.IsAssignableFrom(propertyFrom.PropertyType))
|
||||
{
|
||||
property.SetValue(newObject, fromValue);
|
||||
}
|
||||
// Попытка привести через Convert.ChangeType
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var converted = Convert.ChangeType(fromValue, property.PropertyType);
|
||||
property.SetValue(newObject, converted);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Последняя попытка - через ToString()
|
||||
property.SetValue(newObject, fromValue.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// fields
|
||||
|
||||
@@ -8,6 +8,7 @@ using SladkieBulkiContrakts.ViewModels;
|
||||
using SladkieBulkiContrakts.AdapterContracts;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using SladkieBulkiContrakts.Resources;
|
||||
using SladkieBulkiContrakts.Mapper;
|
||||
|
||||
namespace SladkieBulkiWedApi.Adapters;
|
||||
|
||||
@@ -15,19 +16,15 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
{
|
||||
private readonly IIngredientBusinessLogicContract _ingredientBusinessLogicContract;
|
||||
private readonly ILogger _logger;
|
||||
private readonly Mapper _mapper;
|
||||
private readonly IStringLocalizer<Messages> _localizer;
|
||||
|
||||
public IngredientAdapter(IIngredientBusinessLogicContract ingredientBusinessLogicContract, IStringLocalizer<Messages> localizer, ILogger<IngredientAdapter> logger)
|
||||
public IngredientAdapter(
|
||||
IIngredientBusinessLogicContract ingredientBusinessLogicContract,
|
||||
IStringLocalizer<Messages> localizer,
|
||||
ILogger<IngredientAdapter> logger)
|
||||
{
|
||||
_ingredientBusinessLogicContract = ingredientBusinessLogicContract;
|
||||
_logger = logger;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<IngredientBindingModel, IngredientDataModel>();
|
||||
cfg.CreateMap<IngredientDataModel, IngredientViewModel>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
_localizer = localizer;
|
||||
}
|
||||
|
||||
@@ -37,14 +34,16 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
{
|
||||
return IngredientOperationResponse.OK(
|
||||
_ingredientBusinessLogicContract.GetAllIngredients()
|
||||
.Select(x => _mapper.Map<IngredientViewModel>(x))
|
||||
.Select(x => CustomMapper.MapObject<IngredientViewModel>(x))
|
||||
.ToList()
|
||||
);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
||||
return IngredientOperationResponse.InternalServerError(
|
||||
string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException?.Message)
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -53,7 +52,6 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public IngredientOperationResponse GetElement(string data)
|
||||
{
|
||||
try
|
||||
@@ -65,7 +63,7 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
model = _ingredientBusinessLogicContract.GetIngredientByName(data);
|
||||
|
||||
return IngredientOperationResponse.OK(
|
||||
_mapper.Map<IngredientViewModel>(model)
|
||||
CustomMapper.MapObject<IngredientViewModel>(model)
|
||||
);
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
@@ -76,17 +74,23 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return IngredientOperationResponse.NotFound(string.Format(_localizer["AdapterMessageElementNotFoundException"], data));
|
||||
return IngredientOperationResponse.NotFound(
|
||||
string.Format(_localizer["AdapterMessageElementNotFoundException"], data)
|
||||
);
|
||||
}
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementDeletedException");
|
||||
return IngredientOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], data));
|
||||
return IngredientOperationResponse.BadRequest(
|
||||
string.Format(_localizer["AdapterMessageElementDeletedException"], data)
|
||||
);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
||||
return IngredientOperationResponse.InternalServerError(
|
||||
string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException?.Message)
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -99,7 +103,9 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
{
|
||||
try
|
||||
{
|
||||
_ingredientBusinessLogicContract.InsertIngredient(_mapper.Map<IngredientDataModel>(ingredientModel));
|
||||
_ingredientBusinessLogicContract.InsertIngredient(
|
||||
CustomMapper.MapObject<IngredientDataModel>(ingredientModel)
|
||||
);
|
||||
return IngredientOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
@@ -110,7 +116,9 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return IngredientOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
||||
return IngredientOperationResponse.BadRequest(
|
||||
string.Format(_localizer["AdapterMessageValidationException"], ex.Message)
|
||||
);
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
@@ -120,7 +128,9 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
||||
return IngredientOperationResponse.BadRequest(
|
||||
string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException?.Message)
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -133,7 +143,9 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
{
|
||||
try
|
||||
{
|
||||
_ingredientBusinessLogicContract.UpdateIngredient(_mapper.Map<IngredientDataModel>(ingredientModel));
|
||||
_ingredientBusinessLogicContract.UpdateIngredient(
|
||||
CustomMapper.MapObject<IngredientDataModel>(ingredientModel)
|
||||
);
|
||||
return IngredientOperationResponse.NoContent();
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
@@ -144,12 +156,16 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
catch (ValidationException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ValidationException");
|
||||
return IngredientOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
||||
return IngredientOperationResponse.BadRequest(
|
||||
string.Format(_localizer["AdapterMessageValidationException"], ex.Message)
|
||||
);
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementNotFoundException");
|
||||
return IngredientOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], ingredientModel.Id));
|
||||
return IngredientOperationResponse.BadRequest(
|
||||
string.Format(_localizer["AdapterMessageElementNotFoundException"], ingredientModel.Id)
|
||||
);
|
||||
}
|
||||
catch (ElementExistsException ex)
|
||||
{
|
||||
@@ -159,12 +175,16 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
catch (ElementDeletedException ex)
|
||||
{
|
||||
_logger.LogError(ex, "ElementDeletedException");
|
||||
return IngredientOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], ingredientModel.Id));
|
||||
return IngredientOperationResponse.BadRequest(
|
||||
string.Format(_localizer["AdapterMessageElementDeletedException"], ingredientModel.Id)
|
||||
);
|
||||
}
|
||||
catch (StorageException ex)
|
||||
{
|
||||
_logger.LogError(ex, "StorageException");
|
||||
return IngredientOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
||||
return IngredientOperationResponse.BadRequest(
|
||||
string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException?.Message)
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -172,5 +192,4 @@ internal class IngredientAdapter : IIngredientAdapter
|
||||
return IngredientOperationResponse.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user