Y2S1-Web_Apps_and_Services/ThAmCo.Events/Controllers/FoodController.cs

161 lines
4.7 KiB
C#
Raw Normal View History

2020-06-07 21:36:12 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ThAmCo.Events.Data;
using ThAmCo.Events.Models;
using System.Net.Http;
using System.Diagnostics;
using ThAmCo.Events.Models.ViewModels.Events;
using ThAmCo.Events.Models.Dto;
namespace ThAmCo.Events.Controllers
{
public class FoodController : Controller
{
EventsDbContext _context;
public FoodController()
{
}
// GET: Food
public async Task<IActionResult> Index()
{
//Get full list of menus and their data from the web service.
HttpClient client = new HttpClient
{
BaseAddress = new Uri("http://localhost:32824")
};
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
HttpResponseMessage response = await client.GetAsync("api/values");
if (response.IsSuccessStatusCode)
{
var menus = response.Content.ReadAsAsync<IEnumerable<Food>>().Result.ToList();
return View(menus);
}
else
{
Debug.WriteLine("Index received a bad response from the web service.");
}
return NotFound();
}
// GET: Food/Details/5
public async Task<IActionResult> Details(int? id)
{
//Get details of a specific menu from the web service.
if (id == null)
{
return NotFound();
}
HttpClient client = new HttpClient
{
BaseAddress = new Uri("http://localhost:32824")
};
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
HttpResponseMessage response = await client.GetAsync("api/values/" + id);
if (response.IsSuccessStatusCode)
{
var menu = response.Content.ReadAsAsync<Food>().Result;
return View(menu);
}
else
{
Debug.WriteLine("Index received a bad response from the web service.");
}
return NotFound();
}
// GET: Food/Create
public IActionResult Create()
{
return View();
}
// POST: Food/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,Items,Price")] Food food)
{
if (ModelState.IsValid)
{
HttpClient client = new HttpClient()
{
BaseAddress = new Uri("http://localhost:32824/")
};
client.DefaultRequestHeaders.Accept.ParseAdd("application/json");
HttpResponseMessage response = await client.PostAsJsonAsync("api/values", food);
return RedirectToAction(nameof(Details), new { id = food.Id });
}
return View(food);
}
// GET: Food/Edit/5
public async Task<IActionResult> Edit(int? id)
{
//TODO: Fix
/*if (id == null)
{
return NotFound();
}
var food = await _context.Food.FindAsync(id);
if (food == null)
{
return NotFound();
}
return View(food);*/
return null;
}
// POST: Food/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Items,Price")] Food food)
{
if (id != food.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(food);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (false)
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(food);
}
}
}