You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cat_cafe/cat_cafe/Controllers/BarsController.cs

109 lines
2.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using cat_cafe.Entities;
using cat_cafe.Repositories;
namespace cat_cafe.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BarsController : ControllerBase
{
private readonly BarContext _context;
public BarsController(BarContext context)
{
_context = context;
}
// GET: api/Bars
[HttpGet]
public async Task<ActionResult<IEnumerable<Bar>>> GetBars()
{
return await _context.Bars.ToListAsync();
}
// GET: api/Bars/5
[HttpGet("{id}")]
public async Task<ActionResult<Bar>> GetBar(long id)
{
var bar = await _context.Bars.FindAsync(id);
if (bar == null)
{
return NotFound();
}
return bar;
}
// PUT: api/Bars/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutBar(long id, Bar bar)
{
if (id != bar.Id)
{
return BadRequest();
}
_context.Entry(bar).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!BarExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Bars
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Bar>> PostBar(Bar bar)
{
_context.Bars.Add(bar);
await _context.SaveChangesAsync();
return CreatedAtAction("GetBar", new { id = bar.Id }, bar);
}
// DELETE: api/Bars/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteBar(long id)
{
var bar = await _context.Bars.FindAsync(id);
if (bar == null)
{
return NotFound();
}
_context.Bars.Remove(bar);
await _context.SaveChangesAsync();
return NoContent();
}
private bool BarExists(long id)
{
return _context.Bars.Any(e => e.Id == id);
}
}
}