PIbd-21_Pyatakov_KM_Markov_.../UniversityDataBaseImplemet/Implements/DocumentStorage.cs
Danil Markov 7e3706b2f6 Documents - нужно сделать update и пофиксить delete.
многие ко многим работает (нужен фикс update и delete)
2023-05-18 04:35:12 +04:00

149 lines
5.5 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)
.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.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.DateFrom != null && model.DateTo != null && model.UserId.HasValue)
{
return context.Documents
.Where(d => d.Date >= model.DateFrom && d.Date <= model.DateTo && d.UserId == model.UserId)
.Include(d => d.Students)
.ThenInclude(sd => sd.Student)
.ThenInclude(s => s.StudentStream)
.ThenInclude(ss => ss.Stream)
.Include(d => d.Students)
.ThenInclude(sd => sd.Student)
.ThenInclude(s => s.EducationStatus)
.Select(result => result.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);
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)
.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.Students.Where(c => c.UserId == userId).Count();
int numberOfpages = (int)Math.Ceiling((double)carsCount / pageSize);
return numberOfpages != 0 ? numberOfpages : 1;
}
}
}