From b3deb8c5d92dd616042ea9e265c197abe02f995c Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Mon, 21 Nov 2022 16:34:39 -0600 Subject: AƱadido controlador de rifas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/RafflesController.cs | 70 +++++++++++++++++++++++++ Forms/RaffleForm.cs | 13 +++++ Migrations/ApplicationDbContextModelSnapshot.cs | 2 +- Models/Raffle.cs | 2 +- Profiles/RaffleProfile.cs | 11 ++++ Program.cs | 1 + Services/IRaffleService.cs | 11 ++++ Services/ITokenGenerator.cs | 1 - Services/RaffleService.cs | 61 +++++++++++++++++++++ 9 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 Controllers/RafflesController.cs create mode 100644 Forms/RaffleForm.cs create mode 100644 Profiles/RaffleProfile.cs create mode 100644 Services/IRaffleService.cs create mode 100644 Services/RaffleService.cs diff --git a/Controllers/RafflesController.cs b/Controllers/RafflesController.cs new file mode 100644 index 0000000..775ef3b --- /dev/null +++ b/Controllers/RafflesController.cs @@ -0,0 +1,70 @@ +using AutoMapper; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using BackendPIA.Forms; +using BackendPIA.Models; +using BackendPIA.Services; +using BackendPIA.Logics; + +namespace BackendPIA.Controllers { + [Route("api/raffles")] + [ApiController] + public class RafflesController : ControllerBase { + private readonly IRaffleService _service; + private readonly IMapper _mapper; + + public RafflesController(IMapper mapper, IRaffleService service) { + _service = service; + _mapper = mapper; + } + + [HttpGet] + public async Task> Get([FromQuery] string name = "") { + return await _service.GetRaffles(name); + } + + [HttpGet("{id:int}")] + public async Task> Show(long id) { + var raffle = await _service.GetRaffle(id); + + if(raffle == null) + return NotFound("The resource couldn't be found"); + + return raffle; + } + + [Authorize(Roles = "Administrator")] + [HttpPost] + public async Task> Create(RaffleForm form) { + var raffle = await _service.CreateRaffle(_mapper.Map(form)); + + return raffle; + } + + [Authorize(Roles = "Administrator")] + [HttpPut("{id:int}")] + public async Task> Update(long id, RaffleForm form) { + var raffle = _mapper.Map(form); + raffle.Id = id; + var result = await _service.UpdateRaffle(raffle); + + if(result == null) + return NotFound("The resource couldn't be found."); + + return raffle; + } + + [Authorize(Roles = "Administrator")] + [HttpDelete("{id:int}")] + public async Task Update(long id) { + var result = await _service.DeleteRaffle(id); + + if(!result) + return NotFound("The resource couldn't be found."); + + return StatusCode(303, new { Message = "The resource has been deleted"} ); + } + } +} \ No newline at end of file diff --git a/Forms/RaffleForm.cs b/Forms/RaffleForm.cs new file mode 100644 index 0000000..3046aed --- /dev/null +++ b/Forms/RaffleForm.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.AspNetCore.Identity; + +namespace BackendPIA.Forms { + public class RaffleForm { + [Required] + [StringLength(128)] + public string? Name { get; set; } + [Required] + [Range(1, 54, ErrorMessage = "Value for {0} must be between {1} and {2}.")] + public int Winners { get; set; } + } +} \ No newline at end of file diff --git a/Migrations/ApplicationDbContextModelSnapshot.cs b/Migrations/ApplicationDbContextModelSnapshot.cs index 3b31010..9fceb51 100644 --- a/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Migrations/ApplicationDbContextModelSnapshot.cs @@ -43,7 +43,7 @@ namespace BackendPIA.Migrations b.HasKey("Id"); - b.ToTable("Raffles"); + b.ToTable("Raffles", (string)null); }); modelBuilder.Entity("BackendPIA.Models.UserAccount", b => diff --git a/Models/Raffle.cs b/Models/Raffle.cs index c2dcc58..6e2640d 100644 --- a/Models/Raffle.cs +++ b/Models/Raffle.cs @@ -1,5 +1,4 @@ using System.ComponentModel.DataAnnotations; -using Microsoft.AspNetCore.Identity; namespace BackendPIA.Models { public class Raffle { @@ -8,6 +7,7 @@ namespace BackendPIA.Models { [StringLength(128)] public string? Name { get; set; } [Required] + [Range(1, 54, ErrorMessage = "Value for {0} must be between {1} and {2}.")] public int Winners { get; set; } public bool IsClosed { get; set; } } diff --git a/Profiles/RaffleProfile.cs b/Profiles/RaffleProfile.cs new file mode 100644 index 0000000..4b872b3 --- /dev/null +++ b/Profiles/RaffleProfile.cs @@ -0,0 +1,11 @@ +using AutoMapper; +using BackendPIA.Models; +using BackendPIA.Forms; + +namespace BackendPIA.Profiles { + public class RaffleProfile : Profile { + public RaffleProfile() { + CreateMap().ReverseMap(); + } + } +} diff --git a/Program.cs b/Program.cs index 57feb17..69244aa 100644 --- a/Program.cs +++ b/Program.cs @@ -24,6 +24,7 @@ builder.Services.AddAutoMapper(typeof(Program)); // Custom services configuration. builder.Services.AddSingleton(s => new TokenGenerator(builder.Configuration["Jwt:Key"])); builder.Services.AddScoped(); +builder.Services.AddScoped(); // End of custom services configuration. // Swagger configuration. diff --git a/Services/IRaffleService.cs b/Services/IRaffleService.cs new file mode 100644 index 0000000..2326629 --- /dev/null +++ b/Services/IRaffleService.cs @@ -0,0 +1,11 @@ +using BackendPIA.Models; + +namespace BackendPIA.Services { + public interface IRaffleService { + public Task CreateRaffle(Raffle to_create); + public Task UpdateRaffle(Raffle to_update); + public Task> GetRaffles(string query); + public Task GetRaffle(long id); + public Task DeleteRaffle(long id); + } +} \ No newline at end of file diff --git a/Services/ITokenGenerator.cs b/Services/ITokenGenerator.cs index 32db2b6..4a66029 100644 --- a/Services/ITokenGenerator.cs +++ b/Services/ITokenGenerator.cs @@ -1,4 +1,3 @@ -using System.Security.Claims; using BackendPIA.Models; namespace BackendPIA.Services { diff --git a/Services/RaffleService.cs b/Services/RaffleService.cs new file mode 100644 index 0000000..6cdae3b --- /dev/null +++ b/Services/RaffleService.cs @@ -0,0 +1,61 @@ +using Microsoft.EntityFrameworkCore; +using BackendPIA.Models; + +namespace BackendPIA.Services { + public class RaffleService : IRaffleService { + private readonly ApplicationDbContext _context; + + public RaffleService(ApplicationDbContext context) { + _context = context; + } + + public async Task CreateRaffle(Raffle to_create) { + to_create.IsClosed = false; + + await _context.AddAsync(to_create); + await _context.SaveChangesAsync(); + + return to_create; + } + + public async Task UpdateRaffle(Raffle to_update) { + bool it_exists = _context.Raffles.Any(r => r.Id == to_update.Id); + + if(!it_exists) + return null; + + _context.Update(to_update); + await _context.SaveChangesAsync(); + + return to_update; + } + + public async Task> GetRaffles(string query) { + if(String.IsNullOrEmpty(query)) + return await _context.Raffles.ToListAsync(); + + return _context.Raffles.Where(r => r.Name == query); + } + + public async Task GetRaffle(long id) { + var raffle = await _context.Raffles.FindAsync(id); + + if(raffle == null) + return null; + + return raffle; + } + + public async Task DeleteRaffle(long id) { + var to_delete = await _context.Raffles.FindAsync(id); + + if(to_delete == null) + return false; + + _context.Raffles.Remove(to_delete); + await _context.SaveChangesAsync(); + + return true; + } + } +} \ No newline at end of file -- cgit v1.2.3