From 17b13d82dd0cd4ceba513d3354ade9420528e7bf Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Tue, 22 Nov 2022 18:01:02 -0600 Subject: Añadido método show en el controlador de usuarios MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/RafflesController.cs | 3 --- Controllers/UserAccountsController.cs | 13 +++++++++++++ Forms/RaffleDTO.cs | 8 ++++++++ Forms/TicketDTO.cs | 8 ++++++++ Forms/TicketForm.cs | 13 +++++++++++++ Forms/UserAccountDTO.cs | 8 ++++++++ Profiles/UserAccountProfile.cs | 19 +++++++++++++++++++ Services/IUserAccountService.cs | 1 + Services/UserAccountService.cs | 7 +++++++ 9 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 Forms/RaffleDTO.cs create mode 100644 Forms/TicketDTO.cs create mode 100644 Forms/TicketForm.cs create mode 100644 Forms/UserAccountDTO.cs diff --git a/Controllers/RafflesController.cs b/Controllers/RafflesController.cs index 32604bb..26b5a96 100644 --- a/Controllers/RafflesController.cs +++ b/Controllers/RafflesController.cs @@ -1,12 +1,9 @@ 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")] diff --git a/Controllers/UserAccountsController.cs b/Controllers/UserAccountsController.cs index 51c2481..2a0a8dd 100644 --- a/Controllers/UserAccountsController.cs +++ b/Controllers/UserAccountsController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc; using BackendPIA.Forms; using BackendPIA.Models; using BackendPIA.Services; +using Microsoft.AspNetCore.Authorization; using BackendPIA.Errors; using BackendPIA.Logics; @@ -25,6 +26,18 @@ namespace BackendPIA.Controllers { _token_generator = token_generator; } + [HttpGet("user")] + [Authorize(Roles = "Regular")] + public async Task> Show() { + string email = HttpContext.User.Claims.Where(c => c.Type.Contains("email")).First().Value; + var user = await _user_account_service.GetUserAccount(email); + + if(user == null) + return StatusCode(404, "User couldn't be found."); + + return Ok(_mapper.Map(user)); + } + [HttpPost("signup")] public async Task> Create(UserAccountForm form) { CreateUserAccountLogic logic = new CreateUserAccountLogic(_token_generator, _manager, form, _mapper, _user_account_service, "Regular"); diff --git a/Forms/RaffleDTO.cs b/Forms/RaffleDTO.cs new file mode 100644 index 0000000..b7e151d --- /dev/null +++ b/Forms/RaffleDTO.cs @@ -0,0 +1,8 @@ +namespace BackendPIA.Forms { + public class RaffleDTO { + public long Id { get; set; } + public string? Name { get; set; } + public int Winners { get; set; } + public bool IsClosed { get; set; } + } +} \ No newline at end of file diff --git a/Forms/TicketDTO.cs b/Forms/TicketDTO.cs new file mode 100644 index 0000000..0732708 --- /dev/null +++ b/Forms/TicketDTO.cs @@ -0,0 +1,8 @@ +namespace BackendPIA.Forms { + public class TicketDTO { + public long Id { get; set; } + public int Number { get; set; } + public bool IsWinner { get; set; } + public long RaffleId { get; set; } + } +} \ No newline at end of file diff --git a/Forms/TicketForm.cs b/Forms/TicketForm.cs new file mode 100644 index 0000000..26e7302 --- /dev/null +++ b/Forms/TicketForm.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace BackendPIA.Forms { + public class TicketForm { + [Required] + [Range(1, 54, ErrorMessage = "Value for {0} must be between {1} and {2}.")] + public int Number { get; set; } + [Required] + public long UserAccountId { get; set; } + [Required] + public long RaffleId { get; set; } + } +} \ No newline at end of file diff --git a/Forms/UserAccountDTO.cs b/Forms/UserAccountDTO.cs new file mode 100644 index 0000000..06b0df7 --- /dev/null +++ b/Forms/UserAccountDTO.cs @@ -0,0 +1,8 @@ +namespace BackendPIA.Forms { + public class UserAccountDTO { + public string Id { get; set; } + public string? Email { get; set; } + public string? UserName { get; set; } + public ICollection? Tickets { get; set; } + } +} \ No newline at end of file diff --git a/Profiles/UserAccountProfile.cs b/Profiles/UserAccountProfile.cs index 073b86b..7695265 100644 --- a/Profiles/UserAccountProfile.cs +++ b/Profiles/UserAccountProfile.cs @@ -6,6 +6,25 @@ namespace BackendPIA.Profiles { public class UserAccountProfile : Profile { public UserAccountProfile() { CreateMap().ReverseMap(); + CreateMap().ForMember(dto => dto.Tickets, o => o.MapFrom(UserTickets)); + } + + private ICollection UserTickets(UserAccount user, UserAccountDTO dto) { + ICollection tickets = new List(); + + if(user.Tickets == null) + return tickets; + + foreach (var ticket in user.Tickets) { + tickets.Add(new TicketDTO { + Id = ticket.Id, + Number = ticket.Number, + IsWinner = ticket.IsWinner, + RaffleId = ticket.RaffleId + }); + } + + return tickets; } } } diff --git a/Services/IUserAccountService.cs b/Services/IUserAccountService.cs index 900e562..56c665c 100644 --- a/Services/IUserAccountService.cs +++ b/Services/IUserAccountService.cs @@ -4,5 +4,6 @@ using BackendPIA.Models; namespace BackendPIA.Services { public interface IUserAccountService { public Task CreateUserAccount(UserAccount user, string password, string role); + public Task GetUserAccount(string email); } } \ No newline at end of file diff --git a/Services/UserAccountService.cs b/Services/UserAccountService.cs index e81ea76..eccaff3 100644 --- a/Services/UserAccountService.cs +++ b/Services/UserAccountService.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Identity; using BackendPIA.Models; +using BackendPIA.Forms; namespace BackendPIA.Services { public class UserAccountService : IUserAccountService { @@ -17,5 +18,11 @@ namespace BackendPIA.Services { return result; } + + public async Task GetUserAccount(string email) { + var result = await _manager.FindByEmailAsync(email); + + return result; + } } } \ No newline at end of file -- cgit v1.2.3