SUBD/CarRentBusinessLogic/BusinessLogics/BranchLogic.cs
2023-04-26 22:45:20 +04:00

114 lines
2.6 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 CarRentContracts.BindingModels;
using CarRentContracts.BusinessLogicContracts;
using CarRentContracts.SearchModels;
using CarRentContracts.StoragesContracts;
using CarRentContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRentBusinessLogic.BusinessLogics
{
public class BranchLogic : IBranchLogic
{
private readonly IBranchStorage _branchStorage;
public BranchLogic(IBranchStorage branchStorage)
{
_branchStorage = branchStorage;
}
public void BranchInsertList(int num)
{
_branchStorage.BranchInsertList(num);
}
public List<BranchViewModel>? ReadList(BranchSearchModel? model)
{
var list = model == null ? _branchStorage.GetFullList() :
_branchStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public BranchViewModel? ReadElement(BranchSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _branchStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(BranchBindingModel model)
{
CheckModel(model);
if (_branchStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(BranchBindingModel model)
{
CheckModel(model);
if (_branchStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(BranchBindingModel model)
{
CheckModel(model, false);
if (_branchStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(BranchBindingModel 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 (string.IsNullOrEmpty(model.Address))
{
throw new ArgumentNullException("Нет адреса филиала",
nameof(model.Address));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("Нет номера телефона филиала",
nameof(model.PhoneNumber));
}
var element = _branchStorage.GetElement(
new BranchSearchModel
{
Name = model.Name
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Филиал с таким названием уже есть");
}
}
}
}