-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBeerController.cs
More file actions
162 lines (135 loc) · 3.7 KB
/
BeerController.cs
File metadata and controls
162 lines (135 loc) · 3.7 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using BreweryAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace BreweryAPI.Controllers
{
[Route("Beer")]
[ApiController]
public class BeerController : ControllerBase
{
private readonly BreweryContext _context;
public BeerController(BreweryContext context)
{
_context = context;
}
// GET: api/BeersByBrewer
[HttpGet("BeersByBrewer/{brewerId}")]
public async Task<ActionResult<IEnumerable<BeerModel>>> GetBeersByBrewer(int brewerId)
{
if (_context.Beers == null)
{
return NotFound();
}
var beersByBrewer = await _context.Beers
.Where(beer => beer.BrewerId == brewerId)
.Include(b => b.Brewer)
.ToListAsync();
if (beersByBrewer == null || beersByBrewer.Count == 0)
{
return NotFound();
}
return beersByBrewer;
}
// GET: api/Beer
[HttpGet]
public async Task<ActionResult<IEnumerable<BeerModel>>> GetBeers()
{
if (_context.Beers == null)
{
return NotFound();
}
return await _context.Beers.Include(b => b.Brewer).ToListAsync();
}
// GET: api/Beer/5
[HttpGet("{id}")]
public async Task<ActionResult<BeerModel>> GetBeerModel(int id)
{
if (_context.Beers == null)
{
return NotFound();
}
var beerModel = await _context.Beers.Include(b => b.Brewer).FirstOrDefaultAsync(b => b.BeerId == id);
if (beerModel == null)
{
return NotFound();
}
return beerModel;
}
// PUT: api/Beer/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutBeerModel(int id, BeerModel updatedBeerModel)
{
if (id != updatedBeerModel.BeerId)
{
return BadRequest();
}
var existingBrewery = await _context.Breweries.FindAsync(updatedBeerModel.BrewerId);
if (existingBrewery == null)
{
return NotFound("That brewery doesn't exist");
}
updatedBeerModel.Brewer = existingBrewery;
_context.Entry(updatedBeerModel).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!BeerModelExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Beer
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<BeerModel>> PostBeerModel(BeerModel beerModel)
{
if (_context.Beers == null || _context.Breweries == null)
{
return Problem("Entity set 'BreweryContext.Beers' or 'BreweryContext.Breweries' is null.");
}
var existingBrewery = await _context.Breweries.FindAsync(beerModel.BrewerId);
if (existingBrewery == null)
{
return NotFound("Brewery not found.");
}
var existingBeer = await _context.Beers.FirstOrDefaultAsync(b => b.Name == beerModel.Name && b.BrewerId == beerModel.BrewerId);
if (existingBeer != null)
return Conflict("A beer with this name already exists for this brewer");
beerModel.Brewer = existingBrewery;
_context.Beers.Add(beerModel);
await _context.SaveChangesAsync();
return CreatedAtAction("GetBeerModel", new { id = beerModel.BeerId }, beerModel);
}
// DELETE: api/Beer/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteBeerModel(int id)
{
if (_context.Beers == null)
{
return NotFound();
}
var beerModel = await _context.Beers.FindAsync(id);
if (beerModel == null)
{
return NotFound();
}
_context.Beers.Remove(beerModel);
await _context.SaveChangesAsync();
return NoContent();
}
private bool BeerModelExists(int id)
{
return (_context.Beers?.Any(b => b.BeerId == id)).GetValueOrDefault();
}
}
}