Skip to content

Commit

Permalink
Add Role to UserViewModel (#211)
Browse files Browse the repository at this point in the history
* Add JsonStringEnumConverter.

* Add Role field to UserViewModel.

* Update openapi.json.
  • Loading branch information
TheTedder authored Jun 18, 2024
1 parent f5215cd commit 2478e84
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion LeaderboardBackend.Test/Features/Users/RegistrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public async Task Register_ValidRequest_CreatesAndReturnsUser()
content.Should().NotBeNull().And.BeEquivalentTo(new UserViewModel
{
Id = content!.Id,
Username = request.Username
Username = request.Username,
Role = UserRole.Registered
});
emailSenderMock.Verify(x =>
x.EnqueueEmailAsync(
Expand Down
16 changes: 9 additions & 7 deletions LeaderboardBackend/Models/ViewModels/UserViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json.Serialization;
using LeaderboardBackend.Models.Entities;

namespace LeaderboardBackend.Models.ViewModels;
Expand Down Expand Up @@ -28,12 +29,13 @@ public record UserViewModel
/// <example>J'on-Doe</example>
public required string Username { get; set; }

public static UserViewModel MapFrom(User user)
[JsonConverter(typeof(JsonStringEnumConverter))]
public required UserRole Role { get; set; }

public static UserViewModel MapFrom(User user) => new()
{
return new UserViewModel
{
Id = user.Id,
Username = user.Username,
};
}
Id = user.Id,
Username = user.Username,
Role = user.Role
};
}
1 change: 1 addition & 0 deletions LeaderboardBackend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
{
opt.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
opt.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
Expand Down
32 changes: 22 additions & 10 deletions LeaderboardBackend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,17 +1244,16 @@
},
"IsoDayOfWeek": {
"enum": [
0,
1,
2,
3,
4,
5,
6,
7
"None",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
],
"type": "integer",
"format": "int32"
"type": "string"
},
"LeaderboardViewModel": {
"required": [
Expand Down Expand Up @@ -1482,9 +1481,19 @@
},
"additionalProperties": false
},
"UserRole": {
"enum": [
"Registered",
"Confirmed",
"Administrator",
"Banned"
],
"type": "string"
},
"UserViewModel": {
"required": [
"id",
"role",
"username"
],
"type": "object",
Expand All @@ -1498,6 +1507,9 @@
"type": "string",
"description": "The username of the `User`. It:\r\n<ul><li>must be [2, 25] in length;</li><li>must be made up of alphanumeric characters around zero or one of:</li><ul><li>hyphen;</li><li>underscore; or</li><li>apostrophe</li></ul></ul>\r\nUsernames are saved case-sensitively, but matcehd against case-insensitively.\r\nA `User` may not register with the name 'Cool' when another `User` with the name 'cool'\r\nexists.",
"example": "J'on-Doe"
},
"role": {
"$ref": "#/components/schemas/UserRole"
}
},
"additionalProperties": false
Expand Down

0 comments on commit 2478e84

Please sign in to comment.