using ApiRestaurant.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; namespace ApiRestaurant.Controllers; [Route("api/[controller]")] [ApiController] public class RestaurantController : ControllerBase { private RestaurantContext _context; public RestaurantController(RestaurantContext context) { _context = context; } [HttpGet] public Task> GetAll() { return _context.Restaurants.ToListAsync(); } [HttpGet("{id}")] public async Task> GetOne(Guid id) { var restaurant = await _context.Restaurants.FindAsync(id); if (restaurant == null) return NotFound($"Restourant with [id: '{id}'] not found"); var waiters = await WaiterApiClient.GetAllForRestaurant(id); restaurant.Waiters = waiters; return restaurant; } [HttpPost] public ActionResult Create(RestaurantDTO restaurant) { var newRestaurant = new Restaurant { Name = restaurant.Name }; var res = _context.Restaurants.Add(newRestaurant); _context.SaveChanges(); return res.Entity; } [HttpPut("{id}")] public ActionResult Update(Guid id, RestaurantDTO restaurant) { var oldRestaurant = _context.Restaurants.FirstOrDefault(r => r.Id == id); if (oldRestaurant == null) return NotFound(); oldRestaurant.Name = restaurant.Name; var res = _context.Restaurants.Update(oldRestaurant); _context.SaveChangesAsync(); return res.Entity; } [HttpDelete] public ActionResult Delete(Guid id) { var restaurant = _context.Restaurants.FirstOrDefault(r => r.Id == id); if (restaurant is null) return NotFound(); _context.Restaurants.Remove(restaurant); _context.SaveChangesAsync(); return Ok(); } } public record RestaurantDTO(string Name);