public async Task <ActionResult <Marina> > GetMarina(int id) { var spot = await _marinaService.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 <IList <Spot> > GetAvailableSpots(int marinaId, int boatId, DateTime startDate, DateTime endDate) { IList <Spot> availableSpots = new List <Spot>(); var marina = await _marinaService.GetSingle(marinaId); var boat = await _context.Boats.FindAsync(boatId); // Dates are valid if endDate is later, or on the same day, as startDate and if they are // today or later if (HelperMethods.AreDatesValid(startDate, endDate)) { foreach (Spot spot in marina.Spots) { if (spot.Available) { if (HelperMethods.DoesSpotFitBoat(boat, spot)) { // Only go through Booking Lines that end later than "Now" - does not go // through past bookings var booked = false; foreach (BookingLine bookingLine in spot.BookingLines.Where <BookingLine>(bL => bL.EndDate > DateTime.Now)) { if (HelperMethods.AreDatesIntersecting(bookingLine.StartDate, bookingLine.EndDate, startDate, endDate)) { // Basically returns all spots that // 1. Fit the boat // 2. Have NO date intersects with any existing bookings with no // optimizations in mind whatsoever 🙂 booked = true; break; } } if (!booked) { availableSpots.Add(spot); } } } } } return(availableSpots); }
public async Task <ActionResult <Spot> > PostSpot(Spot spot) { if (spot.SpotId != 0 || spot.Location?.LocationId != 0) { BadRequest("Do not set the ID"); } if (spot.MarinaId is not null) { var marina = await _marinaService.GetSingle(spot.MarinaId); var isAuthorized = await _authorizationService.AuthorizeAsync(User, marina, Operation.Update); if (!isAuthorized.Succeeded) { return(StatusCode(403)); } } await _spotService.Create(spot); await _spotService.Save(); return(CreatedAtAction("GetSpot", new { id = spot.SpotId }, spot)); }
public async Task <IActionResult> Details(int?id) { try { var marina = await _marinaService.GetSingle(id); var isAuthorized = await _authorizationService.AuthorizeAsync(User, marina, Operation.Read); if (isAuthorized.Succeeded) { return(View(marina)); } return(Forbid()); } catch (BusinessException e) { Console.WriteLine(e); throw; // Custom 404 page if you wanna be fancy //Replace this is a proper return view } }