CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/EmployeeStorage.cs

79 lines
2.5 KiB
C#
Raw Normal View History

2024-04-29 01:55:20 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
2024-05-25 13:06:38 +04:00
2024-04-29 01:55:20 +04:00
namespace ElectronicsShopDataBaseImplement.Implements
{
2024-05-25 13:06:38 +04:00
public class EmployeeStorage : IEmployeeStorage
2024-04-29 01:55:20 +04:00
{
2024-05-26 17:47:24 +04:00
public EmployeeViewModel? Insert(EmployeeBindingModel model) {
var newComponent = Employee.Create(model);
if (newComponent == null) {
return null;
}
using var context = new Database();
context.Employees.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model) {
using var context = new Database();
var component = context.Employees.FirstOrDefault(x => x.ID == model.ID);
if (component == null) {
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public EmployeeViewModel? Delete(EmployeeBindingModel model)
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
2024-05-25 13:06:38 +04:00
var element = context.Employees.FirstOrDefault(rec => rec.ID == model.ID);
2024-04-29 01:55:20 +04:00
if (element != null)
{
2024-05-25 13:06:38 +04:00
context.Employees.Remove(element);
2024-04-29 01:55:20 +04:00
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
2024-05-25 13:06:38 +04:00
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
2024-04-29 01:55:20 +04:00
{
if (string.IsNullOrEmpty(model.Login) && !model.ID.HasValue)
{
return null;
}
using var context = new Database();
2024-05-26 17:47:24 +04:00
return context.Employees
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) &&
x.EmployeeFIO == model.EmployeFIO ||
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
2024-04-29 01:55:20 +04:00
}
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
2024-04-29 01:55:20 +04:00
{
if (string.IsNullOrEmpty(model.Login))
{
return new();
}
using var context = new Database();
2024-05-26 17:47:24 +04:00
return context.Employees.Where(x => x.Login
.Contains(model.Login))
.Select(x => x.GetViewModel).ToList();
2024-04-29 01:55:20 +04:00
}
2024-05-25 13:06:38 +04:00
public List<EmployeeViewModel> GetFullList()
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
2024-05-25 13:06:38 +04:00
return context.Employees.Select(x => x.GetViewModel).ToList();
2024-04-29 01:55:20 +04:00
}
}
}