96 lines
3.3 KiB
C#
Raw Normal View History

2024-05-20 05:17:39 +04:00
using DeviceContracts.BindingModels;
using DeviceContracts.SearchModels;
using DeviceContracts.StoragesContracts;
using DeviceContracts.ViewModels;
using DeviceDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeviceDatabaseImplement.Implements
{
public class StaffStorage : IStaffStorage
{
public List<StaffViewModel> GetFullList()
{
using var context = new DeviceDatabase();
return context.Staff.Select(x => x.GetViewModel).ToList();
}
public List<StaffViewModel> GetFilteredList(StaffSearchModel model)
{
if (string.IsNullOrEmpty(model.FullName) &&
string.IsNullOrEmpty(model.Department) &&
string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new DeviceDatabase();
return context.Staff
.Where(x => (string.IsNullOrEmpty(model.FullName) ||
x.FullName == model.FullName) &&
(string.IsNullOrEmpty(model.Department) ||
x.Department == model.Department) &&
(string.IsNullOrEmpty(model.Email) ||
x.Email == model.Email))
.Select(x => x.GetViewModel).ToList();
}
public StaffViewModel? GetElement(StaffSearchModel model)
{
if (string.IsNullOrEmpty(model.FullName) &&
string.IsNullOrEmpty(model.Department) &&
string.IsNullOrEmpty(model.Email))
{
return null;
}
using var context = new DeviceDatabase();
return context.Staff
.FirstOrDefault(x => (string.IsNullOrEmpty(model.FullName) ||
x.FullName == model.FullName) &&
(string.IsNullOrEmpty(model.Department) ||
x.Department == model.Department) &&
(string.IsNullOrEmpty(model.Email) || x.Email == model.Email))
?.GetViewModel;
}
public StaffViewModel? Insert(StaffBindingModel model)
{
var newStaff = Staff.Create(model);
if (newStaff == null)
{
return null;
}
using var context = new DeviceDatabase();
context.Staff.Add(newStaff);
context.SaveChanges();
return newStaff.GetViewModel;
}
public StaffViewModel? Update(StaffBindingModel model)
{
using var context = new DeviceDatabase();
var staff = context.Staff
.FirstOrDefault(x => x.Id == model.Id);
if (staff == null)
{
return null;
}
staff.Update(model);
context.SaveChanges();
return staff.GetViewModel;
}
public StaffViewModel? Delete(StaffBindingModel model)
{
using var context = new DeviceDatabase();
var element = context.Staff
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Staff.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}