From 1f9c04e2fcd0a0dbbf785d830925439a2fe63d4c Mon Sep 17 00:00:00 2001 From: eee4 <41441600+eee4@users.noreply.github.com> Date: Mon, 21 Jul 2025 09:47:31 +0200 Subject: [PATCH] feat: return user's role name inside UserInfoDTO --- Controllers/AuthController.cs | 4 ++-- Controllers/CategoryController.cs | 10 ++++++++-- DTOs/UserInfoDTO.cs | 1 + Mapping/UserMapping.cs | 9 +++++---- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Controllers/AuthController.cs b/Controllers/AuthController.cs index f0793f6..daba614 100644 --- a/Controllers/AuthController.cs +++ b/Controllers/AuthController.cs @@ -70,7 +70,7 @@ public class AuthController : ControllerBase { // All set - generate the token and return it var token = guhf.GenerateJwtToken(user); - SuccessfulLoginDTO response = user.ToSuccessfulLoginDTO(token); + SuccessfulLoginDTO response = user.ToSuccessfulLoginDTO(token, guhf.UserRoleAsStr(user)); return Ok(response); } else return Unauthorized(new {status = "error", error_msg = "Unknown pair of email and password"}); @@ -149,7 +149,7 @@ public class AuthController : ControllerBase return BadRequest(new ErrorDTO { Status = "error", Error_msg = "User not found" }); // Return user data as a DTO - return Ok(u.ToUserInfoDTO()); + return Ok(u.ToUserInfoDTO(guhf.UserRoleAsStr(u))); } diff --git a/Controllers/CategoryController.cs b/Controllers/CategoryController.cs index 552b3e8..535864a 100644 --- a/Controllers/CategoryController.cs +++ b/Controllers/CategoryController.cs @@ -71,9 +71,9 @@ public class CategoryController : ControllerBase /// [AUTHED] Create a new category /// /// - /// Allows authorized users to create categories.
- /// Important! + /// Allows authorized users to create categories. ///

+ /// Important! /// Category names are case insensitive.
/// Has CORS set. ///
@@ -110,4 +110,10 @@ public class CategoryController : ControllerBase } + // TODO: Update category + // PATCH /api/v1/categories/1 + + // TODO: Delete category + // DELETE /api/v1/categories/1 + } diff --git a/DTOs/UserInfoDTO.cs b/DTOs/UserInfoDTO.cs index f30dbda..1dc4930 100644 --- a/DTOs/UserInfoDTO.cs +++ b/DTOs/UserInfoDTO.cs @@ -6,5 +6,6 @@ public record class UserInfoDTO required public string Name { get; set; } required public string Email { get; set; } public int Role { get; set; } + public string? RoleName { get; set; } }; diff --git a/Mapping/UserMapping.cs b/Mapping/UserMapping.cs index 81dbc15..52f4324 100644 --- a/Mapping/UserMapping.cs +++ b/Mapping/UserMapping.cs @@ -5,18 +5,18 @@ namespace QuotifyBE.Mapping; public static class UserMapping { - public static SuccessfulLoginDTO ToSuccessfulLoginDTO(this User user, string token) + public static SuccessfulLoginDTO ToSuccessfulLoginDTO(this User user, string token, string? roleName) { return new SuccessfulLoginDTO { Status = "ok", Token = token, - User = user.ToUserInfoDTO() + User = user.ToUserInfoDTO(roleName) }; } - public static UserInfoDTO ToUserInfoDTO(this User user) + public static UserInfoDTO ToUserInfoDTO(this User user, string? roleName) { return new UserInfoDTO @@ -24,7 +24,8 @@ public static class UserMapping Id = user.Id, Name = user.Name, Email = user.Email, - Role = user.Role + Role = user.Role, + RoleName = roleName }; } }