PIbd-22_Bondarenko_M.S_SUBD/PersonnelDepartmentView/PersonnelDepartmentDatabaseImplement/Implements/DealStorage.cs

113 lines
3.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using PersonnelDepartmentContracts.BindingModels;
using PersonnelDepartmentContracts.SearchModels;
using PersonnelDepartmentContracts.StoragesContracts;
using PersonnelDepartmentContracts.ViewModels;
using PersonnelDepartmentDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PersonnelDepartmentDatabaseImplement.Implements
{
public class DealStorage : IDealStorage
{
public DealViewModel? Delete(DealBindingModel model)
{
using var context = new PersonnelDepartmentDatabase();
var element = context.Deals
.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
context.Deals.Remove(element);
return element.GetViewModel;
}
public DealViewModel? GetElement(DealSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new PersonnelDepartmentDatabase();
return context.Deals
.Include(x => x.Employee)
.Include(x => x.Department)
.Include(x => x.Type)
.Include(x => x.Employee)
.FirstOrDefault(x => (model.Id.HasValue && model.Id == x.Id) ||
(model.DateFrom != null && x.DateFrom == model.DateFrom) ||
(model.DateTo != null && x.DateTo == model.DateTo) ||
(model.EmployeeId.HasValue && x.EmployeeId == model.EmployeeId) ||
(model.DepartmentId.HasValue && x.DepartmentId == model.DepartmentId) ||
(model.PositionId.HasValue && x.PositionId == model.PositionId) ||
(model.TypeId.HasValue && x.TypeId == model.TypeId))?.GetViewModel;
}
public List<DealViewModel> GetFilteredList(DealSearchModel model)
{
using var context = new PersonnelDepartmentDatabase();
return context.Deals
.Include(x => x.Employee)
.Include(x => x.Department)
.Include(x => x.Type)
.Include(x => x.Employee)
.Where(x => (model.Id.HasValue && model.Id == x.Id) ||
(model.DateFrom != null && x.DateFrom == model.DateFrom) ||
(model.DateTo != null && x.DateTo == model.DateTo) ||
(model.EmployeeId.HasValue && x.EmployeeId == model.EmployeeId) ||
(model.DepartmentId.HasValue && x.DepartmentId == model.DepartmentId) ||
(model.PositionId.HasValue && x.PositionId == model.PositionId) ||
(model.TypeId.HasValue && x.TypeId == model.TypeId))
.Select(x => x.GetViewModel) .ToList();
}
public List<DealViewModel> GetFullList()
{
using var context = new PersonnelDepartmentDatabase();
return context.Deals
.Include(x => x.Department)
.Include(x => x.Position)
.Include(x => x.Employee)
.Include(x => x.Type)
.Select(x => x.GetViewModel)
.ToList();
}
public DealViewModel? Insert(DealBindingModel model)
{
var newElement = Deal.Create(model);
if (newElement == null)
{
return null;
}
using var context = new PersonnelDepartmentDatabase();
context.Deals.Add(newElement);
context.SaveChanges();
return newElement.GetViewModel;
}
public DealViewModel? Update(DealBindingModel model)
{
using var context = new PersonnelDepartmentDatabase();
var element = context.Deals
.Include(x => x.Department)
.Include(x => x.Employee)
.Include(x => x.Type)
.Include (x => x.Position)
.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
}
}