94 lines
2.4 KiB
C#
94 lines
2.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using ThAmCo.Catering.Data;
|
|||
|
using ThAmCo.Venues.Data;
|
|||
|
|
|||
|
namespace ThAmCo.Catering.Controllers
|
|||
|
{
|
|||
|
[Route("api/[controller]")]
|
|||
|
[ApiController]
|
|||
|
public class ValuesController : ControllerBase
|
|||
|
{
|
|||
|
private readonly CateringDbContext _context;
|
|||
|
|
|||
|
public ValuesController(CateringDbContext context)
|
|||
|
{
|
|||
|
_context = context;
|
|||
|
}
|
|||
|
|
|||
|
// GET api/values
|
|||
|
[HttpGet]
|
|||
|
public ActionResult<IEnumerable<Menu>> Get()
|
|||
|
{
|
|||
|
return _context.Menus.ToArray();
|
|||
|
}
|
|||
|
|
|||
|
// GET api/values/5
|
|||
|
[HttpGet("{id}")]
|
|||
|
public ActionResult<string> Get(int id)
|
|||
|
{
|
|||
|
var menu = _context.Menus.FirstOrDefault(f => f.Id == id);
|
|||
|
|
|||
|
if (menu != null)
|
|||
|
{
|
|||
|
return Ok(menu);
|
|||
|
}
|
|||
|
return NotFound();
|
|||
|
}
|
|||
|
|
|||
|
// POST api/values/
|
|||
|
//Gives the function the menuid it wants to book.
|
|||
|
[HttpPost]
|
|||
|
public IActionResult Post([FromBody] int id)
|
|||
|
{
|
|||
|
if (_context.Menus.Any(m => m.Id == id))
|
|||
|
{
|
|||
|
FoodBooking fb = new FoodBooking
|
|||
|
{
|
|||
|
MenuId = id,
|
|||
|
//Notes = notes,
|
|||
|
};
|
|||
|
|
|||
|
var updatedFB = _context.Add(fb);
|
|||
|
_context.SaveChangesAsync();
|
|||
|
|
|||
|
return Ok(updatedFB);
|
|||
|
}
|
|||
|
return BadRequest("MenuID was not found.");
|
|||
|
}
|
|||
|
|
|||
|
// PUT api/values/5
|
|||
|
[HttpPut("{id}")]
|
|||
|
public IActionResult Put([Bind("Items,Price")] Menu food)
|
|||
|
{
|
|||
|
//If menu exists, edit the current entry.
|
|||
|
if (_context.Menus.Any(m => m.Id == food.Id) && ModelState.IsValid)
|
|||
|
{
|
|||
|
var menu = _context.Menus.First(m => m.Id == food.Id);
|
|||
|
|
|||
|
_context.Update(menu);
|
|||
|
_context.SaveChangesAsync();
|
|||
|
return Ok(menu);
|
|||
|
}
|
|||
|
//If menu doesn't exist, make a current entry.
|
|||
|
else if (ModelState.IsValid)
|
|||
|
{
|
|||
|
_context.Add(food);
|
|||
|
_context.SaveChangesAsync();
|
|||
|
return Ok();
|
|||
|
}
|
|||
|
|
|||
|
return NotFound();
|
|||
|
}
|
|||
|
|
|||
|
// DELETE api/values/5
|
|||
|
[HttpDelete("{id}")]
|
|||
|
public void Delete(int id)
|
|||
|
{
|
|||
|
}
|
|||
|
}
|
|||
|
}
|