89 lines
2.5 KiB
C#
89 lines
2.5 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 DepartmentStorage : IDepartmentStorage
|
|
{
|
|
public DepartmentViewModel? Delete(DepartmentBindingModel model)
|
|
{
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
var element = context.Departments
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Departments.Remove(element);
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
public DepartmentViewModel? GetElement(DepartmentSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
return context.Departments
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) ||
|
|
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
|
|
(model.Telephone.HasValue && x.Telephone == model.Telephone))?.GetViewModel;
|
|
}
|
|
|
|
public List<DepartmentViewModel> GetFilteredList(DepartmentSearchModel model)
|
|
{
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
return context.Departments
|
|
.Where(x => (model.Id.HasValue && x.Id == model.Id) ||
|
|
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
|
|
(model.Telephone.HasValue && x.Telephone == model.Telephone))
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<DepartmentViewModel> GetFullList()
|
|
{
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
return context.Departments
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public DepartmentViewModel? Insert(DepartmentBindingModel model)
|
|
{
|
|
var newElement = Department.Create(model);
|
|
if (newElement == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
context.Departments.Add(newElement);
|
|
context.SaveChanges();
|
|
return newElement.GetViewModel;
|
|
}
|
|
|
|
public DepartmentViewModel? Update(DepartmentBindingModel model)
|
|
{
|
|
using var context = new PersonnelDepartmentDatabase();
|
|
var element = context.Departments
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element == null)
|
|
{
|
|
return null;
|
|
}
|
|
element.Update(model);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
}
|
|
}
|