2023-04-06 22:52:23 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SchoolContracts.BindingModel;
|
|
|
|
|
using SchoolContracts.BindingModels;
|
|
|
|
|
using SchoolContracts.SearchModel;
|
|
|
|
|
using SchoolContracts.StoragesContracts;
|
|
|
|
|
using SchoolContracts.ViewModels;
|
|
|
|
|
using SchoolDatabaseImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SchoolDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class PaymentStorage : IPaymentStorage
|
|
|
|
|
{
|
|
|
|
|
public List<PaymentViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SchoolDatabase();
|
|
|
|
|
return context.Payments
|
|
|
|
|
.Include(rec => rec.Lesson)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PaymentViewModel> GetFilteredList(PaymentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new SchoolDatabase();
|
2023-04-07 00:30:18 +04:00
|
|
|
|
var list = context.Payments.Where(x => x.LessonId == model.LessonId)
|
2023-04-06 22:52:23 +04:00
|
|
|
|
.OrderBy(x => x.DateOfPayment)
|
2023-04-07 00:30:18 +04:00
|
|
|
|
.Include(x => x.Lesson);
|
2023-04-06 22:52:23 +04:00
|
|
|
|
return list
|
2023-04-07 00:30:18 +04:00
|
|
|
|
.Select(x => new PaymentViewModel
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
2023-04-07 00:30:18 +04:00
|
|
|
|
DateOfPayment = x.DateOfPayment,
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
Remains = x.Remains,
|
|
|
|
|
Sum = x.Sum,
|
|
|
|
|
LessonId = x.LessonId
|
2023-04-06 22:52:23 +04:00
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public PaymentViewModel GetElement(PaymentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-07 00:30:18 +04:00
|
|
|
|
using var context = new SchoolDatabase();
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
2023-04-07 00:30:18 +04:00
|
|
|
|
var payment = context.Payments.Include(x=> x.Lesson)
|
|
|
|
|
.FirstOrDefault(x => x.LessonId == model.LessonId);
|
|
|
|
|
return payment != null? new PaymentViewModel
|
2023-04-06 22:52:23 +04:00
|
|
|
|
{
|
|
|
|
|
DateOfPayment = payment.DateOfPayment,
|
|
|
|
|
Id = payment.Id,
|
|
|
|
|
Remains = payment.Lesson.LessonPrice,
|
|
|
|
|
Sum = payment.Sum,
|
|
|
|
|
LessonId = payment.LessonId
|
|
|
|
|
} :
|
|
|
|
|
null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public PaymentViewModel? Insert(PaymentBindingModel model)
|
|
|
|
|
{
|
2023-04-07 00:30:18 +04:00
|
|
|
|
var context = new SchoolDatabase();
|
|
|
|
|
var newPayment = Payment.Create(context, model);
|
|
|
|
|
if (newPayment == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Payments.Add(newPayment);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newPayment.GetViewModel;
|
2023-04-06 22:52:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PaymentViewModel? Update(PaymentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var context = new SchoolDatabase();
|
2023-04-07 00:30:18 +04:00
|
|
|
|
var element = context.Payments.
|
|
|
|
|
Include(x => x.Lesson)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
2023-04-06 22:52:23 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Оплата не найдена");
|
|
|
|
|
}
|
|
|
|
|
element.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public PaymentViewModel? Delete(PaymentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var context = new SchoolDatabase();
|
|
|
|
|
|
2023-04-07 00:30:18 +04:00
|
|
|
|
var element = context.Payments
|
|
|
|
|
.Include(x => x.Lesson)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
2023-04-06 22:52:23 +04:00
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Payments.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Оплата не найдена");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|