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

120 lines
3.1 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();
return context.Circles
.Where(x => x.Id == model.Id)
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
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;
}
/// <summary>
/// Получение списка занятий по выбранному кружку
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<ReportCircleLessonsViewModel> GetLessonCircles(CircleSearchModel model)
{
if (model.SelectedCirclesIds == null)
{
return new();
}
using var context = new SchoolDatabase();
return context.Circles
.Where(x => model.SelectedCirclesIds.Contains(x.Id))
.Select(x => new ReportCircleLessonsViewModel()
{
CircleId= x.Id,
Lessons = context.CircleLessons
.Include(x => x.Lesson)
.ThenInclude(x => x.Employee)
.Where(x => x.CircleId == x.Id)
.Select(x => new ReportLessonCirclesViewModel()
{
LessonId = x.Circle.Id,
EmployeeName = x.Lesson.Employee.EmployeeName,
Sum = x.Lesson.LessonPrice
})
.ToList()
})
.ToList();
}
}
}