private bool ValidateRoomRq(Models.RoomRequest.RoomRequestVMUser roomRequestVM)
        {
            //ChecktimeValidation, must be here, due to string dates in VM
            DateTime checkinRRq  = roomRequestVM.CheckIn;
            DateTime checkoutRRq = roomRequestVM.CheckOut;

            DateTime checkinRq   = roomRequestVM.CheckInRequest;
            DateTime checkoutRq  = roomRequestVM.CheckOutRequest;
            bool     checkinFail = false;

            if (checkinRRq < checkinRq || checkinRRq > checkoutRq)
            {
                ModelState.AddModelError("CheckIn", "CheckIn must be in valid requesttime");
                checkinFail = true;
            }

            if (checkoutRRq < checkinRq || checkoutRRq > checkoutRq)
            {
                ModelState.AddModelError("CheckOut", "CheckOut must be in valid requesttime");
                checkinFail = true;
            }
            if (!roomRequestVM.DoubleBed && !roomRequestVM.SingleBed && !roomRequestVM.Nevermind)
            {
                ModelState.AddModelError("DoubleBed", "Please select a bed type");
                checkinFail = true;
            }
            if (roomRequestVM.Price == null || Models.Request.Request.Price(roomRequestVM.Price) == 0)
            {
                ModelState.AddModelError("Price", "Please insert a price!");
                checkinFail = true;
            }
            return(checkinFail);
        }
        public async Task <ActionResult> Delete(int?id)
        {
            if (Check.IsNull(id))
            {
                return(RedirectToAction("Index"));
            }
            TravelerProfile travelerProfile = await ProfileManager.LoadUserAndTravelerProfile(User);

            using (var db = new ApplicationDbContext())
            {
                Models.RoomRequest.RoomRequest roomRequest = db.RoomRequests.Find(id);
                roomRequest.RoomRequestResponse = db.RoomRequestResponse.Find(roomRequest.RoomRequestResponse_ID);
                if (Check.IsNull(roomRequest, "RoomRequest Delete"))
                {
                    return(RedirectToAction("Index"));
                }
                if (roomRequest.ApplyingTraveler_ID != travelerProfile.ID)
                {
                    NotificationManager.AddException(new Exception("AccessViolation Roomrq Delete"));
                    return(RedirectToAction("Index"));
                }
                Models.Request.Request rq = await db.Requests.FindAsync(roomRequest.Request_ID);

                rq.Accommodation = await AccommodationsController.LoadAccommodation(rq.Accommodation_ID);

                Models.VM.ProfilePreviewVM host = await ProfilesController.ProfilePreviewVM(rq.RequestOwner_ID);

                Models.RoomRequest.RoomRequestVMUser roomRequestVM = new Models.RoomRequest.RoomRequestVMUser(roomRequest, rq, host);
                return(View(roomRequestVM));
            }
        }
        //GET: RoomRequests/Details/5
        public async Task <ActionResult> Details(int id)
        {
            try
            {
                using (var db = new ApplicationDbContext())
                {
                    Models.RoomRequest.RoomRequest roomRequest = await db.RoomRequests.FindAsync(id);

                    roomRequest.RoomRequestResponse = await db.RoomRequestResponse.FindAsync(roomRequest.RoomRequestResponse_ID);

                    if (Check.IsNull(roomRequest, "DetailsRoomRq"))
                    {
                        NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong");
                        return(View());
                    }
                    Models.Request.Request rq = await db.Requests.FindAsync(roomRequest.Request_ID);

                    rq.Accommodation = await AccommodationsController.LoadAccommodation(rq.Accommodation_ID);

                    Models.VM.ProfilePreviewVM host = await ProfilesController.ProfilePreviewVM(rq.RequestOwner_ID);

                    Models.RoomRequest.RoomRequestVMUser roomRequestVM = new Models.RoomRequest.RoomRequestVMUser(roomRequest, rq, host);
                    return(View(roomRequestVM));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong");
                return(RedirectToAction("Index", "Home"));
            }
        }
        public async Task <ActionResult> Edit(int?id) //id roomRq
        {
            try
            {
                if (!id.HasValue)
                {
                    return(RedirectToAction("Search", "Searches"));
                }
                TravelerProfile travelerProfile = await ProfileManager.LoadUserAndTravelerProfile(User);

                using (var db = new ApplicationDbContext())
                {
                    Models.RoomRequest.RoomRequest roomRequest = await db.RoomRequests.FindAsync(id);

                    if (Check.IsNull(roomRequest, "Edit roomRequest"))
                    {
                        return(RedirectToAction("Index"));
                    }
                    Models.Request.Request request = await db.Requests.FindAsync(roomRequest.Request_ID);

                    request.Accommodation = await AccommodationsController.LoadAccommodation(request.Accommodation_ID);

                    if (!request.IsValid())
                    {
                        NotificationManager.AddNotification(NotificationType.Info, "The request is invalid, you can't edit your roomrequest anymore");
                        return(RedirectToAction("Index"));
                    }
                    if (roomRequest.ApplyingTraveler_ID != travelerProfile.ID)
                    {
                        NotificationManager.AddException(new Exception("Access violation, this is not your Roomrequest! Edit RoomRequest", new AccessViolationException()));

                        return(RedirectToAction("Index"));
                    }
                    roomRequest.RoomRequestResponse = db.RoomRequestResponse.Find(roomRequest.RoomRequestResponse_ID);

                    if (roomRequest.RoomRequestResponse.IsApproved())
                    {
                        NotificationManager.AddNotification(NotificationType.Info, "You cannot edit an approved roomrequest!");
                        return(RedirectToAction("Index"));
                    }
                    if (roomRequest.RoomRequestResponse.IsReserved())
                    {
                        NotificationManager.AddNotification(NotificationType.Info, "You cannot edit a reserved roomrequest!");
                        return(RedirectToAction("Index"));
                    }
                    Models.VM.ProfilePreviewVM host = await ProfilesController.ProfilePreviewVM(request.RequestOwner_ID);

                    Models.RoomRequest.RoomRequestVMUser roomRequestEditVM = new Models.RoomRequest.RoomRequestVMUser(roomRequest, request, null);

                    return(View(roomRequestEditVM));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                return(RedirectToAction("Index"));
            }
        }
        public async Task <ActionResult> Edit(Models.RoomRequest.RoomRequestVMUser roomRequestVM, string accept, string save)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (var db = new ApplicationDbContext())
                    {
                        var trackedRoomRq = await db.RoomRequests.FindAsync(roomRequestVM.ID);

                        trackedRoomRq.RoomRequestResponse = await db.RoomRequestResponse.FindAsync(trackedRoomRq.RoomRequestResponse_ID);

                        Models.RoomRequest.RoomRequest rRqUpdate = trackedRoomRq;
                        bool update = false;
                        if (!string.IsNullOrEmpty(accept))
                        {
                            rRqUpdate.AcceptProposal();
                            update = true;
                        }
                        if (!string.IsNullOrEmpty(save))
                        {
                            rRqUpdate.SetValues(roomRequestVM);
                            update = true;
                        }
                        if (update)
                        {
                            db.Entry(trackedRoomRq).CurrentValues.SetValues(rRqUpdate);
                            db.Entry(trackedRoomRq.RoomRequestResponse).CurrentValues.SetValues(rRqUpdate.RoomRequestResponse);
                            await db.SaveChangesAsync();
                        }
                        return(RedirectToAction("Index"));
                    }
                }
                catch (Exception e)
                {
                    NotificationManager.AddException(e);
                    NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong!");
                }
            }
            return(View(roomRequestVM));
        }
        public async Task <ActionResult> Create(Models.RoomRequest.RoomRequestVMUser roomRequestVM)
        {
            if (roomRequestVM.RequestOwner)
            {
                ModelState.Remove("Text");
                ModelState.Remove("CheckIn");
                ModelState.Remove("CheckOut");
            }
            ModelState.Remove("Price");
            if (ModelState.IsValid)
            {
                if (roomRequestVM.RequestOwner)
                {
                    if ((!roomRequestVM.SingleBed && !roomRequestVM.DoubleBed)) //No beds needed
                    {
                        return(RedirectToAction("Index", "Requests"));
                    }
                }
                if (!roomRequestVM.RequestOwner)
                {
                    if (ValidateRoomRq(roomRequestVM))
                    {
                        return(View(roomRequestVM));
                    }
                }

                try
                {
                    TravelerProfile tp = await ProfileManager.LoadUserAndTravelerProfile(User);

                    using (var db = new ApplicationDbContext())
                    {
                        ///Save new RoomRequest
                        Models.RoomRequest.RoomRequest roomRequest = new Models.RoomRequest.RoomRequest(roomRequestVM);
                        roomRequest.CheckInOwner        = roomRequest.CheckInUser;
                        roomRequest.CheckOutOwner       = roomRequest.CheckOutUser;
                        roomRequest.ApplyingTraveler_ID = tp.ID;
                        roomRequest.RoomRequestResponse = new Models.RoomRequest.RoomRequestResponse();
                        roomRequest.RoomRequestResponse.Init();
                        if (roomRequestVM.RequestOwner) //RequestOwner requests a room
                        {
                            roomRequest.RoomRequestResponse.Approve();
                        }

                        db.RoomRequests.Add(roomRequest);

                        await db.SaveChangesAsync();

                        NotificationManager.AddNotification(NotificationType.Success, "You have successfully applied for a room!");
                        if (roomRequestVM.RequestOwner)
                        {
                            return(RedirectToAction("Index", "Requests"));
                        }


                        return(RedirectToAction("Search", "Searches")); //Todo: Search has to redirected to action with parameters
                    }
                }
                catch (Exception e)
                {
                    NotificationManager.AddException(e);
                    return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
                }
            }
            return(View(roomRequestVM));
        }
        public async Task <ActionResult> Create(int?id) //Request id
        {
            try
            {
                if (!id.HasValue)
                {
                    return(RedirectToAction("Search", "Searches"));
                }
                TravelerProfile tp = await ProfileManager.LoadUserAndTravelerProfile(User);

                using (var db = new ApplicationDbContext())
                {
                    TravelerProfileSearchSession searchSession = await db.LikibuSearchSession.FindAsync(tp.TravelerSearchID); //to fill checkDates, Location

                    Models.Request.Request request = await db.Requests.FindAsync(id);

                    request.Accommodation = await AccommodationsController.LoadAccommodation(request.Accommodation_ID);

                    if (Check.IsNull(request, "Create RoomRequest"))
                    {
                        NotificationManager.AddNotification(NotificationType.Warning, "Request is not valid");
                        return(RedirectToAction("Search", "Searches"));
                    }
                    if (request.IsDeleted("CreateRoomRequest, rq deleted"))
                    {
                        NotificationManager.AddNotification(NotificationType.Warning, "Request is not valid");
                        return(RedirectToAction("Search", "Searches"));
                    }
                    if (request.IsClosed()) //dann keine RoomRequests mehr
                    {
                        NotificationManager.AddNotification(NotificationType.Info, "You can't apply for closed requests");
                        return(RedirectToAction("Search", "Searches"));
                    }
                    //calc Valid Time here
                    DateTime CheckIn     = request.CheckIn;
                    DateTime CheckOut    = request.CheckOut;
                    DateTime chckinUser  = DateTime.Parse(searchSession.CheckIn);
                    DateTime chckoutUser = DateTime.Parse(searchSession.CheckOut);
                    if (chckinUser < CheckIn)
                    {
                        CheckIn = chckinUser;
                    }

                    if (chckoutUser > CheckOut)
                    {
                        CheckOut = chckoutUser;
                    }


                    Models.RoomRequest.RoomRequestVMUser roomRequestVM = new Models.RoomRequest.RoomRequestVMUser(CheckIn, CheckOut, request);
                    if (tp.ID == request.RequestOwner_ID)
                    {
                        roomRequestVM.RequestOwner = true;
                    }
                    return(View(roomRequestVM));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }
        }