SUBD_SushiBar/SushiBar/SushiBarBusinessLogic/BusinessLogics/TaskLogic.cs

121 lines
3.2 KiB
C#
Raw Normal View History

2024-03-26 20:35:34 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarBusinessLogic.BusinessLogics
{
public class TaskLogic : ITaskLogic
{
private readonly ITaskStorage _taskStorage;
public TaskLogic(ITaskStorage taskStorage)
{
_taskStorage = taskStorage;
}
public TaskViewModel? ReadElement(TaskSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _taskStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<TaskViewModel>? ReadList(TaskSearchModel? model)
{
var list = model == null ? _taskStorage.GetFullList() : _taskStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Create(TaskBindingModel model)
{
CheckModel(model);
if (_taskStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(TaskBindingModel model)
{
CheckModel(model, false);
if (_taskStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Update(TaskBindingModel model)
{
CheckModel(model);
if (_taskStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool DeliveryTask(TaskBindingModel model)
{
return StatusUpdate(model, SushiBarDataModels.Enum.TaskStatus.Выдан);
}
public bool FinishTask(TaskBindingModel model)
{
return StatusUpdate(model, SushiBarDataModels.Enum.TaskStatus.Готов);
}
public bool TakeTaskInWork(TaskBindingModel model)
{
return StatusUpdate(model, SushiBarDataModels.Enum.TaskStatus.Выполняется);
}
private bool StatusUpdate(TaskBindingModel model, SushiBarDataModels.Enum.TaskStatus newStatus)
{
if (model.Status + 1 != newStatus)
{
return false;
}
model.Status = newStatus;
if (_taskStorage.Update(model) == null)
{
model.Status--;
return false;
}
return true;
}
2024-03-26 20:35:34 +04:00
private void CheckModel(TaskBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
}
}
}