161 lines
3.7 KiB
C#
161 lines
3.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Microsoft.Extensions.Logging;
|
||
using SoftwareContracts.BindingModels;
|
||
using SoftwareContracts.BusinessLogicsContracts;
|
||
using SoftwareContracts.SearchModels;
|
||
using SoftwareContracts.StoragesContracts;
|
||
using SoftwareContracts.ViewModels;
|
||
using SoftwareDateModels.Enums;
|
||
|
||
namespace SoftwareBusinessLogic.Logic
|
||
{
|
||
public class TestLogic:ITestLogic
|
||
{
|
||
private readonly ITestStorage _repository;
|
||
|
||
private readonly ILogger _logger;
|
||
|
||
public TestLogic(ITestStorage repository,ILogger<TestLogic> logger)
|
||
{
|
||
_repository = repository;
|
||
_logger = logger;
|
||
}
|
||
|
||
public List<TestViewModel>? ReadList(TestSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. Model: {Model}", model);
|
||
|
||
var result = model == null
|
||
? _repository.GetFullList()
|
||
: _repository.GetFilteredList(model);
|
||
|
||
if (result == null)
|
||
{
|
||
_logger.LogWarning("ReadList: Returned null list");
|
||
}
|
||
else
|
||
{
|
||
_logger.LogInformation("ReadList: Returned {Count} items", result.Count);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public TestViewModel? ReadElement(TestSearchModel model)
|
||
{
|
||
_logger.LogInformation("ReadElement. Model: {Model}", model);
|
||
|
||
var result = _repository.GetElement(model);
|
||
|
||
if (result == null)
|
||
{
|
||
_logger.LogWarning("ReadElement: Issue not found");
|
||
}
|
||
else
|
||
{
|
||
_logger.LogInformation("ReadElement: Found Issue with Id: {Id}", result.Id);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public bool Create(TestBindingModel model)
|
||
{
|
||
_logger.LogInformation("Create. Model: {Model}", model);
|
||
|
||
var result = _repository.Insert(model) != null;
|
||
|
||
if (result)
|
||
{
|
||
_logger.LogInformation("Create: Test successfully created");
|
||
}
|
||
else
|
||
{
|
||
_logger.LogError("Create: Failed to create Test");
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public bool Update(TestBindingModel model)
|
||
{
|
||
_logger.LogInformation("Update. Model: {Model}", model);
|
||
|
||
var result = _repository.Update(model) != null;
|
||
|
||
if (result)
|
||
{
|
||
_logger.LogInformation("Update: Test with Id {Id} successfully updated", model.Id);
|
||
}
|
||
else
|
||
{
|
||
_logger.LogError("Update: Failed to update Test with Id {Id}", model.Id);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public bool Delete(TestBindingModel model)
|
||
{
|
||
_logger.LogInformation("Delete. Model: {Model}", model);
|
||
|
||
var result = _repository.Delete(model) != null;
|
||
|
||
if (result)
|
||
{
|
||
_logger.LogInformation("Delete: Test with Id {Id} successfully deleted", model.Id);
|
||
}
|
||
else
|
||
{
|
||
_logger.LogError("Delete: Failed to delete Test with Id {Id}", model.Id);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public bool FailedTest(TestBindingModel model)
|
||
{
|
||
_logger.LogInformation("Marking test as Failed. Test ID: {TestId}", model.Id);
|
||
return ChangeTestStatus(model, TestStatus.Провален);
|
||
}
|
||
|
||
public bool SuccessTest(TestBindingModel model)
|
||
{
|
||
_logger.LogInformation("Marking test as Success. Test ID: {TestId}", model.Id);
|
||
return ChangeTestStatus(model, TestStatus.Успешно);
|
||
}
|
||
|
||
private bool ChangeTestStatus(TestBindingModel model, TestStatus newStatus)
|
||
{
|
||
var orderView = _repository.GetElement(new TestSearchModel { Id = model.Id });
|
||
|
||
if (orderView == null)
|
||
{
|
||
_logger.LogWarning("Test status change failed");
|
||
return false;
|
||
}
|
||
|
||
model.TestStatus = newStatus;
|
||
|
||
if (_repository.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Test status change failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public TestStatisticsViewModel GetStatistics(DateTime from, DateTime to)
|
||
{
|
||
_logger.LogInformation("GetStatistics called. Date range: {From} - {To}", from, to);
|
||
|
||
var result = _repository.GetStatistics(from, to);
|
||
|
||
return result;
|
||
}
|
||
}
|
||
}
|