174 lines
6.7 KiB
C#
174 lines
6.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text.Json;
|
||
using System.Text.RegularExpressions;
|
||
using CandyHouseBase.DataModels;
|
||
using CandyHouseBase.Enums;
|
||
using CandyHouseBase.Exceptions;
|
||
using CandyHouseBase.Extensions;
|
||
using CandyHouseBase.Interfaces.BusinessLogicsContracts;
|
||
using CandyHouseBase.Interfaces.StoragesContracts;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace CandyHouseBase.Implementations
|
||
{
|
||
internal class PekarBusinessLogicContract(
|
||
IPekarStorageContact pekarStorageContact,
|
||
IProductStorageContact productStorageContact,
|
||
IPositionStorageContact positionStorageContact,
|
||
ILogger logger)
|
||
: IPekarBusinessLogicContact
|
||
{
|
||
private readonly IPekarStorageContact _pekarStorageContact = pekarStorageContact;
|
||
private readonly IProductStorageContact _productStorageContact = productStorageContact;
|
||
private readonly IPositionStorageContact _positionStorageContact = positionStorageContact;
|
||
private readonly ILogger _logger = logger;
|
||
|
||
public List<PekarDataModel> GetAllPekars()
|
||
{
|
||
_logger.LogInformation("GetAllPekars");
|
||
var pekars = _pekarStorageContact.GetList() ?? throw new NullListException();
|
||
return pekars;
|
||
}
|
||
|
||
public List<PekarDataModel> GetAllDataOfPekar(string pekarId)
|
||
{
|
||
_logger.LogInformation("GetAllDataOfPekar for pekarId: {pekarId}", pekarId);
|
||
if (pekarId.IsEmpty())
|
||
throw new ArgumentNullException(nameof(pekarId));
|
||
if (!pekarId.IsGuid())
|
||
throw new ValidationException("pekarId must be a GUID");
|
||
|
||
var pekarsWithHistory = _pekarStorageContact.GetPekarWithHistory(pekarId);
|
||
if (pekarsWithHistory == null || !pekarsWithHistory.Any())
|
||
throw new ElementNotFoundException(pekarId);
|
||
|
||
var historyRecords = new List<PekarDataModel>();
|
||
foreach (var pekar in pekarsWithHistory)
|
||
{
|
||
Guid positionGuid;
|
||
if (!Guid.TryParse(pekar.Position, out positionGuid))
|
||
{
|
||
_logger.LogWarning("Invalid Position GUID: {Position}", pekar.Position);
|
||
continue;
|
||
}
|
||
|
||
var position = _positionStorageContact.GetElementById(positionGuid.ToString());
|
||
if (position == null)
|
||
throw new ElementNotFoundException(pekar.Position);
|
||
historyRecords.Add(new PekarDataModel(pekar.Id, pekar.FIO, position.Id, pekar.BonusCoefficient,
|
||
new List<ProductDataModel>()));
|
||
}
|
||
|
||
if (!historyRecords.Any())
|
||
{
|
||
var firstPekar = pekarsWithHistory.First();
|
||
Guid positionGuid;
|
||
if (Guid.TryParse(firstPekar.Position, out positionGuid))
|
||
{
|
||
var position = _positionStorageContact.GetElementById(positionGuid.ToString());
|
||
if (position != null)
|
||
historyRecords.Add(new PekarDataModel(firstPekar.Id, firstPekar.FIO, position.Id,
|
||
firstPekar.BonusCoefficient,
|
||
new List<ProductDataModel>()));
|
||
}
|
||
}
|
||
|
||
return historyRecords;
|
||
}
|
||
|
||
public PekarDataModel GetPekarByData(string data)
|
||
{
|
||
_logger.LogInformation("GetPekarByData for data: {data}", data);
|
||
if (data.IsEmpty())
|
||
throw new ArgumentNullException(nameof(data));
|
||
if (!data.IsGuid() && !Regex.IsMatch(data, @"^[A-Za-zА-Яа-яЁё\s\-]+$"))
|
||
throw new ValidationException("data must be a GUID or FIO");
|
||
|
||
var pekar = _pekarStorageContact.GetElementById(data) ?? _pekarStorageContact.GetElementByFio(data) ??
|
||
throw new ElementNotFoundException(data);
|
||
return pekar;
|
||
}
|
||
|
||
public void InsertPekar(PekarDataModel pekar)
|
||
{
|
||
_logger.LogInformation("InsertPekar: {json}", JsonSerializer.Serialize(pekar));
|
||
if (pekar == null)
|
||
throw new ArgumentNullException(nameof(pekar));
|
||
|
||
pekar.Validate();
|
||
|
||
var existingPekar = _pekarStorageContact.GetElementById(pekar.Id);
|
||
if (existingPekar != null)
|
||
{
|
||
var history = new PekarHistoryDataModel(
|
||
existingPekar.Id,
|
||
existingPekar.FIO,
|
||
existingPekar.Position,
|
||
existingPekar.BonusCoefficient,
|
||
DateTime.Now
|
||
);
|
||
}
|
||
|
||
foreach (var product in pekar.ProductsItems)
|
||
{
|
||
if (_productStorageContact.GetElementById(product.Id) == null)
|
||
throw new ElementNotFoundException(product.Id);
|
||
}
|
||
|
||
_pekarStorageContact.AddElement(pekar);
|
||
}
|
||
|
||
public void UpdatePekar(PekarDataModel pekar)
|
||
{
|
||
_logger.LogInformation("UpdatePekar: {json}", JsonSerializer.Serialize(pekar));
|
||
if (pekar == null)
|
||
throw new ArgumentNullException(nameof(pekar));
|
||
|
||
pekar.Validate();
|
||
|
||
foreach (var product in pekar.ProductsItems)
|
||
{
|
||
if (_productStorageContact.GetElementById(product.Id) == null)
|
||
throw new ElementNotFoundException(product.Id);
|
||
}
|
||
|
||
_pekarStorageContact.UpdateElement(pekar);
|
||
}
|
||
|
||
public void DeletePekar(string id)
|
||
{
|
||
_logger.LogInformation("DeletePekar for id: {id}", id);
|
||
if (id == null)
|
||
throw new ArgumentNullException(nameof(id));
|
||
if (string.IsNullOrEmpty(id))
|
||
throw new ArgumentNullException(nameof(id));
|
||
if (!id.IsGuid())
|
||
throw new ValidationException("id must be a GUID");
|
||
|
||
var pekar = _pekarStorageContact.GetElementById(id);
|
||
if (pekar == null)
|
||
throw new ElementNotFoundException(id);
|
||
|
||
_pekarStorageContact.DeleteElement(id);
|
||
}
|
||
|
||
public void RestorePekar(string id)
|
||
{
|
||
_logger.LogInformation("RestorePekar for id: {id}", id);
|
||
if (id == null)
|
||
throw new ArgumentNullException(nameof(id));
|
||
if (string.IsNullOrEmpty(id))
|
||
throw new ArgumentNullException(nameof(id));
|
||
if (!id.IsGuid())
|
||
throw new ValidationException("id must be a GUID");
|
||
|
||
var pekar = _pekarStorageContact.GetElementById(id);
|
||
if (pekar == null)
|
||
throw new ElementNotFoundException(id);
|
||
|
||
_pekarStorageContact.RestoreElement(id);
|
||
}
|
||
}
|
||
} |