2023-04-06 22:52:23 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2023-04-09 02:44:56 +04:00
|
|
|
|
public List<EmployeeViewModel> GetFullList()
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
return context.Employees
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-09 02:44:56 +04:00
|
|
|
|
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
2023-04-09 02:44:56 +04:00
|
|
|
|
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-04-09 02:44:56 +04:00
|
|
|
|
public EmployeeViewModel? Insert(EmployeeBindingModel model)
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
var res = Employee.Create(model);
|
|
|
|
|
if (res != null)
|
|
|
|
|
{
|
|
|
|
|
context.Employees.Add(res);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
return res?.GetViewModel;
|
|
|
|
|
}
|
2023-04-09 02:44:56 +04:00
|
|
|
|
public EmployeeViewModel? Update(EmployeeBindingModel model)
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
var res = context.Employees.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
res?.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return res?.GetViewModel;
|
|
|
|
|
}
|
2023-04-09 02:44:56 +04:00
|
|
|
|
public EmployeeViewModel? Delete(EmployeeBindingModel model)
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|