147 lines
4.5 KiB
C#
Raw Normal View History

2023-04-08 23:14:49 +04:00
using Microsoft.EntityFrameworkCore;
using System;
2023-04-08 21:09:06 +04:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicBusinessLogic.BindingModels;
using VetClinicBusinessLogic.Interfaces;
using VetClinicBusinessLogic.ViewModels;
using VetClinicDatabaseImplement.Models;
namespace VetClinicDatabaseImplement.Implement
{
public class PaymentStorage : IPaymentStorage
{
public List<PaymentViewModel> GetFullList()
{
var context = new VetClinicDatabase();
return context.Payments.Include(rec => rec.Visit)
.Select(CreateViewModel)
.OrderBy(x => x.DateOfPayment)
.ToList();
}
public List<PaymentViewModel> GetFilteredList(PaymentBindingModel model)
{
if (model == null)
{
return null;
}
var context = new VetClinicDatabase();
var list = context.Payments.Where(rec => rec.VisitsId == model.VisitId)
.OrderBy(x => x.DateOfPayment)
.Include(rec => rec.Visit);
return list
.Select(rec => new PaymentViewModel
{
DateOfPayment = rec.DateOfPayment,
Id = rec.Id,
Remains = rec.Remains,
Sum = rec.Sum,
VisitId = rec.VisitsId
})
.ToList();
}
public PaymentViewModel GetElement(PaymentBindingModel model)
{
if (model == null)
{
return null;
}
var context = new VetClinicDatabase();
{
var payment = context.Payments.Include(rec => rec.Visit)
.FirstOrDefault(rec => rec.VisitsId == model.VisitId);
return payment != null ?
new PaymentViewModel
{
DateOfPayment = payment.DateOfPayment,
Id = payment.Id,
Remains = payment.Visit.Sum,
Sum = payment.Sum,
VisitId = payment.VisitsId
} :
null;
}
}
public PaymentViewModel GetElementFirstLast(PaymentDateBindingModel model)
{
if (model == null)
{
return null;
}
var context = new VetClinicDatabase();
{
return new PaymentViewModel()
{
Remains = context.Payments.Include(rec => rec.Visit)
.Where(x => x.DateOfPayment > model.DateFrom || x.DateOfPayment < model.DateTo).Sum(x => x.Sum)
};
}
}
public void Insert(PaymentBindingModel model)
{
var context = new VetClinicDatabase();
context.Payments.Add(CreateModel(model, new Payment()));
context.SaveChanges();
}
public void Update(PaymentBindingModel model)
{
var context = new VetClinicDatabase();
var element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id);
if (element == null)
{
throw new Exception("Оплата не найдена");
}
CreateModel(model, element);
context.SaveChanges();
}
public void Delete(PaymentBindingModel model)
{
var context = new VetClinicDatabase();
Payment element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Payments.Remove(element);
context.SaveChanges();
}
else
{
throw new Exception("Оплата не найдена");
}
}
private PaymentViewModel CreateViewModel(Payment payment)
{
return new PaymentViewModel
{
DateOfPayment = payment.DateOfPayment,
Id = payment.Id,
Remains = payment.Visit.Sum,
Sum = payment.Sum,
VisitId = payment.VisitsId
};
}
private Payment CreateModel(PaymentBindingModel model, Payment payment)
{
payment.DateOfPayment = model.DateOfPayment;
payment.Id = model.Id;
payment.Remains = model.Remains;
payment.Sum = model.Sum;
payment.VisitsId = model.VisitId;
return payment;
}
}
}