123 lines
4.1 KiB
C#
123 lines
4.1 KiB
C#
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using SchoolAgainStudyContracts.BindingModel;
|
|||
|
using SchoolAgainStudyContracts.SearchModel;
|
|||
|
using SchoolAgainStudyContracts.StorageContracts;
|
|||
|
using SchoolAgainStudyContracts.ViewModel;
|
|||
|
using SchoolAgainStudyDataBaseImplements.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Task = SchoolAgainStudyDataBaseImplements.Models.Task;
|
|||
|
|
|||
|
namespace SchoolAgainStudyDataBaseImplements.Implements
|
|||
|
{
|
|||
|
public class TaskStorage : ITaskStorage
|
|||
|
{
|
|||
|
public List<TaskViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new SchoolDataBase();
|
|||
|
return context.Tasks
|
|||
|
.Include(x => x.Materials)
|
|||
|
.ThenInclude(x => x.Material)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<TaskViewModel> GetFilteredList(TaskSearchModel model)
|
|||
|
{
|
|||
|
if (!model.TeacherId.HasValue)
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new SchoolDataBase();
|
|||
|
if (model.TeacherId.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
|
|||
|
{
|
|||
|
return context.Tasks
|
|||
|
.Include(x => x.Materials)
|
|||
|
.ThenInclude(x => x.Material)
|
|||
|
.Where(x => x.DateIssue >= model.DateFrom && x.DateIssue <= model.DateTo && x.TeacherId == model.TeacherId)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
return context.Tasks
|
|||
|
.Include(x => x.Materials)
|
|||
|
.ThenInclude(x => x.Material)
|
|||
|
.Where(x => x.TeacherId == model.TeacherId)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public TaskViewModel? GetElement(TaskSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new SchoolDataBase();
|
|||
|
return context.Tasks
|
|||
|
.Include(x => x.Materials)
|
|||
|
.ThenInclude(x => x.Material)
|
|||
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) ||
|
|||
|
(model.Id.HasValue && x.Id == model.Id))
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public TaskViewModel? Insert(TaskBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SchoolDataBase();
|
|||
|
var newTask = Task.Create(context, model);
|
|||
|
if (newTask == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Tasks.Add(newTask);
|
|||
|
context.SaveChanges();
|
|||
|
return newTask.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public TaskViewModel? Update(TaskBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SchoolDataBase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var task = context.Tasks.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (task == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
task.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
task.UpdateMaterials(context, model);
|
|||
|
transaction.Commit();
|
|||
|
return task.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public TaskViewModel? Delete(TaskBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SchoolDataBase();
|
|||
|
var element = context.Tasks
|
|||
|
.Include(x => x.Materials)
|
|||
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Tasks.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|