59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
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<List<Restaurant>> GetAll() {
|
|
return _context.Restaurants.ToListAsync();
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Restaurant?>> GetOne(Guid id) {
|
|
var restaurant = await _context.Restaurants.FindAsync(id);
|
|
if (restaurant == null) return NotFound();
|
|
var waiters = await WaiterApiClient.GetAllForRestaurant(id);
|
|
restaurant.Waiters = waiters;
|
|
return restaurant;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult<Restaurant> 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<Restaurant> 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); |