using EmployeeManagmentContracts.SearchModels; using EmployeeManagmentContracts.StoragesContracts; using EmployeeManagmentContracts.ViewModels; using EmployeeManagmentDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; namespace EmployeeManagmentDataBaseImplement.Implements { public class SalaryStorage : ISalaryStorage { public List GetFullList() { using var context = new EmployeeManagementDbContext(); return context.Salaries .Select(s => new SalaryViewModel { Id = s.Id, CountHours = s.CountHours, PriceHour = s.PriceHour, Premium = s.Premium, Date = s.Data, Passed = s.Passed, EmployeeId = s.EmployeesId, EmployeeName = s.Employee != null ? $"{s.Employee.PhisicalPerson.Surname} {s.Employee.PhisicalPerson.Name} {s.Employee.PhisicalPerson.Patronymic} ({s.Employee.NameJob})" : "Не указано" }) .ToList(); } public List GetFilteredList(SalarySearchModel model) { using var context = new EmployeeManagementDbContext(); if (model == null) return new List(); return context.Salaries .Where(s => (!model.Date.HasValue || s.Data >= model.Date)) .Select(s => new SalaryViewModel { Id = s.Id, CountHours = s.CountHours, PriceHour = s.PriceHour, Premium = s.Premium, Date = s.Data, Passed = s.Passed, EmployeeId = s.EmployeesId, EmployeeName = s.Employee != null ? $"{s.Employee.PhisicalPerson.Surname} {s.Employee.PhisicalPerson.Name} {s.Employee.PhisicalPerson.Patronymic} ({s.Employee.NameJob})" : "Не указано" }) .ToList(); } public SalaryViewModel? GetElement(int id) { using var context = new EmployeeManagementDbContext(); var entity = context.Salaries .FirstOrDefault(s => s.Id == id); return entity == null ? null : new SalaryViewModel { Id = entity.Id, CountHours = entity.CountHours, PriceHour = entity.PriceHour, Premium = entity.Premium, Date = entity.Data, Passed = entity.Passed, EmployeeId = entity.EmployeesId, EmployeeName = entity.Employee != null ? $"{entity.Employee.PhisicalPerson.Surname} {entity.Employee.PhisicalPerson.Name} {entity.Employee.PhisicalPerson.Patronymic} ({entity.Employee.NameJob})" : "Не указано" }; } public void Insert(SalaryViewModel model) { using var context = new EmployeeManagementDbContext(); var newSalary = new Salary { CountHours = model.CountHours, PriceHour = model.PriceHour, Premium = model.Premium, Data = model.Date, Passed = model.Passed, EmployeesId = model.EmployeeId }; context.Salaries.Add(newSalary); context.SaveChanges(); } public void Update(SalaryViewModel model) { using var context = new EmployeeManagementDbContext(); var entity = context.Salaries.FirstOrDefault(s => s.Id == model.Id); if (entity != null) { entity.CountHours = model.CountHours; entity.PriceHour = model.PriceHour; entity.Premium = model.Premium; entity.Data = model.Date; entity.Passed = model.Passed; entity.EmployeesId = model.EmployeeId; context.SaveChanges(); } } public void Delete(int id) { using var context = new EmployeeManagementDbContext(); var entity = context.Salaries.FirstOrDefault(s => s.Id == id); if (entity != null) { context.Salaries.Remove(entity); context.SaveChanges(); } } } }