2023-04-07 13:24:39 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
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)
|
2023-04-07 17:34:20 +04:00
|
|
|
|
.FirstOrDefault(record => record.Id == model.Id
|
|
|
|
|
|| record.Name.Equals(model.Name))
|
|
|
|
|
?.GetViewModel;
|
2023-04-07 13:24:39 +04:00
|
|
|
|
}
|
|
|
|
|
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Documents
|
2023-04-07 17:34:20 +04:00
|
|
|
|
.Include(record => record.User)
|
2023-04-07 13:24:39 +04:00
|
|
|
|
.Where(record => record.Id.Equals(model.Id))
|
|
|
|
|
.Select(record => record.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-08 14:02:34 +04:00
|
|
|
|
else if (model.DateFrom != null && model.DateTo != null && model.UserId.HasValue) // фильтрация для отчета#2 Поставщик
|
|
|
|
|
{
|
|
|
|
|
return context.Documents
|
|
|
|
|
.Where(d => d.Date >= model.DateFrom && d.Date <= model.DateTo)
|
|
|
|
|
.Join(context.StudentDocuments,
|
|
|
|
|
doc => doc.Id,
|
|
|
|
|
studDoc => studDoc.DocumentId,
|
|
|
|
|
(doc, studDoc) => new { Document = doc, StudentDocument = studDoc })
|
|
|
|
|
.Join(context.Students,
|
|
|
|
|
studDoc => studDoc.StudentDocument.StudentId,
|
|
|
|
|
stud => stud.Id,
|
|
|
|
|
(studDoc, stud) => new { studDoc.Document, Student = stud })
|
|
|
|
|
.Include(record => record.Student.EducationStatus)
|
|
|
|
|
.Join(context.StudentStreams,
|
|
|
|
|
studEdu => studEdu.Student.Id,
|
|
|
|
|
studStream => studStream.StudentId,
|
|
|
|
|
(studEdu, studStream) => new { studEdu.Document, studEdu.Student, studEdu.EducationStatus, StudentStream = studStream })
|
|
|
|
|
.Join(context.Streams,
|
|
|
|
|
studStream => studStream.StudentStream.StreamId,
|
|
|
|
|
stream => stream.Id,
|
|
|
|
|
(studStream, stream) => new { Stream = stream.Name, studStream.Student, studStream.EducationStatus.Name })
|
|
|
|
|
.Select(result => new {
|
|
|
|
|
result.Stream,
|
|
|
|
|
result.Student,
|
|
|
|
|
result.EducationStatus
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-07 13:24:39 +04:00
|
|
|
|
else if (model.UserId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Documents
|
2023-04-07 17:34:20 +04:00
|
|
|
|
.Include(record => record.User)
|
|
|
|
|
.Where(record => record.UserId == model.UserId)
|
|
|
|
|
.Select(record => record.GetViewModel)
|
2023-04-07 13:24:39 +04:00
|
|
|
|
.ToList();
|
2023-04-07 17:34:20 +04:00
|
|
|
|
}
|
2023-04-07 13:24:39 +04:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public List<DocumentViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
return context.Documents
|
|
|
|
|
.Include(record => record.User)
|
|
|
|
|
.Select(record => record.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public DocumentViewModel? Insert(DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var newDocument = Document.Create(model);
|
|
|
|
|
if(newDocument == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
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
|
2023-04-07 17:34:20 +04:00
|
|
|
|
.Include(record => record.User)
|
2023-04-07 13:24:39 +04:00
|
|
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
|
|
|
if (document == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
document.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return document.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public DocumentViewModel? Delete(DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
var document = context.Documents
|
2023-04-07 17:34:20 +04:00
|
|
|
|
.Include(record => record.User)
|
2023-04-07 13:24:39 +04:00
|
|
|
|
.FirstOrDefault(record => record.Id.Equals(model.Id));
|
|
|
|
|
if (document == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Documents.Remove(document);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return document.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|