PIbd-21_Pyatakov_KM_Markov_.../UniversityDataBaseImplemet/Implements/EducationStatusStorage.cs

158 lines
5.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDataBaseImplemet.Models;
namespace UniversityDataBaseImplemet.Implements
{
public class EducationStatusStorage : IEducationStatusStorage
{
public EducationStatusViewModel? GetElement(EducationStatusSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.EducationStatuses
.Include(record => record.User)
.FirstOrDefault(record => record.Id == model.Id
|| record.Name.Equals(model.Name))
?.GetViewModel;
}
public List<EducationStatusViewModel> GetFilteredList(EducationStatusSearchModel model)
{
using var context = new Database();
if (model.UserId.HasValue && model.PageNumber.HasValue && model.PageSize.HasValue)
{
return context.EducationStatuses
.Include(record => record.User)
.Where(x => x.UserId == model.UserId)
.Skip(model.PageSize.Value * (model.PageNumber.Value - 1))
.Take(model.PageSize.Value)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.Id.HasValue)
{
return context.EducationStatuses
.Include(record => record.User)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.EducationStatuses
.Include(record => record.User)
.Where(record => record.UserId == model.UserId)
.Select(record => record.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<EducationStatusViewModel> GetFullList()
{
using var context = new Database();
return context.EducationStatuses
.Select(record => record.GetViewModel)
.ToList();
}
public EducationStatusViewModel? Insert(EducationStatusBindingModel model)
{
var newEducationStatus = EducationStatus.Create(model);
if (newEducationStatus == null)
{
return null;
}
using var context = new Database();
context.EducationStatuses.Add(newEducationStatus);
context.SaveChanges();
return context.EducationStatuses
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(newEducationStatus.Id))
?.GetViewModel;
}
public EducationStatusViewModel? Update(EducationStatusBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var educationStatus = context.EducationStatuses
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (educationStatus == null)
{
return null;
}
educationStatus.Update(model);
context.SaveChanges();
transaction.Commit();
return educationStatus.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public EducationStatusViewModel? Delete(EducationStatusBindingModel model)
{
using var context = new Database();
var educationStatus = context.EducationStatuses
.Include(record => record.User)
.Include(record => record.Students)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (educationStatus == null)
{
return null;
}
context.EducationStatuses.Remove(educationStatus);
context.SaveChanges();
return educationStatus.GetViewModel;
}
public List<EducationStatusStudentsCountViewModel>? GetEducationStatusStudents()
{
using var context = new Database();
var result = context.EducationStatuses
.GroupJoin(
context.Students,
status => status.Id,
student => student.EducationStatusId,
(status, students) => new EducationStatusStudentsCountViewModel
{
Name = status.Name,
Count = students.Count()
})
.ToList();
return result;
}
public int GetNumberOfPages(int userId, int pageSize)
{
using var context = new Database();
int carsCount = context.EducationStatuses.Where(c => c.UserId == userId).Count();
int numberOfpages = (int)Math.Ceiling((double)carsCount / pageSize);
return numberOfpages != 0 ? numberOfpages : 1;
}
}
}