2024-05-13 09:00:08 +04:00
|
|
|
|
using DeviceContracts.BindingModels;
|
|
|
|
|
using DeviceContracts.BusinessLogicsContracts;
|
|
|
|
|
using DeviceContracts.SearchModels;
|
|
|
|
|
using DeviceContracts.StoragesContracts;
|
|
|
|
|
using DeviceContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace DeviceBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
2024-05-16 04:53:14 +04:00
|
|
|
|
public class CabinetLogic : ICabinetLogic
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2024-05-16 04:53:14 +04:00
|
|
|
|
private readonly ICabinetStorage _cabinetStorage;
|
|
|
|
|
public CabinetLogic(ILogger<CabinetLogic> logger,
|
|
|
|
|
ICabinetStorage cabinetStorage)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2024-05-16 04:53:14 +04:00
|
|
|
|
_cabinetStorage = cabinetStorage;
|
2024-05-13 09:00:08 +04:00
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
public bool Create(CabinetBindingModel model)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-05-16 04:53:14 +04:00
|
|
|
|
if (_cabinetStorage.Insert(model) == null)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
public bool Update(CabinetBindingModel model)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-05-16 04:53:14 +04:00
|
|
|
|
if (_cabinetStorage.Update(model) == null)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
public bool Delete(CabinetBindingModel model)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
2024-05-16 04:53:14 +04:00
|
|
|
|
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
|
|
|
|
if (_cabinetStorage.Delete(model) == null)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
public List<CabinetViewModel>? ReadList(CabinetSearchModel? model)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation(
|
2024-05-16 04:53:14 +04:00
|
|
|
|
"ReadList. Id: {Id}, Room: {Room}, Building: " +
|
|
|
|
|
"{Building}.", model?.Id, model?.Room, model?.Building);
|
|
|
|
|
var list = model == null ? _cabinetStorage.GetFullList() :
|
|
|
|
|
_cabinetStorage.GetFilteredList(model);
|
2024-05-13 09:00:08 +04:00
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
public CabinetViewModel? ReadElement(CabinetSearchModel model)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation(
|
2024-05-16 04:53:14 +04:00
|
|
|
|
"ReadElemnt. Id: {Id}, Room: {Room}, Building: " +
|
|
|
|
|
"{Building}.", model?.Id, model?.Room, model?.Building);
|
|
|
|
|
var element = _cabinetStorage.GetElement(model);
|
2024-05-13 09:00:08 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id: {Id}",
|
|
|
|
|
element.Id);
|
2024-05-13 09:00:08 +04:00
|
|
|
|
return element;
|
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
private void CheckModel(CabinetBindingModel model,
|
2024-05-13 09:00:08 +04:00
|
|
|
|
bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
if (model.Id <= 0)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(
|
2024-05-16 04:53:14 +04:00
|
|
|
|
"Идентификатор должен быть больше 0",
|
|
|
|
|
nameof(model.Id));
|
2024-05-13 09:00:08 +04:00
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Room))
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(
|
2024-05-16 04:53:14 +04:00
|
|
|
|
"Отсутствует номер/название кабинета",
|
|
|
|
|
nameof(model.Room));
|
2024-05-13 09:00:08 +04:00
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
if (model.Building < 1)
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(
|
2024-05-16 04:53:14 +04:00
|
|
|
|
"Номер корпуса должен быть больше 0",
|
|
|
|
|
nameof(model.Id));
|
2024-05-13 09:00:08 +04:00
|
|
|
|
}
|
2024-05-16 04:53:14 +04:00
|
|
|
|
_logger.LogInformation(
|
|
|
|
|
"Id: {Id}, Room: {Room}, Building: " +
|
|
|
|
|
"{Building}.", model?.Id, model?.Room, model?.Building);
|
|
|
|
|
var elementByRoom = _cabinetStorage.GetElement(
|
|
|
|
|
new CabinetSearchModel
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
2024-05-16 04:53:14 +04:00
|
|
|
|
Room = model.Room,
|
2024-05-13 09:00:08 +04:00
|
|
|
|
});
|
2024-05-16 04:53:14 +04:00
|
|
|
|
var elementByBuilding = _cabinetStorage.GetElement(
|
|
|
|
|
new CabinetSearchModel
|
2024-05-13 09:00:08 +04:00
|
|
|
|
{
|
2024-05-16 04:53:14 +04:00
|
|
|
|
Building = model.Building,
|
2024-05-13 09:00:08 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|