2023-04-05 22:23:31 +04:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace SchoolAgainStudyDataBaseImplements.Implements
|
|
|
|
|
{
|
|
|
|
|
public class DiyStorage : IDiyStorage
|
|
|
|
|
{
|
|
|
|
|
public List<DiyViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDataBase();
|
|
|
|
|
return context.Diys
|
|
|
|
|
.Include(x => x.Interests)
|
|
|
|
|
.ThenInclude(x => x.Interest)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DiyViewModel> GetFilteredList(DiySearchModel model)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(!model.StudentId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new SchoolDataBase();
|
|
|
|
|
if(model.DateFrom.HasValue && model.DateTo.HasValue && model.StudentId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Diys
|
|
|
|
|
.Include(x => x.Interests)
|
|
|
|
|
.ThenInclude(x => x.Interest)
|
|
|
|
|
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo && x.StudentId == model.StudentId)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return context.Diys
|
|
|
|
|
.Include(x => x.Interests)
|
|
|
|
|
.ThenInclude(x => x.Interest)
|
|
|
|
|
.Where(x => x.StudentId == model.StudentId )
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DiyViewModel? GetElement(DiySearchModel model)
|
|
|
|
|
{
|
2023-04-06 22:07:26 +04:00
|
|
|
|
if ((string.IsNullOrEmpty(model.Title) && !model.Id.HasValue) || !model.StudentId.HasValue)
|
2023-04-05 22:23:31 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SchoolDataBase();
|
|
|
|
|
return context.Diys
|
|
|
|
|
.Include(x => x.Interests)
|
|
|
|
|
.ThenInclude(x => x.Interest)
|
2023-04-06 22:07:26 +04:00
|
|
|
|
.FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) ||
|
|
|
|
|
(model.Id.HasValue && x.Id == model.Id)) && x.StudentId==model.StudentId)
|
2023-04-05 22:23:31 +04:00
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DiyViewModel? Insert(DiyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDataBase();
|
|
|
|
|
var newDiy = Diy.Create(context, model);
|
|
|
|
|
if (newDiy == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Diys.Add(newDiy);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newDiy.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DiyViewModel? Update(DiyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDataBase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var diy = context.Diys.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (diy == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
diy.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
diy.UpdateInterests(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return diy.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DiyViewModel? Delete(DiyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDataBase();
|
|
|
|
|
var element = context.Diys
|
|
|
|
|
.Include(x => x.Interests)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Diys.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|