2023-03-28 21:24:52 +04:00
|
|
|
|
using BeautySaloonContracts.BindingModels;
|
|
|
|
|
using BeautySaloonContracts.SearchModels;
|
|
|
|
|
using BeautySaloonContracts.StoragesContracts;
|
|
|
|
|
using BeautySaloonContracts.ViewModels;
|
2023-03-29 20:00:53 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-03-28 21:24:52 +04:00
|
|
|
|
|
|
|
|
|
namespace BeautySaloonDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class EmployeeStorage : IEmployeeStorage
|
|
|
|
|
{
|
|
|
|
|
public EmployeeViewModel? Delete(EmployeeBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
var element = context.Employees
|
2023-03-29 20:00:53 +04:00
|
|
|
|
.Include(x => x.Position)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Employees.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
return context.Employees
|
2023-03-29 20:00:53 +04:00
|
|
|
|
.Include(x => x.Position)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)?
|
|
|
|
|
.GetViewModel;
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Phone))
|
|
|
|
|
return context.Employees
|
2023-03-29 20:00:53 +04:00
|
|
|
|
.Include(x => x.Position)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.FirstOrDefault(x => x.Phone.Equals(model.Phone))?
|
|
|
|
|
.GetViewModel;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
|
|
|
|
|
{
|
2023-03-29 20:00:53 +04:00
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
if (string.IsNullOrEmpty(model.Surname) && !model.Id.HasValue)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2023-03-29 20:00:53 +04:00
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
return context.Employees
|
|
|
|
|
.Include(x => x.Position)
|
|
|
|
|
.Where(x => x.Id.Equals(model.Id))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-03-28 21:24:52 +04:00
|
|
|
|
return context.Employees
|
2023-03-29 20:00:53 +04:00
|
|
|
|
.Include(x => x.Position)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.Where(x => x.Surname.Contains(model.Surname))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<EmployeeViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
return context.Employees
|
2023-03-29 20:00:53 +04:00
|
|
|
|
.Include(x => x.Position)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-03-28 21:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmployeeViewModel? Insert(EmployeeBindingModel model)
|
|
|
|
|
{
|
2023-03-29 20:00:53 +04:00
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
model.Id = context.Employees.Count() > 0 ? context.Employees.Max(x => x.Id) + 1 : 1;
|
2023-03-28 21:24:52 +04:00
|
|
|
|
var newEmployee = Employee.Create(model);
|
|
|
|
|
if (newEmployee == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Employees.Add(newEmployee);
|
|
|
|
|
context.SaveChanges();
|
2023-03-29 20:00:53 +04:00
|
|
|
|
return context.Employees
|
|
|
|
|
.Include(x => x.Position)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newEmployee.Id)?
|
|
|
|
|
.GetViewModel;
|
2023-03-28 21:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmployeeViewModel? Update(EmployeeBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new NewdbContext();
|
|
|
|
|
var client = context.Employees
|
2023-03-29 20:00:53 +04:00
|
|
|
|
.Include(x => x.Position)
|
2023-03-28 21:24:52 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
client.Update(model);
|
|
|
|
|
context.SaveChanges();
|
2023-03-29 20:00:53 +04:00
|
|
|
|
return context.Employees
|
|
|
|
|
.Include(x => x.Position)
|
|
|
|
|
.FirstOrDefault(x => x.Id == client.Id)?
|
|
|
|
|
.GetViewModel;
|
2023-03-28 21:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|