summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-11-22 18:01:02 -0600
committerHombreLaser <sebastian-440@live.com>2022-11-22 18:01:02 -0600
commit17b13d82dd0cd4ceba513d3354ade9420528e7bf (patch)
tree71b88eb5a0fb15790364484ee2cac6f801291db9
parent214cd4db690635ba2a30183c1679c3e4d7b8e896 (diff)
Añadido método show en el controlador de usuarios
-rw-r--r--Controllers/RafflesController.cs3
-rw-r--r--Controllers/UserAccountsController.cs13
-rw-r--r--Forms/RaffleDTO.cs8
-rw-r--r--Forms/TicketDTO.cs8
-rw-r--r--Forms/TicketForm.cs13
-rw-r--r--Forms/UserAccountDTO.cs8
-rw-r--r--Profiles/UserAccountProfile.cs19
-rw-r--r--Services/IUserAccountService.cs1
-rw-r--r--Services/UserAccountService.cs7
9 files changed, 77 insertions, 3 deletions
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<ActionResult<UserAccountDTO>> 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<UserAccountDTO>(user));
+ }
+
[HttpPost("signup")]
public async Task<ActionResult<AuthenticationToken>> 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<TicketDTO>? 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<UserAccountForm, UserAccount>().ReverseMap();
+ CreateMap<UserAccount, UserAccountDTO>().ForMember(dto => dto.Tickets, o => o.MapFrom(UserTickets));
+ }
+
+ private ICollection<TicketDTO> UserTickets(UserAccount user, UserAccountDTO dto) {
+ ICollection<TicketDTO> tickets = new List<TicketDTO>();
+
+ 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<IdentityResult> CreateUserAccount(UserAccount user, string password, string role);
+ public Task<UserAccount> 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<UserAccount> GetUserAccount(string email) {
+ var result = await _manager.FindByEmailAsync(email);
+
+ return result;
+ }
}
} \ No newline at end of file