потенциально закончил бизнес логику (лучше прверить что я воообще написал)

This commit is contained in:
frog24 2024-04-28 21:33:37 +04:00
parent 48cebb7325
commit f80e7e1f8c
2 changed files with 131 additions and 1 deletions

View File

@ -0,0 +1,130 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetclinicContracts.BindingModels;
using VetclinicContracts.BusinessLogicsContracts;
using VetclinicContracts.SearchModels;
using VetclinicContracts.StoragesContracts;
using VetclinicContracts.ViewModels;
using VetclinicDataModels.Enums;
namespace VetclinicBusinessLogic.BusinessLogics
{
public class DoctorVisitLogic: IDoctorVisitLogic
{
private readonly ILogger _logger;
private readonly IDoctorVisitStorage _doctorVisitStorage;
public DoctorVisitLogic(ILogger<DoctorVisitLogic> logger, IDoctorVisitStorage doctorVisitStorage)
{
_logger = logger;
_doctorVisitStorage = doctorVisitStorage;
}
public List<DoctorVisitViewModel>? ReadList(DoctorVisitSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = model == null ? _doctorVisitStorage.GetFullList() : _doctorVisitStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public DoctorVisitViewModel? ReadElement(DoctorVisitSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _doctorVisitStorage.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 CreateDoctorVisit(DoctorVisitBindingModel model)
{
CheckModel(model);
if (model.Status != DoctorVisitStatus.Неизвестен)
{
_logger.LogWarning("Wrong DoctorVisit status. Insert operation failed");
return false;
}
model.Status = DoctorVisitStatus.Забронировано;
if (_doctorVisitStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool ChangeStatus(DoctorVisitBindingModel model, DoctorVisitStatus status)
{
CheckModel(model, false);
var element = _doctorVisitStorage.GetElement(new DoctorVisitSearchModel { Id = model.Id });
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != status - 1)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
DoctorVisitStatus oldStatus = model.Status;
model.Status = status;
/* РАЗ УЖ НЕТ ДАТЫ ОКОНЧАНИЯ ТО ЭТО НЕ НАДО?
if (model.Status == DoctorVisitStatus.Закончен)
{
model.DateImplement = DateTime.Now;
}
*/
if (_doctorVisitStorage.Update(model) == null)
{
model.Status = oldStatus;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool BeginDoctorVisit(DoctorVisitBindingModel model)
{
return ChangeStatus(model, DoctorVisitStatus.Выполняется);
}
public bool FinishDoctorVisit(DoctorVisitBindingModel model)
{
return ChangeStatus(model, DoctorVisitStatus.Закончен);
}
private void CheckModel(DoctorVisitBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.DateVisit != null)
{
throw new ArithmeticException("Дата завершения не может быть раньше даты начала");
}
_logger.LogInformation("DoctorVisit. Id:{Id}. DateVisit:{DateVisit}. ServiceId:{ServiceId}. AnimalId:{AnimalId}",
model.Id, model.DateVisit, model.ServiceId, model.AnimalId);
}
}
}

View File

@ -133,7 +133,7 @@ namespace VetclinicBusinessLogic.BusinessLogics
{
throw new ArithmeticException("Дата завершения не может быть раньше даты начала");
}
_logger.LogInformation("DrugPurchase. Id:{Id}. Sum:{Sum}. AnimalId:{AnimalId}", model.Id, model.Sum, model.AnimalId);
_logger.LogInformation("DrugPurchase. Id:{Id}. Cost:{Cost}. DrugId:{DrugId}. AnimalId:{DrugId}", model.Id, model.Cost, model.DrugId, model.AnimalId);
}
}
}