ISEbd-21_Tukaeva_A_A_School/School/SchoolDatabaseImplements/Implements/EmployeeStorage.cs

90 lines
2.5 KiB
C#
Raw Normal View History

using SchoolContracts.BindingModel;
using SchoolContracts.SearchModel;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolDatabaseImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
public List<ExpenseViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Employees
.Select(x => x.GetViewModel)
.ToList();
}
public List<ExpenseViewModel> GetFilteredList(EmployeeSearchModel model)
{
if (model == null)
{
return null;
}
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
if (model.EmployeeName != null)
{
using var context = new SchoolDatabase();
return context.Employees
.Where(rec => rec.EmployeeName.Contains(model.EmployeeName))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public ExpenseViewModel? GetElement(EmployeeSearchModel model)
{
using var context = new SchoolDatabase();
if (model.Id.HasValue)
return context.Employees.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
if (model.EmployeeName != null && model.EmployeePassword != null)
return context.Employees
.FirstOrDefault(x => x.EmployeeName.Equals(model.EmployeeName)
&& x.EmployeePassword.Equals(model.EmployeePassword))
?.GetViewModel;
if (model.EmployeeName != null)
return context.Employees.FirstOrDefault(x => x.EmployeeName.Contains(model.EmployeeName))?.GetViewModel;
return null;
}
public ExpenseViewModel? Insert(EmployeeBindingModel model)
{
using var context = new SchoolDatabase();
var res = Employee.Create(model);
if (res != null)
{
context.Employees.Add(res);
context.SaveChanges();
}
return res?.GetViewModel;
}
public ExpenseViewModel? Update(EmployeeBindingModel model)
{
using var context = new SchoolDatabase();
var res = context.Employees.FirstOrDefault(x => x.Id == model.Id);
res?.Update(model);
context.SaveChanges();
return res?.GetViewModel;
}
public ExpenseViewModel? Delete(EmployeeBindingModel model)
{
using var context = new SchoolDatabase();
var res = context.Employees.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
context.Employees.Remove(res);
context.SaveChanges();
}
return res?.GetViewModel;
}
}
}