示例#1
0
        public async Task <RoomBindingModel> AddRoomAsync([FromBody][Required] RoomBindingModel room)
        {
            var roomInfo = new Room()
            {
                HotelId = room.HotelId,
                Number  = room.Number,
                Type    = room.Type
            };
            IEnumerable <Hotel> hotelResult = await UnitOfWork.Hotel.GetByIdAsync(roomInfo.HotelId);

            List <Hotel> hotels = hotelResult.ToList();

            if (hotels.Any())
            {
                IEnumerable <Room> roomResult = await UnitOfWork.Room.GetByHotelIdByRoomNumberAsync(roomInfo.HotelId, roomInfo.Number);

                List <Room> rooms = roomResult.ToList();
                if (rooms.Any())
                {
                    throw new Exception("Room with such number already exists");
                }
                await UnitOfWork.Room.AddAsync(roomInfo);
            }
            else
            {
                throw new Exception("Provided hotel does not exist");
            }
            room.RoomId = roomInfo.Id;
            return(room);
        }
 private Room CreateModel(RoomBindingModel model, Room room)
 {
     room.Available  = model.Available;
     room.Categoryid = model.Categoryid;
     room.Number     = model.Number.Value;
     return(room);
 }
示例#3
0
        public async Task <IActionResult> AddRoom(RoomBindingModel roomBindingModel)
        {
            // Check that the binding model is valid.
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            // If that's the case, create a model from the binding model.
            var roomCategoryModel = await _roomCategoryService.GetRoomCategoryById(roomBindingModel.RoomCategoryId);

            if (roomCategoryModel == null)
            {
                return(BadRequest("Could not find the room category"));
            }

            var roomModel = new RoomModel
            {
                Name = roomBindingModel.Name,
                RoomCategoryModel = roomCategoryModel
            };
            var successful = await _roomService.AddRoomAsync(roomModel);

            if (!successful)
            {
                return(BadRequest("Could not add the room."));
            }

            return(RedirectToAction("Index"));
        }
 public void Insert(RoomBindingModel model)
 {
     using (var context = new HotelContext())
     {
         context.Room.Add(CreateModel(model, new Room()));
         context.SaveChanges();
     }
 }
示例#5
0
 public async Task AddRoom([FromBody][Required] RoomBindingModel room)
 {
     var roomInfo = new RoomInfo()
     {
         HotelId = room.HotelId,
         Number  = room.Number,
         Type    = room.Type
     };
     await hotelDAO.AddRoomAsync(roomInfo);
 }
 public void Update(RoomBindingModel model)
 {
     using (var context = new HotelContext())
     {
         var element = context.Room.FirstOrDefault(rec => rec.Id == model.Id);
         if (element == null)
         {
             throw new Exception("Клиент не найден");
         }
         CreateModel(model, element);
         context.SaveChanges();
     }
 }
 public RoomViewModel GetElement(RoomBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new HotelContext())
     {
         var client = context.Room.Include(x => x.Category)
                      .FirstOrDefault(rec => rec.Id == model.Id || rec.Number == model.Number);
         return(client != null?CreateFullModel(client) : null);
     }
 }
示例#8
0
        public void Delete(RoomBindingModel model)
        {
            var element = _roomStorage.GetElement(new RoomBindingModel
            {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            _roomStorage.Delete(model);
        }
 public List <RoomViewModel> GetFilteredList(RoomBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new HotelContext())
     {
         return(context.Room.Include(x => x.Category)
                .Where(rec => rec.Available == model.Available)
                .Select(CreateFullModel)
                .ToList());
     }
 }
示例#10
0
 public List <RoomViewModel> Read(RoomBindingModel model)
 {
     if (model == null)
     {
         return(_roomStorage.GetFullList());
     }
     if (model.Id.HasValue || model.Number.HasValue)
     {
         return(new List <RoomViewModel> {
             _roomStorage.GetElement(model)
         });
     }
     return(_roomStorage.GetFilteredList(model));
 }
示例#11
0
 public void CreateOrUpdate(RoomBindingModel model)
 {
     if (model.Id.HasValue)
     {
         _roomStorage.Update(model);
     }
     else
     {
         if (_roomStorage.GetElement(model)?.Number == model.Number)
         {
             throw new Exception("Комната с таким номером уже существует");
         }
         _roomStorage.Insert(model);
     }
 }
 public void Delete(RoomBindingModel model)
 {
     using (var context = new HotelContext())
     {
         Room element = context.Room.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Room.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Клиент не найден");
         }
     }
 }
示例#13
0
        public async Task <IActionResult> AddRoom()
        {
            var roomCategories = await _roomCategoryService.GetAllRoomCategoriesAsync();

            var categoryDescriptions = roomCategories
                                       .Select(cate => new SelectListItem
            {
                Text  = cate.Description,
                Value = cate.Id.ToString()
            }).ToList();

            var roomBindingModel = new RoomBindingModel
            {
                RoomCategoryDescription = categoryDescriptions
            };

            return(View(roomBindingModel));
        }
        public void AddingRoomShouldAddRoom()
        {
            var room = new RoomBindingModel()
            {
                Name = "new room"
            };

            var httpResponse = this.controller.AddRoom(room)
                .ExecuteAsync(CancellationToken.None).Result;

            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);

            var resultRoomName =
                this.unitOfWork.PublicRooms.All()
                .Select(r => r.Name)
                .Last();

            Assert.AreEqual(room.Name, resultRoomName);
        }
示例#15
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxNumber.Text))
            {
                MessageBox.Show("Введите номер комнаты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (CategoryId == null)
            {
                MessageBox.Show("Выберите категорию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int number;

            if (!int.TryParse(textBoxNumber.Text, out number))
            {
                MessageBox.Show("Номер комнаты - целое число", "Ошибка",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                RoomBindingModel model = new RoomBindingModel
                {
                    Categoryid = CategoryId.Value,
                    Available  = true,
                    Number     = number
                };
                if (Id.HasValue)
                {
                    model.Id = Id.Value;
                }
                logicR.CreateOrUpdate(model);
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public IHttpActionResult AddRoom(RoomBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var checkForExistingRoom = this.data.PublicRooms.All()
                .FirstOrDefault(r => r.Name == model.Name);

            if (checkForExistingRoom != null)
            {
                return this.BadRequest("The chat room name already exists.");
            }

            var room = new PublicRoom {Name = model.Name};

            this.data.PublicRooms.Add(room);
            this.data.SaveChanges();

            return this.Ok(room);
        }