/
BeerController.cs
53 lines (47 loc) · 1.65 KB
/
BeerController.cs
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
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApi.Hal.Web.Api.Resources;
using WebApi.Hal.Web.Data;
namespace WebApi.Hal.Web.Api
{
[Route("[controller]")]
public class BeerController : Controller
{
readonly IBeerDbContext beerDbContext;
public BeerController(IBeerDbContext beerDbContext)
{
this.beerDbContext = beerDbContext;
}
[HttpGet("{id}")]
// GET beer/5
public BeerRepresentation Get(int id)
{
var beer = beerDbContext.Beers
.Include("Brewery") // lazy loading isn't on for this query; force loading
.Include("BeerStyle")
.Single(br => br.Id == id);
return new BeerRepresentation
{
Id = beer.Id,
Name = beer.Name,
BreweryId = beer.Brewery == null ? (int?) null : beer.Brewery.Id,
BreweryName = beer.Brewery == null ? null : beer.Brewery.Name,
StyleId = beer.Style == null ? (int?) null : beer.Style.Id,
StyleName = beer.Style == null ? null : beer.Style.Name,
ReviewIds = beerDbContext.Reviews.Where(r => r.Beer_Id == id).Select(r => r.Id).ToList()
};
}
[HttpPut]
// PUT beer?id=1&value=foo
public void Put(int id, string value)
{
}
[HttpDelete]
// DELETE beer?id=1
public void Delete(int id)
{
}
}
}