diff options
-rw-r--r-- | Controllers/RafflesController.cs | 70 | ||||
-rw-r--r-- | Forms/RaffleForm.cs | 13 | ||||
-rw-r--r-- | Migrations/ApplicationDbContextModelSnapshot.cs | 2 | ||||
-rw-r--r-- | Models/Raffle.cs | 2 | ||||
-rw-r--r-- | Profiles/RaffleProfile.cs | 11 | ||||
-rw-r--r-- | Program.cs | 1 | ||||
-rw-r--r-- | Services/IRaffleService.cs | 11 | ||||
-rw-r--r-- | Services/ITokenGenerator.cs | 1 | ||||
-rw-r--r-- | Services/RaffleService.cs | 61 |
9 files changed, 169 insertions, 3 deletions
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<IEnumerable<Raffle>> Get([FromQuery] string name = "") { + return await _service.GetRaffles(name); + } + + [HttpGet("{id:int}")] + public async Task<ActionResult<Raffle>> 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<ActionResult<Raffle>> Create(RaffleForm form) { + var raffle = await _service.CreateRaffle(_mapper.Map<Raffle>(form)); + + return raffle; + } + + [Authorize(Roles = "Administrator")] + [HttpPut("{id:int}")] + public async Task<ActionResult<Raffle>> Update(long id, RaffleForm form) { + var raffle = _mapper.Map<Raffle>(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<ActionResult> 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<RaffleForm, Raffle>().ReverseMap(); + } + } +} @@ -24,6 +24,7 @@ builder.Services.AddAutoMapper(typeof(Program)); // Custom services configuration. builder.Services.AddSingleton<ITokenGenerator>(s => new TokenGenerator(builder.Configuration["Jwt:Key"])); builder.Services.AddScoped<IUserAccountService, UserAccountService>(); +builder.Services.AddScoped<IRaffleService, RaffleService>(); // 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<Raffle> CreateRaffle(Raffle to_create); + public Task<Raffle> UpdateRaffle(Raffle to_update); + public Task<IEnumerable<Raffle>> GetRaffles(string query); + public Task<Raffle> GetRaffle(long id); + public Task<bool> 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<Raffle> CreateRaffle(Raffle to_create) { + to_create.IsClosed = false; + + await _context.AddAsync(to_create); + await _context.SaveChangesAsync(); + + return to_create; + } + + public async Task<Raffle> 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<IEnumerable<Raffle>> GetRaffles(string query) { + if(String.IsNullOrEmpty(query)) + return await _context.Raffles.ToListAsync(); + + return _context.Raffles.Where(r => r.Name == query); + } + + public async Task<Raffle> GetRaffle(long id) { + var raffle = await _context.Raffles.FindAsync(id); + + if(raffle == null) + return null; + + return raffle; + } + + public async Task<bool> 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 |