2023-04-07 23:28:12 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SchoolContracts.BindingModels;
|
2023-04-06 22:52:23 +04:00
|
|
|
|
using SchoolContracts.SearchModel;
|
|
|
|
|
using SchoolContracts.StoragesContracts;
|
|
|
|
|
using SchoolContracts.ViewModels;
|
2023-04-07 23:28:12 +04:00
|
|
|
|
using SchoolDatabaseImplement;
|
|
|
|
|
using SchoolDatabaseImplement.Models;
|
2023-04-06 22:52:23 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SchoolDatabaseImplements.Implements
|
|
|
|
|
{
|
|
|
|
|
internal class ExpenseStorage : IExpenseStorage
|
|
|
|
|
{
|
|
|
|
|
public ExpenseViewModel? Delete(ExpenseBindingModel model)
|
|
|
|
|
{
|
2023-04-07 23:28:12 +04:00
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
var element = context.Expenses.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Expenses.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2023-04-06 22:52:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExpenseViewModel GetElement(ExpenseSearchModel model)
|
|
|
|
|
{
|
2023-04-07 23:28:12 +04:00
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
if (model.Id.HasValue)//сначала ищем по Id
|
|
|
|
|
{
|
|
|
|
|
return context.Expenses
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrEmpty(model.ExpenseName))//затем по названию
|
|
|
|
|
{
|
|
|
|
|
return context.Expenses
|
|
|
|
|
.FirstOrDefault(x => x.ExpenseName == model.ExpenseName)?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2023-04-06 22:52:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ExpenseViewModel> GetFilteredList(ExpenseSearchModel model)
|
|
|
|
|
{
|
2023-04-07 23:28:12 +04:00
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
return context.Expenses
|
|
|
|
|
.Where (x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-06 22:52:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ExpenseViewModel> GetFullList()
|
|
|
|
|
{
|
2023-04-07 23:28:12 +04:00
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
return context.Expenses
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-06 22:52:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExpenseViewModel? Insert(ExpenseBindingModel model)
|
|
|
|
|
{
|
2023-04-07 23:28:12 +04:00
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
var newExpense = Expense.Create(model);
|
|
|
|
|
if (newExpense == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Expenses.Add(newExpense);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newExpense.GetViewModel;
|
2023-04-06 22:52:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExpenseViewModel? Update(ExpenseBindingModel model)
|
|
|
|
|
{
|
2023-04-07 23:28:12 +04:00
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
var expense = context.Expenses.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (expense == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
expense.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return expense.GetViewModel;
|
2023-04-06 22:52:23 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|