добавил услугу

This commit is contained in:
Katanaa Die 2024-04-27 16:33:43 +04:00
parent f06dc8871f
commit e1902ba03a
8 changed files with 338 additions and 0 deletions

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetclinicDataModels.Models
{
public interface IServiceModel : IId
{
string ServiceName { get; }
double Price { get; }
int DoctorId { get; }
Dictionary<int, (IMedicineModel, int)> ServiceMedicines { get; }
}
}

View File

@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetclinicBusinessLogic.BusinessLogics
{
public class DoctorLogic : IDoctorLogic
{
private readonly ILogger _logger;
private readonly IDoctorStorage _doctorStorage;
public DoctorLogic(ILogger<DoctorLogic> logger, IDoctorStorage
doctorStorage)
{
_logger = logger;
_doctorStorage = doctorStorage;
}
public List<DoctorViewModel>? ReadList(DoctorSearchModel? model)
{
_logger.LogInformation("ReadList. DoctorFIO:{DoctorFIO}. Id:{ Id}", model?.DoctorFIO, model?.Id);
var list = model == null ? _doctorStorage.GetFullList() :
_doctorStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public DoctorViewModel? ReadElement(DoctorSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DoctorFIO:{DoctorFIO}.Id:{ Id}", model.DoctorFIO, model.Id);
var element = _doctorStorage.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(DoctorBindingModel model)
{
CheckModel(model);
if (_doctorStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DoctorBindingModel model)
{
CheckModel(model);
if (_doctorStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DoctorBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_doctorStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(DoctorBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.DoctorFIO))
{
throw new ArgumentNullException("Нет ФИО клиента",
nameof(model.DoctorFIO));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет Email клиента",
nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля клиента",
nameof(model.Password));
}
_logger.LogInformation("Doctor. DoctorFIO:{DoctorFIO}." +
"Email:{ Email}. Password:{ Password}. Id: { Id} ", model.DoctorFIO, model.Email, model.Password, model.Id);
var element = _doctorStorage.GetElement(new DoctorSearchModel
{
Email = model.Email,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с таким логином уже есть");
}
}
}
}

View File

@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetclinicBusinessLogic.BusinessLogics
{
public class ServiceLogic : IServiceLogic
{
private readonly ILogger _logger;
private readonly IServiceStorage _serviceStorage;
public ServiceLogic(ILogger<ServiceLogic> logger, IServiceStorage serviceStorage)
{
_logger = logger;
_serviceStorage = serviceStorage;
}
public List<ServiceViewModel>? ReadList(ServiceSearchModel? model)
{
_logger.LogInformation("ReadList. ServiceName:{ServiceName}. Id:{ Id}", model?.ServiceName, model?.Id);
var list = model == null ? _serviceStorage.GetFullList() :
_serviceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ServiceViewModel? ReadElement(ServiceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ServiceName:{ServiceName}.Id:{ Id}", model.ServiceName, model.Id);
var element = _serviceStorage.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(ServiceBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ServiceBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ServiceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_serviceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ServiceBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ServiceName))
{
throw new ArgumentNullException("Нет названия услуги",
nameof(model.ServiceName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена услуги должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Service. ServiceName:{ServiceName}. Price:{ Price }. Id: { Id}", model.ServiceName, model.Price, model.Id);
var element = _serviceStorage.GetElement(new ServiceSearchModel
{
ServiceName = model.ServiceName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Услуга с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetclinicContracts.BindingModels
{
public class ServiceBindingModel : IServiceModel
{
public int Id { get; set; }
public string ServiceName { get; set; } = string.Empty;
public double Price { get; set; }
public int DoctorId { get; set; }
public Dictionary<int, (IMedicineModel, int)> ServiceMedicines { get; set; } = new();
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetclinicContracts.BindingModels;
namespace VetclinicContracts.BusinessLogicsContracts
{
public interface IServiceLogic
{
List<ServiceViewModel>? ReadList(ServiceSearchModel? model);
ServiceViewModel? ReadElement(ServiceSearchModel model);
bool Create(ServiceBindingModel model);
bool Update(ServiceBindingModel model);
bool Delete(ServiceBindingModel model);
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetclinicContracts.SearchModels
{
public class ServiceSearchModel
{
public int? Id { get; set; }
public string? ServiceName { get; set; } = string.Empty;
public int? DoctorId { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetclinicContracts.BindingModels;
using VetclinicContracts.SearchModels;
namespace VetclinicContracts.StoragesContracts
{
public interface IServiceStorage
{
List<ServiceViewModel> GetFullList();
List<ServiceViewModel> GetFilteredList(ServiceSearchModel model);
ServiceViewModel? GetElement(ServiceSearchModel model);
ServiceViewModel? Insert(ServiceBindingModel model);
ServiceViewModel? Update(ServiceBindingModel model);
ServiceViewModel? Delete(ServiceBindingModel model);
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VetclinicContracts.ViewModels
{
public class ServiceViewModel : IServiceModel
{
public int Id { get; set; }
[DisplayName("Название услуги")]
public string ServiceName { get; set; } = string.Empty;
[DisplayName("Цена услуги")]
public double Price { get; set; }
[DisplayName("Доктор")]
public string DcotorFIO { get; set; } = string.Empty;
public int DoctorId { get; set; }
public Dictionary<int, (IMedicineModel, int)> ServiceMedicines { get; set; } = new();
}
}