Esempio n. 1
0
        public async Task <ActionResult> ReserveRoom(int id, int idrrq)  //RoomID
        {
            try
            {
                using (var db = new ApplicationDbContext())
                {
                    Models.RoomRequest.RoomRequest rRq = await db.RoomRequests.FindAsync(idrrq);

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

                    Models.RoomRequest.RoomRequest rRqUpdate = rRq;
                    rRqUpdate.Reserve(id);
                    db.Entry(rRq).CurrentValues.SetValues(rRqUpdate);
                    db.Entry(rRq.RoomRequestResponse).CurrentValues.SetValues(rRqUpdate.RoomRequestResponse);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Reservations", "Requests", new { id = rRq.Request_ID }));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong!");
                return(View());
            }
        }
        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));
            }
        }
Esempio n. 3
0
        [HttpGet]                                           ///Todo implement Reservation Validation and Edit View appropriate    Preselection  of Profiles
        public async Task <ActionResult> SelectRoom(int id) // RoomRequest id
        {
            try
            {
                using (var db = new ApplicationDbContext())
                {
                    Models.RoomRequest.RoomRequest roomRequest = db.RoomRequests.Find(id);
                    roomRequest.RoomRequestResponse = await db.RoomRequestResponse.FindAsync(roomRequest.RoomRequestResponse_ID);

                    Models.VM.ProfilePreviewVM applicantPrevVM = await ProfilesController.ProfilePreviewVM(roomRequest.ApplyingTraveler_ID);

                    List <Models.RoomRequest.RoomRequest> reservations = await db.RoomRequests.Where(p => p.Request_ID == roomRequest.Request_ID).Include(p => p.RoomRequestResponse).Where(p => p.RoomRequestResponse.responseStatus == Models.RoomRequest.RoomRequestResponseStatus.Reserved).ToListAsync();

                    Models.Request.Request rq = db.Requests.Where(p => p.ID == roomRequest.Request_ID).Include(p => p.Accommodation).Include(p => p.Accommodation.BedRooms).FirstOrDefault();
                    Models.RoomRequest.RoomRequestVMOwner applicantVM = new Models.RoomRequest.RoomRequestVMOwner(roomRequest, applicantPrevVM);

                    Models.Request.SelectRoomVM sRVM = new Models.Request.SelectRoomVM(applicantVM, reservations, rq.Accommodation.BedRooms);
                    return(View(sRVM));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong!");
                return(View());
            }
        }
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            try
            {
                using (var db = new ApplicationDbContext())
                {
                    Models.RoomRequest.RoomRequest         roomRequest = db.RoomRequests.Find(id);
                    Models.RoomRequest.RoomRequestResponse response    = db.RoomRequestResponse.Find(roomRequest.RoomRequestResponse_ID);
                    roomRequest.Delete();
                    response.Canceled();
                    db.Entry(response).State    = EntityState.Modified;
                    db.Entry(roomRequest).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    NotificationManager.AddNotification(NotificationType.Success, "Room request has been deleted!");
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong!");
                return(RedirectToAction("Index"));
            }
        }
        //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> Propose(int id, int rqid) // id RoomRequestID
        {
            try
            {
                TravelerProfile travelerProfile = await ProfileManager.LoadUserAndTravelerProfile(User);

                using (var db = new ApplicationDbContext())
                {
                    Models.RoomRequest.RoomRequest roomRequest = await db.RoomRequests.FindAsync(id); //test with null!!!

                    if (Check.IsNull(roomRequest, "Propose"))
                    {
                        return(RedirectToAction("SelectFlatmates", new { id = rqid, update = 0, rrqid = 0 }));
                    }

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

                    if (Check.IsNull(request, "Propose"))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                    if (roomRequest.IsDeleted("Propose") || request.IsDeleted("Propose")) //Todo delete RoomRequest if request is deleted
                    {
                        NotificationManager.AddNotification(NotificationType.Warning, "RoomRequest is not valid, Request might has been deleted");
                        return(RedirectToAction("Index", "Home"));
                    }
                    if (request.RequestOwner_ID != travelerProfile.ID)
                    {
                        NotificationManager.AddException(new Exception("Access Violation @Propose, RoomRqCtrl", new AccessViolationException()));
                        return(RedirectToAction("Index", "Home"));
                    }
                    Models.RoomRequest.RoomRequestVMOwner roomRequestVM = new Models.RoomRequest.RoomRequestVMOwner(roomRequest);
                    return(View(roomRequestVM));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                return(RedirectToAction("SelectFlatmates", new { id = rqid, update = 0, rrqid = 0 }));
            }
        }
        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));
        }
        // GET: RoomRequests/Details/5
        public async Task <ActionResult> DetailsHost(int?id)
        {
            if (Check.IsNull(id))
            {
                return(View());
            }
            using (var db = new ApplicationDbContext())
            {
                Models.RoomRequest.RoomRequest roomRequest = await db.RoomRequests.FindAsync(id);

                if (Check.IsNull(roomRequest, "DetailsRoomRqOwner"))
                {
                    return(View());
                }
                roomRequest.RoomRequestResponse = await db.RoomRequestResponse.FindAsync(roomRequest.RoomRequestResponse_ID);

                Models.VM.ProfilePreviewVM applPreVM = await ProfilesController.ProfilePreviewVM(roomRequest.ApplyingTraveler_ID);

                Models.RoomRequest.RoomRequestVMOwner roomRequestVM = new Models.RoomRequest.RoomRequestVMOwner(roomRequest, applPreVM);
                return(View(roomRequestVM));
            }
        }
        public async Task <ActionResult> Propose(Models.RoomRequest.RoomRequestVMOwner roomRqVM) // id RoomRequestID
        {
            try
            {
                TravelerProfile tp = await ProfileManager.LoadUserAndTravelerProfile(User);

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

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

                    if (request.RequestOwner_ID != tp.ID)
                    {
                        throw new Exception("Access Violation", new AccessViolationException());
                    }
                    roomRequest.RoomRequestResponse = await db.RoomRequestResponse.FindAsync(roomRequest.RoomRequestResponse_ID);

                    Models.RoomRequest.RoomRequest roomRequestUpdate = roomRequest;

                    roomRequestUpdate.SetValues(roomRqVM);
                    db.Entry(roomRequest).CurrentValues.SetValues(roomRequestUpdate);
                    db.Entry(roomRequest.RoomRequestResponse).CurrentValues.SetValues(roomRequestUpdate.RoomRequestResponse);
                    await db.SaveChangesAsync();

                    NotificationManager.AddNotification(NotificationType.Success, "Your proposal has been saved!");
                    return(RedirectToAction("SelectFlatmates", new { id = roomRqVM.Request_ID, update = 0, rrqid = 0 }));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong");
                return(RedirectToAction("SelectFlatmates", new { id = roomRqVM.Request_ID, update = 0, rrqid = 0 }));
            }
        }
        public async Task <ActionResult> SelectFlatmates(int id, int update, int rrqid)  //id RequestID
        {
            try
            {
                TravelerProfile tp = await ProfileManager.LoadUserAndTravelerProfile(User);


                using (var db = new ApplicationDbContext())
                {
                    Models.Request.Request request = await db.Requests.FindAsync(id);

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

                    if (Check.IsNull(request, "UpdateSelectFlatmates"))
                    {
                        return(RedirectToAction("Index", "Requests"));
                    }
                    if (request.RequestOwner_ID != tp.ID)
                    {
                        NotificationManager.AddException(new Exception("User is not RequestOwner, Access Violation, AcceptTraveler", new AccessViolationException()));
                        return(RedirectToAction("Index", "Home"));
                    }
                    if (update != 0 && rrqid != 0)
                    {
                        Models.RoomRequest.RoomRequest roomRequest = await db.RoomRequests.FindAsync(rrqid);

                        if (Check.IsNull(roomRequest, "UpdateSelectFlatmates"))
                        {
                            return(RedirectToAction("Index", "Requests"));
                        }
                        roomRequest.RoomRequestResponse = await db.RoomRequestResponse.FindAsync(roomRequest.RoomRequestResponse_ID);

                        if (roomRequest.IsDeleted("RoomRequest") || request.IsDeleted("RoomRequest"))
                        {
                            NotificationManager.AddNotification(NotificationType.Warning, "RoomRequest is not valid, Request might has been been deleted");
                            return(RedirectToAction("Index", "Home"));
                        }

                        var trackedRoomRq      = roomRequest;
                        var trackedRRqResponse = roomRequest.RoomRequestResponse;

                        if (update > 0)
                        {
                            roomRequest.RoomRequestResponse.Approve();
                            NotificationManager.AddNotification(NotificationType.Success, "Flatmate approved!");
                        }
                        if (update < 0)
                        {
                            roomRequest.RoomRequestResponse.Decline();
                            NotificationManager.AddNotification(NotificationType.Success, "Flatmate declined!");
                        }

                        db.Entry(trackedRoomRq).CurrentValues.SetValues(roomRequest);
                        db.Entry(trackedRRqResponse).CurrentValues.SetValues(trackedRRqResponse);
                        await db.SaveChangesAsync();
                    }

                    IQueryable <Models.RoomRequest.RoomRequest> roomRequestQuery = db.RoomRequests.Where(p => p.Request_ID == id).Include(p => p.RoomRequestResponse).Where(p => p.RoomRequestResponse.responseStatus == Models.RoomRequest.RoomRequestResponseStatus.Pending);
                    List <Models.RoomRequest.RoomRequest>       roomRequestList  = await roomRequestQuery.ToListAsync();

                    List <Models.RoomRequest.RReqVMListItemRqOwn> roomRequestVMList = new List <Models.RoomRequest.RReqVMListItemRqOwn>();
                    foreach (var roomRq in roomRequestList)
                    {
                        if (roomRq.IsValid())
                        {
                            Models.VM.ProfilePreviewVM applicantVMPrev = await ProfilesController.ProfilePreviewVM(roomRq.ApplyingTraveler_ID);

                            roomRequestVMList.Add(new Models.RoomRequest.RReqVMListItemRqOwn(roomRq, request, applicantVMPrev));
                        }
                    }
                    string rrqstats = await RoomRequestStats(request);

                    Models.RoomRequest.RReqVMListRqOwn SelectFM = new Models.RoomRequest.RReqVMListRqOwn(request.ID, roomRequestVMList, rrqstats);

                    return(View(SelectFM));
                }
            }
            catch (Exception e)
            {
                NotificationManager.AddException(e);
                NotificationManager.AddNotification(NotificationType.Error, "Oops, something went wrong");
                return(RedirectToAction("Index", "Requests"));
            }
        }
        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));
        }