public async Task <IActionResult> Details(int?id) { try { var spot = await _spotService.GetSingle(id); var isAuthorized = await _authorizationService.AuthorizeAsync(User, spot, Operation.Read); if (isAuthorized.Succeeded) { return(View(spot)); } return(Forbid()); } catch (BusinessException e) { Console.WriteLine(e); throw; } }
public async Task <ActionResult <Spot> > GetSpot(int id) { var spot = await _spotService.GetSingle(id); if (spot is not null) { var isAuthorized = await _authorizationService.AuthorizeAsync(User, spot, Operation.Read); if (isAuthorized.Succeeded) { return(Ok(spot)); } else { return(StatusCode(403)); } } else { return(NotFound()); } }
public async Task <ActionResult <Booking> > CreateBookingLocally(int boatId, int spotId, string start, string end) { var startDate = DateTime.Parse(start); var endDate = DateTime.Parse(end); // Find boat & spot objects in db var boat = await _boatService.GetSingle(boatId); var spot = await _spotService.GetSingle(spotId); // Check whether the logged user owns the boat var isAuthorized = await _authorizationService.AuthorizeAsync(User, boat, Operation.Book); if (!isAuthorized.Succeeded) { return(Unauthorized()); } // get booking from session if created before var booking = HttpContext.Session.Get <Booking>("Booking"); // Check whether booking is consistent, and if not, reinitialize if (booking is null || booking.BookingReferenceNo == 0 || booking.BoatId != boatId) { booking = new Booking { BoatId = boatId }; await _bookingService.Create(booking); } // If the spot fits the boat if (HelperMethods.DoesSpotFitBoat(boat, spot)) { // And the selected dates are valid if (HelperMethods.AreDatesValid(startDate, endDate)) { // Next 5 lines make sure that no dates overlap in the // booking's booking lines You cannot physically be in two // places at the same time bool areBookingLinesDatesValid = true; foreach (BookingLine bookingLine in booking.BookingLines) { if (HelperMethods.AreDatesIntersecting(bookingLine.StartDate, bookingLine.EndDate, startDate, endDate)) { areBookingLinesDatesValid = false; } } // Finally, if all conditions are met if (areBookingLinesDatesValid) { // Add bookingLine to the booking lines inside the booking booking = _bookingService.CreateBookingLine(booking, startDate, endDate, spot); } } } // store booking object in the session // don't yet know whether you rewrite value if you add it with the same key or if it needs to be removed first //HttpContext.Session.Remove("Booking"); HttpContext.Session.Set("Booking", booking); // hopefully serialization is not needed and returns booking in json format return(Ok(booking)); }