ISEbd-21 Melnikov I. O. Lab Work 06 Base #18

Closed
Igor-Melnikov wants to merge 5 commits from lab6 into lab5
21 changed files with 512 additions and 11 deletions
Showing only changes of commit 803eb06fc7 - Show all commits

View File

@ -0,0 +1,115 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
{
public class ImplementerLogic : IImplementerLogic
{
private readonly ILogger _logger;
private readonly IImplementerStorage _implementerStorage;
public ImplementerLogic(ILogger<ImplementerLogic> logger, IImplementerStorage implementerStorage)
{
_logger = logger;
_implementerStorage = implementerStorage;
}
public List<ImplementerViewModel>? ReadList(ImplementerSearchModel? model)
{
_logger.LogInformation("Readlist. Id: {Id}", model?.Id);
var list = model == null ? _implementerStorage.GetFullList() : _implementerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("Readlist. Count: {Count}", list.Count);
return list;
}
public ImplementerViewModel? ReadElement(ImplementerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _implementerStorage.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(ImplementerBindingModel model)
{
CheckModel(model);
if (_implementerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ImplementerBindingModel model)
{
CheckModel(model);
if (_implementerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ImplementerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_implementerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ImplementerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.WorkExperience <= 0)
{
throw new ArgumentException("Опыт работы не должен быть меньше единицы", nameof(model.WorkExperience));
}
if (model.Qualification <= 0)
{
throw new ArgumentException("Квалификация не должна быть отрицательной", nameof(model.Qualification));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Заполните пароль исполнителя", nameof(model.ImplementerFIO));
}
if (string.IsNullOrEmpty(model.ImplementerFIO))
{
throw new ArgumentNullException("Заполните ФИО исполнителя", nameof(model.ImplementerFIO));
}
_logger.LogInformation("Implementer. Id: {Id}, FIO: {FIO}", model.Id, model.ImplementerFIO);
var element = _implementerStorage.GetElement(new ImplementerSearchModel
{
ImplementerFIO = model.ImplementerFIO,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Исполнитель с таким значением ФИО уже есть");
}
}
}
}

View File

@ -29,7 +29,23 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics
_logger.LogInformation("Readlist. Count: {Count}", list.Count);
return list;
}
public bool SetNewStatus(OrderBindingModel model, OrderStatus newstatus)
public OrderViewModel? ReadElement(OrderSearchModel? model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _orderStorage.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 SetNewStatus(OrderBindingModel model, OrderStatus newstatus)
{
CheckModel(model, false);
OrderViewModel? vm = _orderStorage.GetElement(new()

View File

@ -0,0 +1,13 @@
using BlacksmithWorkshopDataModels.Models;
namespace BlacksmithWorkshopContracts.BindingModels
{
public class ImplementerBindingModel : IImplementerModel
{
public int Id { get; set; }
public string ImplementerFIO { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public int WorkExperience { get; set; }
public int Qualification { get; set; }
}
}

View File

@ -13,5 +13,6 @@ namespace BlacksmithWorkshopContracts.BindingModels
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }
public int ClientId { get; set; }
public int ImplementerId { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
namespace BlacksmithWorkshopContracts.BusinessLogicsContracts
{
public interface IImplementerLogic
{
List<ImplementerViewModel>? ReadList(ImplementerSearchModel? model);
ImplementerViewModel? ReadElement(ImplementerSearchModel model);
bool Create(ImplementerBindingModel model);
bool Update(ImplementerBindingModel model);
bool Delete(ImplementerBindingModel model);
}
}

View File

@ -7,6 +7,7 @@ namespace BlacksmithWorkshopContracts.BusinessLogicContracts
public interface IOrderLogic
{
List<OrderViewModel>? ReadList(OrderSearchModel? model);
OrderViewModel? ReadElement (OrderSearchModel? model);
bool CreateOrder(OrderBindingModel model);
bool TakeOrderInWork(OrderBindingModel model);
bool FinishOrder(OrderBindingModel model);

View File

@ -0,0 +1,9 @@
namespace BlacksmithWorkshopContracts.SearchModels
{
public class ImplementerSearchModel
{
public int? Id { get; set; }
public string? ImplementerFIO { get; set; }
public string? Password { get; set; }
}
}

View File

@ -1,4 +1,6 @@
namespace BlacksmithWorkshopContracts.SearchModels
using BlacksmithWorkshopDataModels.Enums;
namespace BlacksmithWorkshopContracts.SearchModels
{
public class OrderSearchModel
{
@ -6,5 +8,7 @@
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? ClientId { get; set; }
public OrderStatus? Status { get; set; }
public int? ImplementerId { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
namespace BlacksmithWorkshopContracts.StoragesContracts
{
public interface IImplementerStorage
{
List<ImplementerViewModel> GetFullList();
List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model);
ImplementerViewModel? GetElement(ImplementerSearchModel model);
ImplementerViewModel? Insert(ImplementerBindingModel model);
ImplementerViewModel? Update(ImplementerBindingModel model);
ImplementerViewModel? Delete(ImplementerBindingModel model);
}
}

View File

@ -0,0 +1,18 @@
using BlacksmithWorkshopDataModels.Models;
using System.ComponentModel;
namespace BlacksmithWorkshopContracts.ViewModels
{
public class ImplementerViewModel : IImplementerModel
{
public int Id { get; set; }
[DisplayName("Имя")]
public string ImplementerFIO { get; set; } = string.Empty;
[DisplayName("Пароль")]
public string Password { get; set; } = string.Empty;
[DisplayName("Опыт")]
public int WorkExperience { get; set; }
[DisplayName("Квалификация")]
public int Qualification { get; set; }
}
}

View File

@ -24,5 +24,8 @@ namespace BlacksmithWorkshopContracts.ViewModels
public DateTime? DateImplement { get; set; }
[DisplayName("Клиент")]
public string ClientFIO { get; set; } = string.Empty;
}
public int ImplementerId { get; set; }
[DisplayName("Исполнитель")]
public string ImplementerFIO { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,10 @@
namespace BlacksmithWorkshopDataModels.Models
{
public interface IImplementerModel : IId
{
string ImplementerFIO { get; }
string Password { get; }
int WorkExperience { get; }
int Qualification { get; }
}
}

View File

@ -11,5 +11,6 @@ namespace BlacksmithWorkshopDataModels.Models
OrderStatus Status { get; }
DateTime DateCreate { get; }
DateTime? DateImplement { get; }
int ImplementerId { get; }
}
}

View File

@ -25,5 +25,6 @@ namespace BlacksmithWorkshopDatabaseImplement
public virtual DbSet<ManufactureComponent> ManufactureComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { set; get; }
public virtual DbSet<Implementer> Implementers { set; get; }
}
}

View File

@ -0,0 +1,87 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDatabaseImplement.Models;
namespace BlacksmithWorkshopDatabaseImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
public List<ImplementerViewModel> GetFullList()
{
using var context = new BlacksmithWorkshopDatabase();
return context.Implementers.Select(x => x.GetViewModel).ToList();
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.ImplementerFIO))
{
return new();
}
using var context = new BlacksmithWorkshopDatabase();
if (model.Id.HasValue)
{
return context.Implementers
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Implementers
.Where(x => x.ImplementerFIO == model.ImplementerFIO)
.Select(x => x.GetViewModel)
.ToList();
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
if (!model.Id.HasValue && (string.IsNullOrEmpty(model.ImplementerFIO) || string.IsNullOrEmpty(model.Password)))
{
return new();
}
using var context = new BlacksmithWorkshopDatabase();
if (model.Id.HasValue)//Сначала ищем по Id
{
return context.Implementers
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
return context.Implementers//Затем по логину и паролю
.FirstOrDefault(x => x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password)?.GetViewModel;
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var newImplementer = Implementer.Create(model);
if (newImplementer == null)
{
return null;
}
context.Implementers.Add(newImplementer);
context.SaveChanges();
return newImplementer.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (implementer == null)
{
return null;
}
implementer.Update(model);
context.SaveChanges();
return implementer.GetViewModel;
}
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (implementer == null)
{
return null;
}
context.Implementers.Remove(implementer);
context.SaveChanges();
return implementer.GetViewModel;
}
}
}

View File

@ -14,6 +14,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
using var context = new BlacksmithWorkshopDatabase();
List<OrderViewModel> orderList = context.Orders
.Include(x => x.Client)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
foreach (var order in orderList)
@ -37,7 +38,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
orderList = context.Orders
.Where(x => x.Id == model.Id)
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.DateFrom.HasValue && model.DateTo.HasValue) //если у модели нет Id, ищем по датам
@ -45,17 +47,28 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
orderList = context.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Include(x => x.Client)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ClientId.HasValue)
else if (model.ClientId.HasValue)//далее ищем по клиенту
{
orderList = context.Orders
.Where(x => x.ClientId == model.ClientId)
.Include(x => x.Client)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.Status.HasValue)//далее ищем по статусу
{
orderList = context.Orders
.Where(x => x.Status == model.Status)
.Include(x => x.Client)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
}
foreach (var order in orderList)
{
string manufactureName = context.Manufactures
@ -66,13 +79,22 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
if (!model.Id.HasValue && !model.ImplementerId.HasValue)
{
return null;
}
using var context = new BlacksmithWorkshopDatabase();
return context.Orders
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
if (model.Id.HasValue)//Сначала ищем по Id
{
return context.Orders
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
if (model.ImplementerId.HasValue)
{
return context.Orders
.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel;
}
return null;
}
public OrderViewModel? Insert(OrderBindingModel model)
{

View File

@ -0,0 +1,58 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BlacksmithWorkshopDatabaseImplement.Models
{
public class Implementer : IImplementerModel
{
public int Id { get; private set; }
[Required]
public string ImplementerFIO { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
[Required]
public int WorkExperience { get; private set; }
[Required]
public int Qualification { get; private set; }
//Привязываем исполнителя к заказам один ко многим
[ForeignKey("ImplementerId")]
public virtual List<Order> Orders { get; private set; } = new();
public static Implementer? Create(ImplementerBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Password = model.Password,
Qualification = model.Qualification,
ImplementerFIO = model.ImplementerFIO,
WorkExperience = model.WorkExperience,
};
}
public void Update(ImplementerBindingModel model)
{
if (model == null)
{
return;
}
Password = model.Password;
Qualification = model.Qualification;
ImplementerFIO = model.ImplementerFIO;
WorkExperience = model.WorkExperience;
}
public ImplementerViewModel GetViewModel => new()
{
Id = Id,
Password = Password,
Qualification = Qualification,
ImplementerFIO = ImplementerFIO,
WorkExperience = WorkExperience,
};
}
}

View File

@ -24,6 +24,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
public int ClientId { get; private set; }
[Required]
public virtual Client? Client { get; private set; }
[Required]
public int ImplementerId { get; private set; }
public virtual Implementer? Implementer { get; private set; }
public static Order? Create(OrderBindingModel model)
{
if (model == null)
@ -39,7 +42,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ClientId = model.ClientId
ClientId = model.ClientId,
ImplementerId = model.ImplementerId
};
}
public static Order Create(OrderViewModel model)
@ -77,7 +81,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
DateCreate = DateCreate,
DateImplement = DateImplement,
ClientId = ClientId,
ClientFIO = Client?.ClientFIO ?? string.Empty
};
ClientFIO = Client?.ClientFIO ?? string.Empty,
ImplementerId = ImplementerId,
ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty
};
}
}

View File

@ -16,6 +16,8 @@ namespace BlacksmithWorkshopFileImplement.Models
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int ClientId { get; private set; }
//TODO
public int ImplementerId => throw new NotImplementedException();
public static Order? Create(OrderBindingModel? model)
{
if (model == null)

View File

@ -16,6 +16,8 @@ namespace BlacksmithWorkshopListImplement.Models
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int ClientId { get; private set; }
//TODO
public int ImplementerId => throw new NotImplementedException();
public static Order? Create(OrderBindingModel? model)
{
if (model == null)

View File

@ -0,0 +1,101 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.BusinessLogicContracts;
using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Enums;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Mvc;
namespace BlacksmithWorkshopRestAPI.Controllers
{
public class ImplementerController : Controller
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly IImplementerLogic _logic;
public ImplementerController(IOrderLogic order, IImplementerLogic logic, ILogger<ImplementerController> logger)
{
_logger = logger;
_order = order;
_logic = logic;
}
[HttpGet]
public ImplementerViewModel? Login(string login, string password)
{
try
{
return _logic.ReadElement(new ImplementerSearchModel
{
ImplementerFIO = login,
Password = password
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка авторизации сотрудника");
throw;
}
}
[HttpGet]
public List<OrderViewModel>? GetNewOrders()
{
try
{
return _order.ReadList(new OrderSearchModel
{
Status = OrderStatus.Принят
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения новых заказов");
throw;
}
}
[HttpGet]
public OrderViewModel? GetImplementerOrder(int implementerId)
{
try
{
return _order.ReadElement(new OrderSearchModel
{
ImplementerId = implementerId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения текущего заказа исполнителя");
throw;
}
}
[HttpPost]
public void TakeOrderInWork(OrderBindingModel model)
{
try
{
_order.TakeOrderInWork(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка перевода заказа с номером {Id} в работу", model.Id);
throw;
}
}
[HttpPost]
public void FinishOrder(OrderBindingModel model)
{
try
{
_order.FinishOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка отметки о готовности заказа с номером {Id}", model.Id);
throw;
}
}
}
}