2024-11-03 19:18:40 +04:00
|
|
|
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);
|
2024-11-04 00:42:54 +04:00
|
|
|
if (restaurant == null) return NotFound($"Restourant with [id: '{id}'] not found");
|
2024-11-03 19:18:40 +04:00
|
|
|
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);
|