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 GetFullList() { using var context = new SchoolDatabase(); return context.Payments .Include(rec => rec.Lesson) .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(PaymentSearchModel model) { if (model == null) { return null; } using var context = new SchoolDatabase(); var list = context.Payments.Where(rec => rec.LessonId == model.LessonId) .OrderBy(x => x.DateOfPayment) .Include(rec => rec.Lesson); return list .Select(rec => new PaymentViewModel { DateOfPayment = rec.DateOfPayment, Id = rec.Id, Remains = rec.Remains, Sum = rec.Sum, LessonId = rec.LessonId }) .ToList(); } public PaymentViewModel GetElement(PaymentSearchModel model) { if (model == null) { return null; } var context = new SchoolDatabase(); { var payment = context.Payments.Include(rec => rec.Lesson) .FirstOrDefault(rec => rec.LessonId == model.LessonId); return payment != null ? new PaymentViewModel { DateOfPayment = payment.DateOfPayment, Id = payment.Id, Remains = payment.Lesson.LessonPrice, Sum = payment.Sum, LessonId = payment.LessonId } : null; } } public PaymentViewModel GetElementFirstLast(/*PaymentDateBindingModel model*/) { //TODO //if (model == null) //{ // return null; //} //var context = new SchoolDatabase(); //{ // return new PaymentViewModel() // { // Remains = context.Payments.Include(rec => rec.Lesson) // .Where(x => x.DateOfPayment > model.DateFrom || x.DateOfPayment < model.DateTo).Sum(x => x.Sum) // }; //} return null; } public PaymentViewModel? Insert(PaymentBindingModel model) { //TODO //var newPayment = Payment.Create(model); //if(newPayment == null) //{ // return null; //} //var context = new SchoolDatabase(); //context.Payments.Add(newPayment); //context.SaveChanges(); //return newPayment.GetViewModel; return null; } public PaymentViewModel? Update(PaymentBindingModel model) { var context = new SchoolDatabase(); var element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Оплата не найдена"); } element.Update(model); context.SaveChanges(); return element.GetViewModel; } public PaymentViewModel? Delete(PaymentBindingModel model) { var context = new SchoolDatabase(); var element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Payments.Remove(element); context.SaveChanges(); return element?.GetViewModel; } else { throw new Exception("Оплата не найдена"); } } } }