PIbd-21_Pyatakov_KM_Markov_.../UniversityBusinessLogic/BusinessLogics/DocumentLogic.cs

102 lines
3.1 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class DocumentLogic : IDocumentLogic
{
private readonly IDocumentStorage _documentStorage;
public DocumentLogic(IDocumentStorage documentStorage)
{
_documentStorage = documentStorage;
}
public bool Create(DocumentBindingModel model)
{
CheckModel(model);
if(_documentStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(DocumentBindingModel model)
{
CheckModel(model, false);
if (_documentStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(DocumentBindingModel model)
{
CheckModel(model, false);
if (_documentStorage.Delete(model) == null)
{
return false;
}
return true;
}
public DocumentViewModel? ReadElement(DocumentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var document = _documentStorage.GetElement(model);
if (document == null)
{
return null;
}
return document;
}
public List<DocumentViewModel>? ReadList(DocumentSearchModel? model)
{
var list = model == null ? _documentStorage.GetFullList() : _documentStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
private void CheckModel(DocumentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия документа", nameof(model.Name));
}
if (model.Date > DateTime.Now)
{
throw new ArgumentNullException("Неверно указана дата", nameof(model.Name));
}
var document = _documentStorage.GetElement(new DocumentSearchModel
{
Name = model.Name,
});
if (document != null && document.Id != model.Id)
{
throw new InvalidOperationException("Приказ с таким названием уже есть");
}
}
}
}