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

114 lines
3.0 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using SchoolContracts.BindingModels;
using SchoolContracts.SearchModel;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement;
using SchoolDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolDatabaseImplements.Implements
{
public class CircleExpenseStorage : ICircleExpenseStorage
{
public CircleExpenseViewModel? Delete(CircleExpenseBindingModel model)
{
using var context = new SchoolDatabase();
var circleExpense = context.CircleExpenses.FirstOrDefault(rec => rec.Id == model.Id);
if (circleExpense != null)
{
context.CircleExpenses.Remove(circleExpense);
context.SaveChanges();
return circleExpense.GetViewModel;
}
return null;
}
public CircleExpenseViewModel GetElement(CircleExpenseSearchModel model)
{
if (model == null)
{
return null;
}
using var context = new SchoolDatabase();
if (model.Id.HasValue)
{
return context.CircleExpenses.Include(x => x.Circle)
.Include(x => x.Expense)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
return null;
}
public List<ReportExpensesInCirclesViewModel> GetExpensesInCircles(CircleExpenseSearchModel model)
{
if (model.DateFrom == null || model.DateTo == null)
{
return new();
}
using var context = new SchoolDatabase();
return context.CircleExpenses.Include(x => x.Circle)
.Include(x => x.Expense)
.Select(x => new ReportExpensesInCirclesViewModel()
{
CircleId = x.Circle.Id,
ExpenseName = x.Expense.ExpenseName,
Count = x.Expense.Count,
Sum= x.Expense.Sum
})
.ToList();
}
public List<CircleExpenseViewModel> GetFilteredList(CircleExpenseSearchModel model)
{
using var context = new SchoolDatabase();
return context.CircleExpenses
.Where(x => x.Id == model.Id)
.Include(x => x.Circle)
.Include(x => x.Expense)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CircleExpenseViewModel> GetFullList()
{
using var context = new SchoolDatabase();
return context.CircleExpenses
.Include(x => x.Circle)
.Include(x => x.Expense)
.Select(x => x.GetViewModel)
.ToList();
}
public CircleExpenseViewModel? Insert(CircleExpenseBindingModel model)
{
using var context = new SchoolDatabase();
var newCircleExpense = CircleExpense.Create(context, model);
if (newCircleExpense == null)
{
return null;
}
context.CircleExpenses.Add(newCircleExpense);
context.SaveChanges();
return newCircleExpense.GetViewModel;
}
public CircleExpenseViewModel? Update(CircleExpenseBindingModel model)
{
using var context = new SchoolDatabase();
var circleExpense = context.CircleExpenses.FirstOrDefault(x => x.Id == model.Id);
if (circleExpense == null)
{
return null;
}
circleExpense.Update(model);
context.SaveChanges();
return circleExpense.GetViewModel;
}
}
}