public async Task <ActionResult> Create([Bind(Include = "Name,Street,City,PhoneNumber,CheckIn,CheckOut,ConfirmationFileUrl,Price")] Accommodation accommodation) { try { //check valid check-in and check-out if (!accommodation.IsCheckInBeforeCheckOut()) { ModelState.AddModelError("", "Please check the check-in and check-out dates. Check-out cannot be before check-in."); } else if (ModelState.IsValid) { int tripId = Int32.Parse(accommodation.ConfirmationFileUrl); //temporarily storing tripid in confirmationurl Trip trip = await db.Trips.FindAsync(tripId); if (trip.TripOwner != User.Identity.GetUserId()) { return(View("CustomisedError", new HandleErrorInfo( new UnauthorizedAccessException("Oops, this trip doesn't seem to be yours, you cannot add an accommodation to it."), "Trip", "Index"))); } //if before trip start date -> error if (accommodation.CheckIn < trip.StartDate) { ModelState.AddModelError("", "The check-in date is before the trip start date (" + trip.StartDate.ToShortDateString() + "). Please correct."); } else { try { AssignAccommodationToStep(accommodation, trip, true); accommodation.ConfirmationFileUrl = null; db.Accommodations.Add(accommodation); //increase trip budget trip.TotalCost += accommodation.Price; await db.SaveChangesAsync(); //return update view in case user wants to attach confirmation file return(RedirectToAction("Edit", new { id = accommodation.AccommodationId })); } catch (ArgumentException ex) { //give feedback to user about which step to check ViewBag.ErrorMessage = ex.Message; } } } } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, contact the system administrator."); } ViewBag.TripId = accommodation.ConfirmationFileUrl; ViewBag.CheckIn = String.Format("{0:dd-MM-yyyy hh:mm tt}", accommodation.CheckIn); ViewBag.CheckOut = String.Format("{0:dd-MM-yyyy hh:mm tt}", accommodation.CheckOut); return(View(accommodation)); }
public async Task <ActionResult> Create([Bind(Include = "Title,Country,TripCategory,StartDate,Notes")] Trip trip) { try { if (ModelState.IsValid) { trip.TripOwner = User.Identity.GetUserId(); //check if user already has a trip with that name if (await db.Trips.AnyAsync(t => t.TripOwner == trip.TripOwner && t.Title.ToLower() == trip.Title.ToLower())) { ModelState.AddModelError("", "You have already created a trip with that title. Please give this trip a different title."); } else { db.Trips.Add(trip); await db.SaveChangesAsync(); return(RedirectToAction("Details", new { id = trip.TripId })); } } } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, contact the system administrator."); } ViewBag.CountryList = Trip.GetCountries(); return(View(trip)); }
public async Task <ActionResult> Create(StepViewModel stepViewModel) { try { await CheckTripNotNullOrNonOwner(stepViewModel.TripId); } catch (UnauthorizedAccessException ex) { return(View("CustomisedError", new HandleErrorInfo(ex, "Trip", "Index"))); } try { if (ModelState.IsValid) { Step newStep = new Step() { SequenceNo = stepViewModel.SequenceNo, From = stepViewModel.From, To = stepViewModel.To, WalkingTime = stepViewModel.WalkingTimeHours + stepViewModel.WalkingTimeMinutes / 60.0, WalkingDistance = stepViewModel.WalkingDistance, Ascent = stepViewModel.Ascent, Description = stepViewModel.Description, Notes = stepViewModel.Notes, TripId = stepViewModel.TripId }; //retrieve all subsequent steps and update seq no foreach (Step item in db.Steps.Where(s => s.TripId == newStep.TripId && s.SequenceNo >= newStep.SequenceNo)) { item.SequenceNo++; } db.Steps.Add(newStep); await db.SaveChangesAsync(); //retrieve all steps where seq no >= to new step.seq no in an array including new step and assign accommodation of previous step for that seq no Step[] subsequentSteps = await db.Steps.Where(s => s.TripId == newStep.TripId && s.SequenceNo >= newStep.SequenceNo).OrderBy(s => s.SequenceNo).ToArrayAsync(); for (int i = 0; i < subsequentSteps.Length - 1; i++) { subsequentSteps[i].AccommodationId = subsequentSteps[i + 1].AccommodationId; } //set last one to null subsequentSteps[subsequentSteps.Length - 1].AccommodationId = null; await db.SaveChangesAsync(); return(RedirectToAction("Details", new { id = newStep.StepId })); } } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, contact the system administrator."); } ViewBag.SeqNo = stepViewModel.SequenceNo; return(View(stepViewModel)); }
public async Task <ActionResult> Edit(Review review) { //check if the step belongs to authenticated user! Step step = await db.Steps.FindAsync(review.StepId); if (step.Trip.TripOwner != User.Identity.GetUserId()) { return(View("CustomisedError", new HandleErrorInfo( new UnauthorizedAccessException("Oops, this review doesn't seem to be yours, you cannot add nor edit it."), "Trip", "Index"))); } if (ModelState.IsValid) { if (review.ReviewId == 0) { review.ReviewId = review.StepId; db.Reviews.Add(review); } else { db.MarkAsModified(review); } await db.SaveChangesAsync(); return(RedirectToAction("Details", "Step", new { id = review.StepId })); } else { ViewBag.StepId = review.StepId; ViewBag.From = step.From; ViewBag.To = step.To; ViewBag.Create = true; return(View(review)); } }
public async Task <ActionResult> EditLeisure([Bind(Include = "ID,Name,StartTime,Price,Notes,Address,LeisureCategory, StepId")] LeisureActivity leisureActivity) { if (ModelState.IsValid) { //if new activity, add to db if (leisureActivity.ID == 0) { try { await UpdateTripBudgetIfOwner(leisureActivity); db.Activities.Add(leisureActivity); } catch (UnauthorizedAccessException ex) { return(View("CustomisedError", new HandleErrorInfo(ex, "Trip", "Index"))); } } else { try { await UpdateLeisureActivity(leisureActivity); } catch (UnauthorizedAccessException ex) { return(View("CustomisedError", new HandleErrorInfo(ex, "Trip", "Index"))); } } await db.SaveChangesAsync(); return(RedirectToAction("Details", "Step", new { id = leisureActivity.StepId })); } ViewBag.StepId = leisureActivity.StepId; return(View(leisureActivity)); }