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

117 lines
3.7 KiB
C#
Raw Normal View History

2023-04-08 22:25:55 +04:00
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 Stream = UniversityDataBaseImplemet.Models.Stream;
namespace UniversityDataBaseImplemet.Implements
{
public class StreamStorage:IStreamStorage
{
public StreamViewModel? GetElement(StreamSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Streams
2023-04-09 03:08:30 +04:00
.Include(record => record.StreamStudents)
.ThenInclude(record => record.Student)
.FirstOrDefault(record => record.Id == model.Id
2023-04-08 22:25:55 +04:00
|| record.Name.Equals(model.Name))
?.GetViewModel;
}
public List<StreamViewModel> GetFilteredList(StreamSearchModel model)
{
using var context = new Database();
if (model.Id.HasValue)
{
return context.Streams
2023-04-09 03:08:30 +04:00
.Include(record => record.StreamStudents)
.ThenInclude(record => record.Student)
.Where(record => record.Id.Equals(model.Id))
2023-04-08 22:25:55 +04:00
.Select(record => record.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.Streams
2023-04-09 03:08:30 +04:00
.Include(record => record.StreamStudents)
.ThenInclude(record => record.Student)
.Where(record => record.UserId == model.UserId)
2023-04-08 22:25:55 +04:00
.Select(record => record.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<StreamViewModel> GetFullList()
{
using var context = new Database();
return context.Streams
2023-04-09 03:08:30 +04:00
.Include(record => record.StreamStudents)
.ThenInclude(record => record.Student)
.Select(record => record.GetViewModel)
2023-04-08 22:25:55 +04:00
.ToList();
}
public StreamViewModel? Insert(StreamBindingModel model)
{
var newStream = Stream.Create(model);
if (newStream == null)
{
return null;
}
using var context = new Database();
context.Streams.Add(newStream);
context.SaveChanges();
return newStream.GetViewModel;
}
public StreamViewModel? Update(StreamBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var stream = context.Streams
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (stream == null)
{
return null;
}
stream.Update(model);
context.SaveChanges();
2023-04-09 03:08:30 +04:00
stream.UpdateStreamStudents(context, model);
transaction.Commit();
2023-04-08 22:25:55 +04:00
return stream.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public StreamViewModel? Delete(StreamBindingModel model)
{
using var context = new Database();
var stream = context.Streams
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (stream == null)
{
return null;
}
context.Streams.Remove(stream);
context.SaveChanges();
return stream.GetViewModel;
}
}
}