SUBD_SushiBar/SushiBar/SushiBarBusinessLogic/BusinessLogics/TaskLogic.cs

126 lines
3.3 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 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;
}
private void CheckModel(TaskBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
}
public void ClearEntity()
{
_taskStorage.ClearEntity();
}
}
}