SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonDatabaseImplement/Implements/EmployeeStorage.cs

87 lines
2.8 KiB
C#

using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonDatabaseImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
public EmployeeViewModel? Delete(EmployeeBindingModel model)
{
using var context = new NewdbContext();
var element = context.Employees
.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
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.Phone))
return context.Employees
.FirstOrDefault(x => x.Phone.Equals(model.Phone))?
.GetViewModel;
return null;
}
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
{
if (string.IsNullOrEmpty(model.Surname))
{
return new();
}
using var context = new NewdbContext();
return context.Employees
.Where(x => x.Surname.Contains(model.Surname))
.Select(x => x.GetViewModel)
.ToList();
}
public List<EmployeeViewModel> GetFullList()
{
using var context = new NewdbContext();
return context.Employees
.Select(x => x.GetViewModel)
.ToList();
}
public EmployeeViewModel? Insert(EmployeeBindingModel model)
{
var newEmployee = Employee.Create(model);
if (newEmployee == null)
{
return null;
}
using var context = new NewdbContext();
context.Employees.Add(newEmployee);
context.SaveChanges();
return newEmployee.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model)
{
using var context = new NewdbContext();
var client = context.Employees
.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
}
}