109 lines
3.6 KiB
C#
109 lines
3.6 KiB
C#
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) &&
|
|
model.AccessLevel == 0)
|
|
{
|
|
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) && (model.AccessLevel == 0 ||
|
|
x.AccessLevel == model.AccessLevel))
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public StaffViewModel? GetElement(StaffSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.FullName) &&
|
|
string.IsNullOrEmpty(model.Department) &&
|
|
string.IsNullOrEmpty(model.Email) &&
|
|
model.AccessLevel == 0)
|
|
{
|
|
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) && (model.AccessLevel == 0 ||
|
|
x.AccessLevel == model.AccessLevel))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public StaffViewModel? Insert(StaffBindingModel model)
|
|
{
|
|
var newStaffMember = Staff.Create(model);
|
|
if (newStaffMember == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new DeviceDatabase();
|
|
context.Staff.Add(newStaffMember);
|
|
context.SaveChanges();
|
|
return newStaffMember.GetViewModel;
|
|
}
|
|
|
|
public StaffViewModel? Update(StaffBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var staffMember = context.Staff
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (staffMember == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
staffMember.Update(model);
|
|
context.SaveChanges();
|
|
return staffMember.GetViewModel;
|
|
}
|
|
|
|
public StaffViewModel? Delete(StaffBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var staffMember = context.Staff
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (staffMember != null)
|
|
{
|
|
context.Staff.Remove(staffMember);
|
|
context.SaveChanges();
|
|
return staffMember.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|