public async Task <IActionResult> Put(int id, [FromBody] MenuPutDto menu) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (menu.Id != id) { return(NotFound()); } var existingMenu = await _context.Menus.FindAsync(menu.Id); if (existingMenu == null || existingMenu.Id != menu.Id) { return(NotFound()); } existingMenu.Name = menu.Name; _context.Menus.Update(existingMenu); await _context.SaveChangesAsync(); MenuGetDto newMenu = GetMenus().FirstOrDefault(x => x.Id == existingMenu.Id); if (newMenu == null) { return(NotFound()); } return(Ok(newMenu)); }
public IActionResult Get(int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } MenuGetDto menu = GetMenus().FirstOrDefault(x => x.Id == id); if (menu == null) { return(NotFound()); } return(Ok(menu)); }
public async Task <MenuGetDto> UpdateMenu(int id, MenuPostDto menu) { EnsureClient(); MenuGetDto newMenu = null; try { var response = await _client.PutAsJsonAsync("api/menu/" + id, menu); response.EnsureSuccessStatusCode(); newMenu = await response.Content.ReadAsAsync <MenuGetDto>(); } catch (HttpRequestException ex) { _logger.LogError("Caught an error when deleting a menu at id " + id + ". Exception: " + ex); } return(newMenu); }
/// <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)); }