99 lines
2.6 KiB
C#
99 lines
2.6 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;
|
|
}
|
|
}
|
|
}
|