public async Task <IActionResult> Create([Bind("Id,Title,Date,Duration,Type,Venue")] EventViewModel @event) { if (ModelState.IsValid) { if (@event.Venue != null) { //Make sure venue is fine for this event and make reservation var reservation = new ReservationPostDTO() { EventDate = @event.Date, VenueCode = @event.Venue, StaffId = "1" }; try { HttpResponseMessage response = await getConnection().PostAsJsonAsync("/api/reservations", reservation); if (response.IsSuccessStatusCode) { var reservationResponse = await response.Content.ReadAsAsync <ReservationGetDTO>(); //Save reservation info to event Event EventToAdd = new Event() { Id = @event.Id, Title = @event.Title, Date = @event.Date, Duration = @event.Duration, Type = @event.Type, Venue = @event.Venue, VenueReference = reservationResponse.Reference }; try { _context.Add(EventToAdd); await _context.SaveChangesAsync(); } catch (Exception) { ModelState.AddModelError("Duration", "Please use a valid duration in the format: 00:00:00"); return(View(@event)); } return(RedirectToAction(nameof(Index))); } else { return(BadRequest()); } } catch (Exception) { return(BadRequest("Unable to connect to Venue API")); } } else { Event EventToAdd = new Event() { Id = @event.Id, Title = @event.Title, Date = @event.Date, Duration = @event.Duration, Type = @event.Type }; try { _context.Add(EventToAdd); await _context.SaveChangesAsync(); } catch (Exception) { ModelState.AddModelError("Duration", "Please use a valid duration in the format: 00:00:00"); return(View(@event)); } return(RedirectToAction(nameof(Index))); } } ViewData["Type"] = new SelectList(await GetEventTypes(), "Id", "Title"); return(View(@event)); }
public async Task <IActionResult> SetVenue(int id, [Bind("Id,Date,Venue")] EventViewModel @event) { if (id != @event.Id) { return(NotFound()); } if (@event.Venue != null) { try { var current = _context.Events .Where(o => o.Disabled == null) .FirstOrDefault(e => e.Id == id); if (current == null) { return(NotFound()); } //Remove old reservation if (current.Venue != null && current.VenueReference != null) { try { HttpResponseMessage res = await getConnection().DeleteAsync("/api/reservations/" + current.VenueReference); if (!res.IsSuccessStatusCode) { return(BadRequest("Couldn't remove reservation for old venue")); } } catch (Exception) { return(BadRequest("Unable to connect to Venue API")); } } //Add new reservation var reservation = new ReservationPostDTO() { EventDate = @event.Date, VenueCode = @event.Venue, StaffId = "1" }; try { HttpResponseMessage response = await getConnection().PostAsJsonAsync("/api/reservations", reservation); if (response.IsSuccessStatusCode) { var reservationResponse = await response.Content.ReadAsAsync <ReservationGetDTO>(); //Save reservation info to event in db current.Venue = @event.Venue; current.VenueReference = reservationResponse.Reference; await _context.SaveChangesAsync(); } } catch (Exception) { return(BadRequest("Unable to connect to Venue API")); } } catch (DbUpdateConcurrencyException) { if (!EventExists(@event.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } var venues = await GetVenueData(@event); ViewData["Venue"] = new SelectList(venues, "Code", "Name"); ViewData["NumVenues"] = venues.Count(); return(View(@event)); }