2023-04-07 13:46:29 +04:00
|
|
|
|
using HotelContracts.BindingModels;
|
|
|
|
|
using HotelContracts.SearchModels;
|
|
|
|
|
using HotelContracts.StoragesContracts;
|
|
|
|
|
using HotelContracts.ViewModels;
|
|
|
|
|
using HotelDatabaseImplement.Models;
|
2023-05-19 17:52:18 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-07 13:46:29 +04:00
|
|
|
|
|
|
|
|
|
namespace HotelDatabaseImplement.Implements;
|
|
|
|
|
|
|
|
|
|
public class RoomStorage : IRoomStorage
|
|
|
|
|
{
|
|
|
|
|
public List<RoomViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
return context.Rooms
|
|
|
|
|
.Select(x => x.GetView)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RoomViewModel> GetFilteredList(RoomSearchModel model)
|
|
|
|
|
{
|
2023-05-19 17:52:18 +04:00
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
if (model.IsReserved.HasValue)
|
2023-04-07 13:46:29 +04:00
|
|
|
|
{
|
2023-05-19 17:52:18 +04:00
|
|
|
|
return context.Rooms
|
|
|
|
|
.Where(x => x.IsReserved == model.IsReserved)
|
|
|
|
|
.Select(x => x.GetView)
|
|
|
|
|
.ToList();
|
2023-04-07 13:46:29 +04:00
|
|
|
|
}
|
2023-05-19 17:52:18 +04:00
|
|
|
|
|
2023-04-07 13:46:29 +04:00
|
|
|
|
if (model.Cost.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Rooms
|
|
|
|
|
.Where(x => x.Cost <= model.Cost)
|
|
|
|
|
.Select(x => x.GetView)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return context.Rooms
|
|
|
|
|
.Where(x => x.Type == model.Type)
|
|
|
|
|
.Select(x => x.GetView)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RoomViewModel? GetElement(RoomSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue) return null;
|
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
return context.Rooms.FirstOrDefault(x => x.Id == model.Id)?.GetView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RoomViewModel? Insert(RoomBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var item = Room.Create(model);
|
|
|
|
|
if (item == null) return null;
|
|
|
|
|
|
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
context.Rooms.Add(item);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return item.GetView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RoomViewModel? Update(RoomBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
var item = context.Rooms.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (item == null) return null;
|
|
|
|
|
|
|
|
|
|
item.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return item.GetView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RoomViewModel? Delete(RoomBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
var item = context.Rooms.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (item == null) return null;
|
|
|
|
|
context.Rooms.Remove(item);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return item.GetView;
|
|
|
|
|
}
|
|
|
|
|
}
|