ISEbd-21_Tukaeva_A_A_School/School/SchoolDatabaseImplements/Implements/CircleStorage.cs

130 lines
3.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using SchoolContracts.BindingModel;
using SchoolContracts.SearchModel;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace SchoolDatabaseImplement.Implements
{
public class CircleStorage : ICircleStorage
{
public List<CircleViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.Circles
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CircleViewModel> GetFilteredList(CircleSearchModel model)
{
using var context = new SchoolDatabase();
if (model.DateFrom.HasValue && model.DateTo.HasValue) // если не ищем по айдишнику, значит ищем по диапазону дат
{
return context.Circles
.Where(x => model.DateFrom <= x.DateStart.Date && x.DateStart <= model.DateTo)
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.ClientId.HasValue)
{
return context.Circles
.Where(x => x.Client.Id == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public CircleViewModel? GetElement(CircleSearchModel model)
{
if (model == null)
{
return null;
}
using var context = new SchoolDatabase();
if(model.Id.HasValue)
{
return context.Circles.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
return null;
}
public CircleViewModel? Insert(CircleBindingModel model)
{
using var context = new SchoolDatabase();
var newCircle = Circle.Create(context, model);
if (newCircle == null)
{
return null;
}
context.Circles.Add(newCircle);
context.SaveChanges();
return newCircle.GetViewModel;
}
public CircleViewModel? Update(CircleBindingModel model)
{
using var context = new SchoolDatabase();
var circle = context.Circles.FirstOrDefault(x => x.Id == model.Id);
if (circle == null)
{
return null;
}
circle.Update(model);
context.SaveChanges();
return circle.GetViewModel;
}
public CircleViewModel? Delete(CircleBindingModel model)
{
using var context = new SchoolDatabase();
var circle = context.Circles.FirstOrDefault(rec => rec.Id == model.Id);
if (circle != null)
{
context.Circles.Remove(circle);
context.SaveChanges();
return circle.GetViewModel;
}
return null;
}
public List<ReportCircleLessonsViewModel> GetLessonCircles(CircleSearchModel model)
{
if (model.SelectedCirclesIds == null)
{
return new();
}
using var context = new SchoolDatabase();
return context.Circles
.Where(c => model.SelectedCirclesIds.Contains(c.Id))
.Select(c => new ReportCircleLessonsViewModel()
{
CircleLessons = GetCircleLessons(context, new() { Id = c.Id })
})
.ToList();
}
/// <summary>
/// Получение списка занятий по выбранному кружку
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
private static List<ReportLessonCirclesViewModel> GetCircleLessons(SchoolDatabase context, CircleSearchModel model)
{
return context.Circles
.Include(wir => wir.CircleLessons)
.Where(wir => wir.Id == model.Id)
.Select(wir => new ReportLessonCirclesViewModel()
{
LessonId = wir.CircleLessons.Id
})
.ToList();
}
}
}