SUBD-Petrushin-Egor-PIbd-22/TaskTrackerBusinessLogics/BusinessLogic/TaskLogic.cs

107 lines
2.9 KiB
C#
Raw Normal View History

2024-05-13 14:29:34 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.BusinessLogicsContracts;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
using TaskTrackerDataModels.Enums;
namespace TaskTrackerBusinessLogics.BusinessLogic
{
public class TaskLogic : ITaskLogic
{
private readonly ITaskStorage _taskStorage;
public TaskLogic(ITaskStorage taskStorage)
{
_taskStorage = taskStorage;
}
public bool CreateTask(TaskBindingModel model)
{
CheckModel(model);
if (model.Status != MyTaskStatus.Неизвестен)
{
return false;
}
model.Status = MyTaskStatus.Новый;
if (_taskStorage.Insert(model) == null)
{
return false;
}
return true;
}
public List<TaskViewModel>? ReadList(TaskSearchModel? model)
{
var list = model == null ? _taskStorage.GetFullList() : _taskStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool TakeTaskInWork(TaskBindingModel model)
{
return ChangeStatus(model, MyTaskStatus.Выполняется);
}
public bool FinishTask(TaskBindingModel model)
{
return ChangeStatus(model, MyTaskStatus.Готово);
}
public bool DeliveryTask(TaskBindingModel model)
{
return ChangeStatus(model, MyTaskStatus.Проверено);
}
private void CheckModel(TaskBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
}
private bool ChangeStatus(TaskBindingModel model, MyTaskStatus newStatus)
{
CheckModel(model, false);
var task = _taskStorage.GetElement(new TaskSearchModel { Id = model.Id });
if (task == null)
{
return false;
}
if (task.Status + 1 != newStatus)
{
return false;
}
model.Status = newStatus;
if (model.Status == MyTaskStatus.Готово)
{
model.DateImplement = DateTime.Now;
}
else
{
model.DateImplement = task.DateImplement;
}
if (_taskStorage.Update(model) == null)
{
return false;
}
return true;
}
}
}