PIbd-42_Kashin_M.I_CPO_Cour.../EmployeeManagmentDataBaseImplement/Implements/EmployeeStorage.cs

86 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EmployeeManagmentDataBaseImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
public List<Employee> GetFullList()
{
using (var context = new DatabaseContext())
{
return context.Employees.ToList();
}
}
public List<Employee> GetFilteredList(EmployeeSearchModel searchModel)
{
using (var context = new DatabaseContext())
{
return context.Employees
.Where(e => searchModel.Name != null && e.Name.Contains(searchModel.Name))
.ToList();
}
}
public Employee? GetElement(EmployeeSearchModel searchModel)
{
using (var context = new DatabaseContext())
{
return context.Employees.FirstOrDefault(e => e.Id == searchModel.Id);
}
}
public void Insert(EmployeeBindingModel model)
{
using (var context = new DatabaseContext())
{
var newEmployee = new Employee
{
Name = model.Name,
StartJob = model.StartJob,
EndJob = model.EndJob,
PartTimeJob = model.PartTimeJob,
Bid = model.Bid,
PhysicalPersonsId = model.PhysicalPersonsId
};
context.Employees.Add(newEmployee);
context.SaveChanges();
}
}
public void Update(EmployeeBindingModel model)
{
using (var context = new DatabaseContext())
{
var employee = context.Employees.FirstOrDefault(e => e.Id == model.Id);
if (employee == null)
return;
employee.Name = model.Name;
employee.StartJob = model.StartJob;
employee.EndJob = model.EndJob;
employee.PartTimeJob = model.PartTimeJob;
employee.Bid = model.Bid;
context.SaveChanges();
}
}
public void Delete(int id)
{
using (var context = new DatabaseContext())
{
var employee = context.Employees.FirstOrDefault(e => e.Id == id);
if (employee != null)
{
context.Employees.Remove(employee);
context.SaveChanges();
}
}
}
}
}