-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBeersController.cs
More file actions
96 lines (79 loc) · 2.38 KB
/
BeersController.cs
File metadata and controls
96 lines (79 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using BreweryApi.Models;
using BreweryApi.Models.DTOs;
using BreweryApi.Services;
namespace BreweryApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BeersController : ControllerBase
{
private readonly BeerService _beerService;
public BeersController(BeerService beerService)
{
_beerService = beerService;
}
// GET: api/Beers
[HttpGet]
public async Task<ActionResult<IEnumerable<BeerDTO>>> GetBeer()
{
return _beerService.GetBeers();
}
// GET: api/Beers/5
[HttpGet("{id}")]
public async Task<ActionResult<BeerDTO>> GetBeer(int id)
{
var result = _beerService.GetBeerDTO(id);
if ( result == null) { return NotFound(); }
return result;
}
// PUT: api/Beers/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutBeer(int id, Beer beer)
{
if (id != beer.Id)
{
return BadRequest();
}
_beerService.UpdateBeer(beer);
try
{
_beerService.SaveDb();
}
catch (DbUpdateConcurrencyException)
{
if (!_beerService.BeerExsists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Beers
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Beer>> PostBeer(Beer beer)
{
_beerService.InsertBeer(beer);
return CreatedAtAction(nameof(GetBeer), new { id = beer.Id }, beer);
}
// DELETE: api/Beers/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteBeer(int id)
{
var beer = _beerService.GetBeer(id);
if (beer == null)
{
return NotFound();
}
_beerService.DeleteBeer(beer);
return NoContent();
}
}
}