/// <summary> /// Remove rooms /// </summary> public async Task <ServiceResult> RemoveRangeAsync(RemoveRoomsModel model) { try { RemoveRoomModel removeRoomModel = new RemoveRoomModel() { MoveBookings = model.MoveBookings, NewRoomId = model.NewRoomId }; foreach (int roomId in model.RoomIds) { await RemoveAsync(roomId, removeRoomModel); } return(ServiceResult.Success(model.RoomIds)); } catch (Exception e) { return(ServiceResult.Error(e.Message, HttpStatusCode.InternalServerError)); } }
/// <summary> /// Remove the room /// </summary> public async Task <ServiceResult> RemoveAsync(int id, RemoveRoomModel model) { try { Room room = await _roomRepository.Table.Include(e => e.Bookings) .AsNoTracking() .FirstOrDefaultAsync(e => e.Id == id); if (room == null) { return(ServiceResult.Error(ErrorMessages.NotFound, HttpStatusCode.NotFound)); } // If we are looking for transferring the bookings to another room if (model != null && model.MoveBookings) { foreach (var booking in room.Bookings.Select(b => new Booking(b.PersonId, model.NewRoomId, b.StartDate, b.EndDate))) { _bookingRepository.Add(booking); } } var local = _roomRepository.Context.Set <Room>().Local.FirstOrDefault(e => e.Id == id); if (local != null) { _roomRepository.Context.Entry(local).State = EntityState.Detached; } _roomRepository.Remove(room); await _roomRepository.SaveChangesAsync(); return(ServiceResult.Success(room.Id)); } catch (Exception e) { return(ServiceResult.Error(e.Message, HttpStatusCode.InternalServerError)); } }