115 lines
3.8 KiB
C#
115 lines
3.8 KiB
C#
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using TaskTrackerContracts.BindingModels;
|
|||
|
using TaskTrackerContracts.SearchModels;
|
|||
|
using TaskTrackerContracts.StoragesContracts;
|
|||
|
using TaskTrackerContracts.ViewModels;
|
|||
|
using TaskTrackerDatabase.Models;
|
|||
|
|
|||
|
namespace TaskTrackerDatabase.Implements
|
|||
|
{
|
|||
|
public class DirectionStorage : IDirectionStorage
|
|||
|
{
|
|||
|
public List<DirectionViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new TaskTrackerDatabase();
|
|||
|
return context.Directions
|
|||
|
.Include(x => x.Students)
|
|||
|
.ThenInclude(x => x.Student)
|
|||
|
//.ToList()
|
|||
|
.Include(x => x.Subjects)
|
|||
|
.ThenInclude(x => x.Subject)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public List<DirectionViewModel> GetFilteredList(DirectionSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Name))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new TaskTrackerDatabase();
|
|||
|
return context.Directions
|
|||
|
.Include(x => x.Students)
|
|||
|
.ThenInclude(x => x.Student)
|
|||
|
.Include(x => x.Subjects)
|
|||
|
.ThenInclude(x => x.Subject)
|
|||
|
.Where(x => x.Name.Contains(model.Name))
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public DirectionViewModel? GetElement(DirectionSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new TaskTrackerDatabase();
|
|||
|
return context.Directions
|
|||
|
.Include(x => x.Students)
|
|||
|
.ThenInclude(x => x.Student)
|
|||
|
.Include(x => x.Subjects)
|
|||
|
.ThenInclude(x => x.Subject)
|
|||
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
|
|||
|
(model.Id.HasValue && x.Id == model.Id))
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
public DirectionViewModel? Insert(DirectionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new TaskTrackerDatabase();
|
|||
|
var newReinforced = Direction.Create(context, model);
|
|||
|
if (newReinforced == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Directions.Add(newReinforced);
|
|||
|
context.SaveChanges();
|
|||
|
return newReinforced.GetViewModel;
|
|||
|
}
|
|||
|
public DirectionViewModel? Update(DirectionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new TaskTrackerDatabase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var reinforced = context.Directions.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (reinforced == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
reinforced.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
reinforced.UpdateStudents(context, model);
|
|||
|
reinforced.UpdateSubjects(context, model);
|
|||
|
transaction.Commit();
|
|||
|
return reinforced.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
public DirectionViewModel? Delete(DirectionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new TaskTrackerDatabase();
|
|||
|
var element = context.Directions
|
|||
|
.Include(x => x.Students)
|
|||
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Directions.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|