CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/EmployeeLogic.cs

103 lines
4.0 KiB
C#
Raw Normal View History

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
2024-04-29 01:28:41 +04:00
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-05-26 17:47:24 +04:00
namespace ElectronicsShopBusinessLogic.BusinessLogic {
public class EmployeeLogic : IEmployeeLogic {
private readonly ILogger _logger;
private readonly IEmployeeStorage _storage;
2024-05-26 17:47:24 +04:00
public EmployeeLogic(ILogger<EmployeeLogic> loger, IEmployeeStorage storage) {
_logger = loger;
_storage = storage;
}
public bool Create(EmployeeBindingModel model) {
CheckModel(model);
2024-05-26 17:47:24 +04:00
if (_storage.Insert(model) == null) {
_logger.LogWarning("Create operation failed");
return false;
}
return true;
}
2024-05-26 17:47:24 +04:00
public bool Delete(EmployeeBindingModel model) {
CheckModel(model, false);
2024-04-28 23:36:49 +04:00
_logger.LogInformation($"Delete.ID:{model.ID}");
2024-05-26 17:47:24 +04:00
if (_storage.Delete(model) == null) {
2024-04-30 21:31:36 +04:00
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
2024-05-27 17:35:14 +04:00
public EmployeeViewModel? ReadElement(EmployeeSearchModel model) {
2024-05-26 17:47:24 +04:00
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
2024-04-30 21:31:36 +04:00
_logger.LogInformation($"ReadElement.Login:{model.Login}.ID:{model.ID}");
var element = _storage.GetElement(model);
2024-05-26 17:47:24 +04:00
if (element == null) {
2024-04-30 21:31:36 +04:00
_logger.LogWarning("ReadElement. element not fount");
return null;
}
2024-04-30 21:31:36 +04:00
_logger.LogInformation($"ReadElement.find.ID:{element.ID}");
2024-04-29 01:28:41 +04:00
return element;
}
2024-05-26 17:47:24 +04:00
public List<EmployeeViewModel>? ReadList(EmployeeSearchModel model) {
2024-04-30 21:31:36 +04:00
_logger.LogInformation($"ReadList. ClientID:{model?.ID}");
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
2024-05-26 17:47:24 +04:00
if (list == null) {
2024-04-30 21:31:36 +04:00
_logger.LogWarning("ReadList. return null list");
return null;
}
2024-04-30 21:31:36 +04:00
_logger.LogInformation($"ReadList.Count:{list.Count}");
2024-04-29 01:28:41 +04:00
return list;
}
2024-05-26 17:47:24 +04:00
public bool Update(EmployeeBindingModel model) {
CheckModel(model);
2024-05-26 17:47:24 +04:00
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2024-05-26 17:47:24 +04:00
private void CheckModel(EmployeeBindingModel model, bool withParams = true) {
if (model == null) {
throw new ArgumentNullException(nameof(model));
}
2024-05-26 17:47:24 +04:00
if (!withParams) {
return;
}
2024-05-26 17:47:24 +04:00
if (string.IsNullOrEmpty(model.Login)) {
throw new ArgumentNullException("Нет логина сотрудника", nameof(model.Login));
}
2024-05-26 17:47:24 +04:00
if (string.IsNullOrEmpty(model.EmployeeFIO)) {
throw new ArgumentNullException("Нет имени сотрудника", nameof(model.EmployeeFIO));
}
2024-05-26 17:47:24 +04:00
if (string.IsNullOrEmpty(model.Password)) {
throw new ArgumentNullException("Нет пароля сотрудника", nameof(model.Password));
}
_logger.LogInformation($"Client. ID:{model.ID}.ClientFIO:{model.EmployeeFIO}.Password:{model.Password}.Email:{model.Login}.");
2024-05-26 17:47:24 +04:00
var element = _storage.GetElement(new EmployeeSearchModel {
Login = model.Login
});
2024-05-27 19:12:19 +04:00
if (element != null && element.Login == model.Login) {
2024-05-26 17:47:24 +04:00
throw new InvalidOperationException("Сотрудник с таким логином уже есть");
}
}
2024-05-26 17:47:24 +04:00
}
2024-05-26 17:47:24 +04:00
}