72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
using HotelContracts.BindingModels;
|
|
using HotelContracts.SearchModels;
|
|
using HotelContracts.StoragesContracts;
|
|
using HotelContracts.ViewModels;
|
|
using HotelDatabaseImplement.Models;
|
|
|
|
namespace HotelDatabaseImplement.Implements;
|
|
|
|
public class GuestStorage : IGuestStorage
|
|
{
|
|
public List<GuestViewModel> GetFullList()
|
|
{
|
|
using var context = new HotelDataBase();
|
|
return context.Guests
|
|
.Select(x => x.GetView)
|
|
.ToList();
|
|
}
|
|
|
|
public List<GuestViewModel> GetFilteredList(GuestSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return new List<GuestViewModel>();
|
|
}
|
|
|
|
using var context = new HotelDataBase();
|
|
return context.Guests
|
|
.Where(x => x.Name == model.Name)
|
|
.Select(x => x.GetView)
|
|
.ToList();
|
|
}
|
|
|
|
public GuestViewModel? GetElement(GuestSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue) return null;
|
|
using var context = new HotelDataBase();
|
|
return context.Guests.FirstOrDefault(x => x.Id == model.Id)?.GetView;
|
|
}
|
|
|
|
public GuestViewModel? Insert(GuestBindingModel model)
|
|
{
|
|
var item = Guest.Create(model);
|
|
if (item == null) return null;
|
|
|
|
using var context = new HotelDataBase();
|
|
context.Guests.Add(item);
|
|
context.SaveChanges();
|
|
|
|
return item.GetView;
|
|
}
|
|
|
|
public GuestViewModel? Update(GuestBindingModel model)
|
|
{
|
|
using var context = new HotelDataBase();
|
|
var item = context.Guests.FirstOrDefault(x => x.Id == model.Id);
|
|
if (item == null) return null;
|
|
|
|
item.Update(model);
|
|
context.SaveChanges();
|
|
return item.GetView;
|
|
}
|
|
|
|
public GuestViewModel? Delete(GuestBindingModel model)
|
|
{
|
|
using var context = new HotelDataBase();
|
|
var item = context.Guests.FirstOrDefault(x => x.Id == model.Id);
|
|
if (item == null) return null;
|
|
context.Guests.Remove(item);
|
|
context.SaveChanges();
|
|
return item.GetView;
|
|
}
|
|
} |