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

126 lines
4.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<SalaryViewModel> 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<SalaryViewModel> GetFilteredList(SalarySearchModel model)
{
using var context = new EmployeeManagementDbContext();
if (model == null) return new List<SalaryViewModel>();
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();
}
}
}
}