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

118 lines
4.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
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 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("Процедура с таким названием уже есть");
}
}
}
}