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

153 lines
5.7 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
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 DocumentStorage : IDocumentStorage
{
public DocumentViewModel? GetElement(DocumentSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Documents
.Include(record => record.User)
.Include(record => record.Students)
.ThenInclude(record => record.Student)
.Include(record => record.EducationGroupDocument)
.ThenInclude(record => record.EducationGroup)
.FirstOrDefault(record => record.Id == model.Id
|| record.Name.Equals(model.Name))
?.GetViewModel;
}
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
{
using var context = new Database();
if (model.UserId.HasValue && model.PageNumber.HasValue && model.PageSize.HasValue)
{
return context.Documents
.Include(record => record.User)
.Include(record => record.Students)
.ThenInclude(record => record.Student)
.Include(record => record.EducationGroupDocument)
.ThenInclude(record => record.EducationGroup)
.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.Documents
.Include(record => record.User)
.Include(record => record.Students)
.ThenInclude(record => record.Student)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.Documents
.Include(record => record.User)
.Include(record => record.Students)
.ThenInclude(record => record.Student)
.Where(record => record.UserId == model.UserId)
.Select(record => record.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<DocumentViewModel> GetFullList()
{
using var context = new Database();
return context.Documents
.Include(record => record.User)
.Include(record => record.Students)
.ThenInclude(record => record.Student)
.Select(record => record.GetViewModel)
.ToList();
}
public DocumentViewModel? Insert(DocumentBindingModel model)
{
using var context = new Database();
var newDocument = Document.Create(context, model);
if(newDocument == null)
{
return null;
}
context.Documents.Add(newDocument);
context.SaveChanges();
return newDocument.GetViewModel;
}
public DocumentViewModel? Update(DocumentBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var document = context.Documents
.Include(record => record.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (document == null)
{
return null;
}
document.Update(model);
context.SaveChanges();
document.UpdateStudents(context, model);
document.UpdateGroups(context, model);
transaction.Commit();
return document.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public DocumentViewModel? Delete(DocumentBindingModel model)
{
using var context = new Database();
var document = context.Documents
.Include(record => record.User)
.Include(record => record.Students)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (document == null)
{
return null;
}
context.Documents.Remove(document);
context.SaveChanges();
return document.GetViewModel;
}
public int GetNumberOfPages(int userId, int pageSize)
{
using var context = new Database();
int carsCount = context.Documents.Where(c => c.UserId == userId).Count();
int numberOfpages = (int)Math.Ceiling((double)carsCount / pageSize);
return numberOfpages != 0 ? numberOfpages : 1;
}
}
}