2023-05-17 10:48:52 +04:00

126 lines
3.5 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 Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.BusinessLogicContracts;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
namespace TaskTrackerBusinessLogic
{
public class ProjectLogic : IProjectLogic
{
private readonly ILogger _logger;
private readonly IProjectStorage _projectStorage;
public ProjectLogic(ILogger<ProjectLogic> logger, IProjectStorage
projectStorage)
{
_logger = logger;
_projectStorage = projectStorage;
}
public List<ProjectViewModel>? ReadList(ProjectSearchModel? model)
{
Stopwatch sw = new Stopwatch();
sw.Start();
_logger.LogInformation("ReadList. ProjectName:{ProjectName}.Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _projectStorage.GetFullList() :
_projectStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds);
_logger.LogInformation("Чтение проектов " + sw.Elapsed.TotalMilliseconds);
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ProjectViewModel? ReadElement(ProjectSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ProjectName:{ProjectName}. Id:{ Id}", model.Name, model.Id);
var element = _projectStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(ProjectBindingModel model)
{
CheckModel(model);
if (_projectStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ProjectBindingModel model)
{
CheckModel(model);
if (_projectStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ProjectBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_projectStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ProjectBindingModel 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.State))
{
throw new ArgumentNullException("Нет названия компонента",
nameof(model.State));
}
_logger.LogInformation("Project. ProjectName:{ProjectName}. Cost:{ Cost}. Id: { Id} ", model.Name, model.Name, model.Id);
var element = _projectStorage.GetElement(new ProjectSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}