118 lines
4.1 KiB
C#
118 lines
4.1 KiB
C#
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 VacationStorage : IVacationStorage
|
||
{
|
||
public List<VacationViewModel> GetFullList()
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
return context.Vacations
|
||
.Select(v => new VacationViewModel
|
||
{
|
||
Id = v.Id,
|
||
StartData = v.StartData,
|
||
EndData = v.EndData,
|
||
Passed = v.Passed,
|
||
EmployeeId = v.EmployeesId,
|
||
EmployeeName = v.Employee != null ? $"{v.Employee.PhisicalPerson.Surname} {v.Employee.PhisicalPerson.Name} {v.Employee.PhisicalPerson.Patronymic} ({v.Employee.NameJob})" : "Не указано"
|
||
|
||
})
|
||
.ToList();
|
||
}
|
||
|
||
public List<VacationViewModel> GetFilteredList(VacationSearchModel model)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
if (model == null) return new List<VacationViewModel>();
|
||
|
||
return context.Vacations
|
||
.Where(v =>
|
||
(!model.StartData.HasValue || v.StartData >= model.StartData) &&
|
||
(!model.EndData.HasValue || v.EndData <= model.EndData))
|
||
.Select(v => new VacationViewModel
|
||
{
|
||
Id = v.Id,
|
||
StartData = v.StartData,
|
||
EndData = v.EndData,
|
||
Passed = v.Passed,
|
||
EmployeeId = v.EmployeesId,
|
||
EmployeeName = v.Employee != null ? $"{v.Employee.PhisicalPerson.Surname} {v.Employee.PhisicalPerson.Name} {v.Employee.PhisicalPerson.Patronymic} ({v.Employee.NameJob})" : "Не указано"
|
||
})
|
||
.ToList();
|
||
}
|
||
|
||
public VacationViewModel? GetElement(int id)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var entity = context.Vacations
|
||
.FirstOrDefault(v => v.Id == id);
|
||
|
||
return entity == null ? null : new VacationViewModel
|
||
{
|
||
Id = entity.Id,
|
||
StartData = entity.StartData,
|
||
EndData = entity.EndData,
|
||
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(VacationViewModel model)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var newVacation = new Vacation
|
||
{
|
||
StartData = model.StartData,
|
||
EndData = model.EndData,
|
||
Passed = model.Passed,
|
||
EmployeesId = model.EmployeeId
|
||
};
|
||
|
||
context.Vacations.Add(newVacation);
|
||
context.SaveChanges();
|
||
}
|
||
|
||
public void Update(VacationViewModel model)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var entity = context.Vacations.FirstOrDefault(v => v.Id == model.Id);
|
||
|
||
if (entity != null)
|
||
{
|
||
entity.StartData = model.StartData;
|
||
entity.EndData = model.EndData;
|
||
entity.Passed = model.Passed;
|
||
entity.EmployeesId = model.EmployeeId;
|
||
|
||
context.SaveChanges();
|
||
}
|
||
}
|
||
|
||
public void Delete(int id)
|
||
{
|
||
using var context = new EmployeeManagementDbContext();
|
||
|
||
var entity = context.Vacations.FirstOrDefault(v => v.Id == id);
|
||
|
||
if (entity != null)
|
||
{
|
||
context.Vacations.Remove(entity);
|
||
context.SaveChanges();
|
||
}
|
||
}
|
||
}
|
||
}
|