-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBreweriesController.cs
More file actions
97 lines (81 loc) · 2.52 KB
/
BreweriesController.cs
File metadata and controls
97 lines (81 loc) · 2.52 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
97
using BreweryApi.Models;
using BreweryApi.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace BreweryApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BreweriesController : ControllerBase
{
private BreweryService _breweryService;
public BreweriesController( BreweryService breweryService )
{
_breweryService = breweryService;
}
// GET: api/Breweries
[HttpGet]
public async Task<ActionResult<IEnumerable<Brewery>>> GetBrewery()
{
return _breweryService.GetBreweries();
}
// GET: api/Breweries/5
[HttpGet("{id}")]
public async Task<ActionResult<Brewery>> GetBrewery(int id)
{
var brewery = _breweryService.GetBrewery(id);
if (brewery == null)
{
return NotFound();
}
return brewery;
}
// PUT: api/Breweries/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutBrewery(int id, Brewery brewery)
{
if (id != brewery.Id)
{
return BadRequest();
}
_breweryService.UpdateBrewery(brewery);
try
{
_breweryService.SaveDb();
}
catch (DbUpdateConcurrencyException)
{
if (!_breweryService.BreweryExsists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Breweries
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Brewery>> PostBrewery(Brewery brewery)
{
_breweryService.InsertBrewery(brewery);
return CreatedAtAction("GetBrewery", new { id = brewery.Id }, brewery);
}
// DELETE: api/Breweries/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteBrewery(int id)
{
var brewery = _breweryService.GetBrewery(id);
if (brewery == null)
{
return NotFound();
}
_breweryService.DeleteBrewery(brewery);
return NoContent();
}
}
}