-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathPizzaVotesController.cs
More file actions
62 lines (53 loc) · 1.61 KB
/
PizzaVotesController.cs
File metadata and controls
62 lines (53 loc) · 1.61 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
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace backend.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PizzaVotesController : ControllerBase
{
private readonly ApplicationDbContext _dbContext;
public PizzaVotesController(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
// GET api/pizzavotes
[HttpGet]
public async Task<ActionResult<List<PizzaVotes>>> Get()
{
return await _dbContext.PizzaVotes.ToListAsync();
}
// GET api/pizzavotes/{email}
[Authorize]
[HttpGet("{id}")]
public async Task<ActionResult<PizzaVotes>> Get(string id)
{
return await _dbContext.PizzaVotes.FindAsync(id);
}
// POST api/pizzavotes
[Authorize]
[HttpPost]
public async Task Post(PizzaVotes model)
{
await _dbContext.AddAsync(model);
await _dbContext.SaveChangesAsync();
}
// PUT api/pizzavotes/{email}
[Authorize]
[HttpPut("{id}")]
public async Task<ActionResult> Put(string id, PizzaVotes model)
{
var exists = await _dbContext.PizzaVotes.AnyAsync(f => f.Id == id);
if (!exists)
{
return NotFound();
}
_dbContext.PizzaVotes.Update(model);
await _dbContext.SaveChangesAsync();
return Ok();
}
}
}