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();
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|