CreateReservation([FromBody] ReservationPostDto reservation) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var availability = await _context.Availabilities .Include(a => a.Reservation) .Include(a => a.Venue) .FirstOrDefaultAsync( a => a.Date == reservation.EventDate && a.VenueCode == reservation.VenueCode); if (availability == null || availability.Reservation != null) { return(BadRequest("Venue is not available on the requested date.")); } availability.Reservation = new Reservation { Reference = $"{availability.VenueCode}{availability.Date:yyyyMMdd}", EventDate = availability.Date, VenueCode = availability.VenueCode, WhenMade = DateTime.Now, StaffId = reservation.StaffId }; await _context.SaveChangesAsync(); return(CreatedAtAction("GetReservation", new { reference = availability.Reservation.Reference }, ReservationGetDto.FromModel(availability.Reservation))); }
public async Task <IActionResult> VenueCancel(int?id, string reference) { if (!id.HasValue || reference == null) { return(NotFound()); } ReservationGetDto reservation = await _reservations.GetReservation(reference); if (reservation.Reference == null) { return(NotFound()); } Event @event = await _context.Events.FindAsync(id); if (@event == null || @event.Cancelled) { return(NotFound()); } await _reservations.CancelReservation(reference); return(RedirectToAction(nameof(Venue), new { id })); }
public async Task <IActionResult> GetReservation([FromRoute] string reference) { var reservation = await _context.Reservations.FindAsync(reference); if (reservation == null) { return(NotFound()); } return(Ok(ReservationGetDto.FromModel(reservation))); }
public async Task <IActionResult> GetReservation([FromRoute] string reference) { var reservation = await _context.Reservations .Include(r => r.Availability) .ThenInclude(a => a.Venue) .FirstOrDefaultAsync(r => r.Reference == reference); if (reservation == null) { return(NotFound()); } return(Ok(ReservationGetDto.FromModel(reservation))); }
public async Task <IActionResult> DeleteReservation([FromRoute] string reference) { var reservation = await _context.Reservations.FindAsync(reference); if (reservation == null) { return(NotFound()); } _context.Reservations.Remove(reservation); await _context.SaveChangesAsync(); return(Ok(ReservationGetDto.FromModel(reservation))); }
public async Task <IActionResult> DeleteReservation([FromRoute] string reference) { var reservation = await _context.Reservations .Include(r => r.Availability) .ThenInclude(a => a.Venue) .FirstOrDefaultAsync(r => r.Reference == reference); if (reservation == null) { return(NotFound()); } var dto = ReservationGetDto.FromModel(reservation); _context.Reservations.Remove(reservation); await _context.SaveChangesAsync(); return(Ok(dto)); }
public async Task <ReservationGetDto> GetReservation(string reference) { EnsureClient(); ReservationGetDto reservation; try { var response = await _client.GetAsync("api/reservations/" + reference); response.EnsureSuccessStatusCode(); string responseStr = await response.Content.ReadAsStringAsync(); reservation = await response.Content.ReadAsAsync <ReservationGetDto>(); } catch (HttpRequestException ex) { _logger.LogError("Caught an error when accessing a reservation at reference " + reference + ". Exception: " + ex); reservation = new ReservationGetDto(); } return(reservation); }
public async Task <ReservationGetDto> CreateReservation(DateTime eventDate, string venueCode) { EnsureClient(); ReservationPostDto reservationDetails = new ReservationPostDto() { EventDate = eventDate, StaffId = "1", VenueCode = venueCode }; ReservationGetDto reservation; try { var response = await _client.PostAsJsonAsync("api/reservations", reservationDetails); response.EnsureSuccessStatusCode(); reservation = await response.Content.ReadAsAsync <ReservationGetDto>(); } catch (HttpRequestException ex) { _logger.LogError("Caught an error when creating a reservation. Exception: " + ex); reservation = new ReservationGetDto(); } return(reservation); }
public async Task <IActionResult> Venue(int?id, [Bind("Id,Date,SelectedVenue,TypeId")] EventVenueViewModel ev) { if (!ModelState.IsValid || !id.HasValue) { return(View(ev)); } if (ev.Id != id) { return(NotFound()); } Event @event = await _context.Events.FindAsync(id); if (@event == null || @event.Cancelled) { return(NotFound()); } // Create the reservation. ReservationGetDto reservationGetDto = await _reservations.CreateReservation(ev.Date, ev.SelectedVenue); // Empty view model in case reservation fails. EventVenueViewModel model = new EventVenueViewModel() { Id = ev.Id, Date = ev.Date, Title = @event.Title, Duration = @event.Duration, TypeId = ev.TypeId, TypeTitle = (await _eventTypes.GetEventType(ev.TypeId)).Title }; if (reservationGetDto.Reference == null) { return(View(model)); } // Assign reservation information. model.Reservation = new EventReservationViewModel() { EventDate = reservationGetDto.EventDate, Reference = reservationGetDto.Reference, //StaffId = reservationGetDto.StaffId, // Read above for the same reasoning for the removal of this property. VenueCode = reservationGetDto.VenueCode, WhenMade = reservationGetDto.WhenMade }; // Get venue information from any other availabilities of the same VenueCode (due to the lack of // information on the API). List <AvailabilityApiGetDto> avail = await _availabilities .GetAvailabilities(ev.TypeId, new DateTime(2018, 07, 10), new DateTime(2019, 2, 10)); Venue venue = avail .Where(x => x.Code == reservationGetDto.VenueCode) .Select(a => new Venue() { Code = a.Code, Capacity = a.Capacity, Description = a.Description, Name = a.Name }) .FirstOrDefault(); model.Venue = venue; // Update reservation on the database entity. @event.VenueReservation = reservationGetDto.Reference; model.ReservationReference = reservationGetDto.Reference; await _context.SaveChangesAsync(); // Push database changes. // Show that view model from before. return(View(model)); }
public async Task <IActionResult> Venue(int?id) { if (!id.HasValue) { return(NotFound()); } Event @event = await _context.Events.Where(x => !x.Cancelled) .FirstOrDefaultAsync(m => m.Id == id); if (@event == null) { return(NotFound()); } if (@event.VenueReservation != null) { ReservationGetDto reservation = await _reservations.GetReservation(@event.VenueReservation); if (reservation.Reference == null) { goto NO_RES; } // HACKY WORKAROUND List <AvailabilityApiGetDto> avail = await _availabilities .GetAvailabilities(@event.TypeId, new DateTime(2018, 07, 10), new DateTime(2019, 2, 10)); Venue venue = avail .Where(x => x.Code == reservation.VenueCode) .Select(a => new Venue() { Code = a.Code, Capacity = a.Capacity, Description = a.Description, Name = a.Name }).FirstOrDefault(); if (venue == null) { return(BadRequest()); // Unfortunately ran out of valid venues for this type due to some scuffed design preventing us doing it properly. } EventVenueViewModel reservedViewModel = new EventVenueViewModel() { Title = @event.Title, Date = @event.Date, Duration = @event.Duration, Id = @event.Id, TypeId = @event.TypeId, TypeTitle = (await _eventTypes.GetEventType(@event.TypeId)).Title, Reservation = new EventReservationViewModel() { Reference = reservation.Reference, //StaffId = reservation.StaffId, // In the case of StaffId, it makes little sense having to include a single staff for a reservation // when many staff can be assigned to an event and be added/removed at will. WhenMade = reservation.WhenMade, VenueCode = reservation.VenueCode, EventDate = reservation.EventDate }, Venue = venue, ReservationReference = reservation.Reference }; return(View(reservedViewModel)); } NO_RES: // Label to redirect execution out of the if statement and to avoid nested / inverted if statements. // (I know a few people who would kill me for using this.) List <AvailabilityApiGetDto> apiGetDtoList = await _availabilities .GetAvailabilities(@event.TypeId, @event.Date, @event.Date.Add(@event.Duration.Value)); List <Availability> availabilities = apiGetDtoList .Select(x => new Availability { CostPerHour = x.CostPerHour, Date = x.Date, VenueCode = x.Code, Venue = new Venue { Code = x.Code, Description = x.Description, Name = x.Name, Capacity = x.Capacity } }).ToList(); // This block may be redundant depending on the availability behaviour and if a reserved availability is not included in the list. // (I assume its redundant so its commented out.) //List<Availability> nonReserved = new List<Availability>(); //foreach (Availability availability in availabilities) //{ // ReservationGetDto reservations = await _reservations.GetReservation(availability.VenueCode, @event.Date); // if (reservations.Reference == null) // nonReserved.Add(availability); // If the availability in the list has a reservation assigne then it is reserved. //} //availabilities.Clear(); SelectList list = new SelectList( availabilities.Select(x => new { x.Venue.Name, x.VenueCode, x.Date, x.CostPerHour } ), "VenueCode", "Name"); EventVenueViewModel novenue = new EventVenueViewModel() { Title = @event.Title, Date = @event.Date, Duration = @event.Duration, Id = @event.Id, TypeId = @event.TypeId, TypeTitle = (await _eventTypes.GetEventType(@event.TypeId)).Title, Availabilities = availabilities, AvailabilitiesSelectList = list }; return(View(novenue)); }
/// <summary> /// HTTP GET endpoint for "/Events/Details/<paramref name="id"/>" <para/> /// </summary> /// <param name="id">The <see cref="Event"/>'s Id</param> /// <returns>A <see cref="EventDetailsViewModel"/> the specified event.</returns> public async Task <IActionResult> Details(int?id) { if (!id.HasValue) { return(NotFound()); } Event @event = await _context.Events.Where(x => !x.Cancelled) .FirstOrDefaultAsync(m => m.Id == id); if (@event == null) { return(NotFound()); } Dictionary <string, float> pricing = new Dictionary <string, float>(); if (@event.AssignedMenu != 0) { MenuGetDto menu = await _menuManagement.GetMenu(@event.AssignedMenu); if (menu != null) { float foodPrice = (float)menu.Food.Sum(x => x.Cost); pricing.Add("Menu (sum of food)", foodPrice); } } if (@event.VenueReservation != null) { ReservationGetDto res = await _reservations.GetReservation(@event.VenueReservation); List <AvailabilityApiGetDto> avail = await _availabilities .GetAvailabilities(@event.TypeId, new DateTime(2018, 07, 10), new DateTime(2019, 2, 10)); AvailabilityApiGetDto availability = avail.FirstOrDefault(x => x.Code == res.VenueCode); pricing.Add("Venue (per hour)", (float)availability.CostPerHour); } EventDetailsViewModel viewModel = new EventDetailsViewModel() { Id = @event.Id, Title = @event.Title, Date = @event.Date, Duration = @event.Duration, TypeId = (await _eventTypes.GetEventType(@event.TypeId)).Title, Bookings = await _context.Guests .Include(e => e.Customer) .Include(e => e.Event) .Where(e => e.EventId == id) .Select(x => new GuestBookingDetailsViewModel() // Convert to nested view models { Attended = x.Attended, Customer = new CustomerDetailsViewModel() { Email = x.Customer.Email, FirstName = x.Customer.FirstName, Id = x.Customer.Id, Surname = x.Customer.Surname }, Event = new EventDetailsViewModel() { Id = x.Event.Id, Date = x.Event.Date, Duration = x.Event.Duration, Title = x.Event.Title, TypeId = x.Event.TypeId }, CustomerId = x.CustomerId, EventId = x.EventId }).ToListAsync(), Pricings = pricing }; return(View(viewModel)); }