PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicBusinessLogics/BusinessLogics/ServiceLogic.cs

136 lines
4.4 KiB
C#
Raw Normal View History

2024-05-01 01:24:55 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StoragesContracts;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class ServiceLogic : IServiceLogic
{
private readonly ILogger _logger;
private readonly IServiceStorage _serviceStorage;
public ServiceLogic(ILogger<ServiceLogic> logger, IServiceStorage serviceStorage)
{
_logger = logger;
_serviceStorage = serviceStorage;
}
public bool Create(ServiceBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Service.Id: {Id}", model.Id);
if (_serviceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ServiceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Service.Id: {Id}", model.Id);
if (_serviceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ServiceViewModel? ReadElement(ServiceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Service.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var element = _serviceStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Service.Id: {Id}", element?.Id);
return element;
}
public List<ServiceViewModel>? ReadList(ServiceSearchModel? model)
{
_logger.LogInformation("ReadList. Service.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(ServiceBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Service.Id: {Id}", model.Id);
if (_serviceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ServiceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Не указано название услуги", nameof(model.Name));
}
if (model.Cost <= 0)
{
throw new ArgumentNullException("Не указан цена", nameof(model.Cost));
}
if (model.MedicationId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор медикоментов", nameof(model.MedicationId));
}
_logger.LogInformation("CheckModel. Service.Id: {Id}", model.Id);
var element = _serviceStorage.GetElement(new ServiceSearchModel
{
Name = model.Name
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Услуга с данным названием уже есть");
}
}
}
}