CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/EmployeeStorage.cs

80 lines
2.6 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-25 13:06:38 +04:00
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-25 13:06:38 +04:00
return context.Employees.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Login) && x.EmployeeFIO == model.EmployeFIO ||
2024-04-29 01:55:20 +04:00
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
}
2024-05-25 13:06:38 +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-25 13:06:38 +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
}
2024-05-25 13:06:38 +04:00
public EmployeeViewModel? Insert(EmployeeBindingModel model)
2024-04-29 01:55:20 +04:00
{
2024-05-25 13:06:38 +04:00
var newComponent = Employee.Create(model);
2024-04-29 01:55:20 +04:00
if (newComponent == null)
{
return null;
}
using var context = new Database();
2024-05-25 13:06:38 +04:00
context.Employees.Add(newComponent);
2024-04-29 01:55:20 +04:00
context.SaveChanges();
return newComponent.GetViewModel;
}
2024-05-25 13:06:38 +04:00
public EmployeeViewModel? Update(EmployeeBindingModel model)
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
2024-05-25 13:06:38 +04:00
var component = context.Employees.FirstOrDefault(x => x.ID == model.ID);
2024-04-29 01:55:20 +04:00
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}