92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using BeautySalonContracts.BindingModels;
|
|
using BeautySalonContracts.SearchModels;
|
|
using BeautySalonContracts.StoragesContracts;
|
|
using BeautySalonContracts.ViewModels;
|
|
using BeautySalonDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BeautySalonDatabaseImplement.Implements
|
|
{
|
|
public class VisitStorage : IVisitStorage
|
|
{
|
|
public VisitViewModel? Delete(VisitBindingModel model)
|
|
{
|
|
using var context = new BeautySalonDatabase();
|
|
var element = context.Visits
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Service)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Visits.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public VisitViewModel? GetElement(VisitSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new BeautySalonDatabase();
|
|
return context.Visits.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
}
|
|
|
|
public List<VisitViewModel> GetFilteredList(VisitSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new BeautySalonDatabase();
|
|
return context.Visits
|
|
.Where(x => x.ClientId == model.ClientId)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<VisitViewModel> GetFullList()
|
|
{
|
|
using var context = new BeautySalonDatabase();
|
|
return context.Visits
|
|
.Include(x => x.Service)
|
|
.Include(x => x.Client)
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public VisitViewModel? Insert(VisitBindingModel model)
|
|
{
|
|
var newVisit = Visit.Create(model);
|
|
if (newVisit == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new BeautySalonDatabase();
|
|
context.Visits.Add(newVisit);
|
|
context.SaveChanges();
|
|
return context.Visits
|
|
.Include(x => x.Service)
|
|
.Include(x => x.Client)
|
|
.FirstOrDefault(x => x.Id == newVisit.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public VisitViewModel? Update(VisitBindingModel model)
|
|
{
|
|
using var context = new BeautySalonDatabase();
|
|
var order = context.Visits.FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
order.Update(model);
|
|
context.SaveChanges();
|
|
return order.GetViewModel;
|
|
}
|
|
}
|
|
}
|