summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-11-24 18:15:51 -0600
committerHombreLaser <sebastian-440@live.com>2022-11-24 18:15:51 -0600
commite90d61eca62e1a9cb194d05407969831576c8586 (patch)
tree1f9223bfef475240c264f00090e8aae9113eb369
parent1a504435120bf50f7a51ff1351abe8b7e398d6a2 (diff)
Añadido controlador de premios
-rw-r--r--Controllers/PrizesController.cs85
-rw-r--r--Forms/PrizeForm.cs1
-rw-r--r--Profiles/PrizeProfile.cs7
-rw-r--r--Program.cs1
-rw-r--r--Services/IPrizeService.cs12
-rw-r--r--Services/PrizeService.cs42
6 files changed, 146 insertions, 2 deletions
diff --git a/Controllers/PrizesController.cs b/Controllers/PrizesController.cs
new file mode 100644
index 0000000..0707697
--- /dev/null
+++ b/Controllers/PrizesController.cs
@@ -0,0 +1,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."});
+ }
+ }
+} \ No newline at end of file
diff --git a/Forms/PrizeForm.cs b/Forms/PrizeForm.cs
index 04f33a0..00666f5 100644
--- a/Forms/PrizeForm.cs
+++ b/Forms/PrizeForm.cs
@@ -9,6 +9,7 @@ namespace BackendPIA.Forms {
public string? Name { get; set; }
[Required]
[UniqueTier]
+ [Range(0, 53)]
public int Tier { get; set; }
[Required]
public string? Category { get; set; }
diff --git a/Profiles/PrizeProfile.cs b/Profiles/PrizeProfile.cs
index 46545ef..1a40062 100644
--- a/Profiles/PrizeProfile.cs
+++ b/Profiles/PrizeProfile.cs
@@ -1,5 +1,6 @@
-using AutoMapper;
-
+using AutoMapper;
+using Microsoft.AspNetCore.JsonPatch;
+using Microsoft.AspNetCore.JsonPatch.Operations;
using BackendPIA.Models;
using BackendPIA.Forms;
@@ -7,6 +8,8 @@ namespace BackendPIA.Profiles {
public class PrizeProfile : Profile {
public PrizeProfile() {
CreateMap<PrizeForm, Prize>();
+ CreateMap<JsonPatchDocument<PrizeForm>, JsonPatchDocument<Prize>>();
+ CreateMap<Operation<PrizeForm>, Operation<Prize>>(); 
}
}
} \ No newline at end of file
diff --git a/Program.cs b/Program.cs
index 008bfc7..ec416e6 100644
--- a/Program.cs
+++ b/Program.cs
@@ -29,6 +29,7 @@ builder.Services.AddSingleton<ITokenGenerator>(s => new TokenGenerator(builder.C
builder.Services.AddScoped<IUserAccountService, UserAccountService>();
builder.Services.AddScoped<IRaffleService, RaffleService>();
builder.Services.AddScoped<ITicketService, TicketService>();
+builder.Services.AddScoped<IPrizeService, PrizeService>();
// End of custom services configuration.
// Swagger configuration.
diff --git a/Services/IPrizeService.cs b/Services/IPrizeService.cs
new file mode 100644
index 0000000..c7261ab
--- /dev/null
+++ b/Services/IPrizeService.cs
@@ -0,0 +1,12 @@
+using Microsoft.AspNetCore.Mvc.ModelBinding;
+using Microsoft.AspNetCore.JsonPatch;
+using BackendPIA.Models;
+
+namespace BackendPIA.Services {
+ public interface IPrizeService {
+ public Task<Prize> CreatePrize(Prize to_create);
+ public Task<IEnumerable<Prize>> GetPrizes();
+ public Task<Prize> GetPrize(long id);
+ public Task<bool> DeletePrize(long id);
+ }
+} \ No newline at end of file
diff --git a/Services/PrizeService.cs b/Services/PrizeService.cs
new file mode 100644
index 0000000..aa07c65
--- /dev/null
+++ b/Services/PrizeService.cs
@@ -0,0 +1,42 @@
+using Microsoft.EntityFrameworkCore;
+using Microsoft.AspNetCore.Mvc.ModelBinding;
+using Microsoft.AspNetCore.JsonPatch;
+using BackendPIA.Models;
+
+namespace BackendPIA.Services {
+ public class PrizeService : IPrizeService {
+ private readonly ApplicationDbContext _context;
+
+ public PrizeService(ApplicationDbContext context) {
+ _context = context;
+ }
+
+ public async Task<Prize> CreatePrize(Prize to_create) {
+ await _context.AddAsync(to_create);
+ await _context.SaveChangesAsync();
+ to_create.Raffle = _context.Raffles.Find(to_create.RaffleId);
+
+ return to_create;
+ }
+
+ public async Task<IEnumerable<Prize>> GetPrizes() {
+ return await _context.Prizes.Include(p => p.Raffle).ToListAsync();
+ }
+
+ public async Task<Prize> GetPrize(long id) {
+ return await _context.Prizes.Include(p => p.Raffle).FirstOrDefaultAsync(p => p.Id == id);
+ }
+
+ public async Task<bool> DeletePrize(long id) {
+ var prize = _context.Prizes.Find(id);
+
+ if(prize == null)
+ return false;
+
+ _context.Remove(prize);
+ _context.SaveChangesAsync();
+
+ return true;
+ }
+ }
+} \ No newline at end of file