Rogashova_E.A._CourseWork_H.../Hospital/HospitalBusinessLogic/BusinessLogics/ProceduresLogic.cs

151 lines
5.2 KiB
C#
Raw Normal View History

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class ProceduresLogic : IProceduresLogic
{
private readonly ILogger _logger;
private readonly IProceduresStorage _proceduresStorage;
public ProceduresLogic(ILogger<ProceduresLogic> logger, IProceduresStorage proceduresStorage)
{
_logger = logger;
_proceduresStorage = proceduresStorage;
}
public bool AddMedicineToProcedure(ProceduresSearchModel model, IMedicinesModel medicine)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("AddMedicine.Id:{ Id}", model.Id);
var element = _proceduresStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddMedicine element not found");
return false;
}
_logger.LogInformation("AddMedicine find. Id:{Id}", element.Id);
element.ProcedureMedicine[medicine.Id] = medicine;
_proceduresStorage.Update(new()
{
Id = element.Id,
ProceduresName = element.ProceduresName,
Type = element.Type,
ClientId = element.ClientId,
ProcedureMedicine = element.ProcedureMedicine
});
return true;
}
public bool Create(ProceduresBindingModel model)
{
CheckModel(model);
if (_proceduresStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ProceduresBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_proceduresStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ProceduresViewModel? ReadElement(ProceduresSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ProceduresName:{ProceduresName}. Id:{ Id}", model.ProceduresName, model.Id);
var element = _proceduresStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<ProceduresViewModel>? ReadList(ProceduresSearchModel? model)
{
_logger.LogInformation("ReadList. ProceduresName:{ProceduresName}. Id:{ Id}", model?.ProceduresName, model?.Id);
var list = model == null ? _proceduresStorage.GetFullList() : _proceduresStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(ProceduresBindingModel model)
{
CheckModel(model);
if (_proceduresStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ProceduresBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ProceduresName))
{
throw new ArgumentNullException("Нет названия процедуры", nameof(model.ProceduresName));
}
if (string.IsNullOrEmpty(model.Type))
{
throw new ArgumentNullException("Нет типа процедуры", nameof(model.Type));
}
_logger.LogInformation("Procedures. ProceduresName:{ProceduresName}. Type:{ Type}. Id: { Id} ", model.ProceduresName, model.Type, model.Id);
var element = _proceduresStorage.GetElement(new ProceduresSearchModel
{
ProceduresName = model.ProceduresName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Процедура с таким названием уже есть");
}
}
}
}