80 lines
2.2 KiB
C#
80 lines
2.2 KiB
C#
|
using HotelContracts.BindingModels;
|
|||
|
using HotelContracts.SearchModels;
|
|||
|
using HotelContracts.StoragesContracts;
|
|||
|
using HotelContracts.ViewModels;
|
|||
|
using HotelDatabaseImplement.Models;
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Type))
|
|||
|
{
|
|||
|
return new List<RoomViewModel>();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new HotelDataBase();
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|