blob: 0707697e5401c1b90a25ac6919c8567690be7638 (
plain)
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
|
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
using BackendPIA.Errors;
using BackendPIA.Forms;
using BackendPIA.Models;
using BackendPIA.Services;
namespace BackendPIA.Controllers {
[Route("api/prizes")]
[ApiController]
public class PrizesController : ControllerBase {
private readonly IMapper _mapper;
private readonly IPrizeService _prize_service;
private readonly ApplicationDbContext _context;
public PrizesController(IMapper mapper, IPrizeService service, ApplicationDbContext context) {
_mapper = mapper;
_prize_service = service;
_context = context;
}
[HttpGet]
[Authorize]
public async Task<ActionResult<IEnumerable<Prize>>> Index() {
var result = await _prize_service.GetPrizes();
return Ok(result);
}
[HttpGet("{id:int}")]
public async Task<ActionResult<Prize>> Show(long id) {
var result = await _prize_service.GetPrize(id);
if(result == null)
return NotFound(new NotFoundError(404, $"The resource with id {id} couldn't be found"));
return result;
}
[HttpPost]
[Authorize(Roles = "Administrator")]
public async Task<ActionResult<Prize>> Create(PrizeForm form) {
if(!_context.Raffles.Any(r => r.Id == form.RaffleId))
return BadRequest(new NotFoundError(404, $"The raffle with id {form.RaffleId} couldn't be found"));
var to_create = _mapper.Map<Prize>(form);
var result = await _prize_service.CreatePrize(to_create);
return Ok(result);
}
[HttpPatch("{id:int}")]
[Authorize(Roles = "Administrator")]
public async Task<ActionResult<Prize>> Update([FromBody] JsonPatchDocument<PrizeForm> patch_doc_form, long id) {
if(patch_doc_form == null)
return BadRequest(ModelState);
var patch_doc = _mapper.Map<JsonPatchDocument<Prize>>(patch_doc_form);
var prize = _context.Prizes.Find(id);
if(prize == null)
return NotFound(new NotFoundError(404, $"The resource with id {id} couldn't be found"));
patch_doc.ApplyTo(prize, ModelState);
if(!ModelState.IsValid)
return BadRequest(ModelState);
prize.Raffle = _context.Raffles.Find(prize.RaffleId);
return Ok(prize);
}
[HttpDelete("{id:int}")]
[Authorize(Roles = "Administrator")]
public async Task<ActionResult> Delete(long id) {
var result = await _prize_service.DeletePrize(id);
if(!result)
return NotFound($"The prize with id {id} couldnt be found.");
return StatusCode(303, new { Message = $"The prize with id {id} has been deleted."});
}
}
}
|