CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/EmployeeStorage.cs

82 lines
2.7 KiB
C#
Raw Permalink 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-27 17:35:14 +04:00
using Microsoft.EntityFrameworkCore;
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
{
using var context = new Database();
2024-05-27 17:35:14 +04:00
if (model.ID.HasValue) {
return context.Employees
.FirstOrDefault(x => model.ID.HasValue && x.ID == model.ID)?.GetViewModel;
}
else if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password)) {
return context.Employees
.FirstOrDefault(x => (x.Login == model.Login && x.Password == model.Password))
?.GetViewModel;
}
return new();
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
}
}
}