Skip to content

Commit 87da12e

Browse files
committed
updated claimsPrincipal to contain users first/last name. updated PostDTOs to contain more restricted data
1 parent 3f3c2e7 commit 87da12e

File tree

5 files changed

+26
-20
lines changed

5 files changed

+26
-20
lines changed

exercise.wwwapi/DTOs/Posts/CreatePostRequestDTO.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ namespace exercise.wwwapi.DTOs.Posts
44
{
55
public class CreatePostRequestDTO
66
{
7-
[JsonPropertyName("author_id")]
8-
public int AuthorId { get; set; }
9-
107
[JsonPropertyName("body")]
118
public string Body { get; set; }
129

exercise.wwwapi/Endpoints/PostEndpoints.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,20 @@ public static async Task<IResult> CreatePost(
6565
postRepository.Insert(post);
6666
await postRepository.SaveAsync();
6767

68-
var response = new ResponseDTO<CreatePostSuccessDTO>
69-
{
70-
Status = "success",
71-
Data = new CreatePostSuccessDTO
68+
var response = new ResponseDTO<CreatePostSuccessDTO>
7269
{
73-
Posts = new PostDTO
70+
Status = "success",
71+
Data = new CreatePostSuccessDTO
7472
{
75-
Body = post.Body,
76-
CreatedAt = post.CreatedAt
73+
Posts = new PostDTO
74+
{
75+
Body = post.Body,
76+
CreatedAt = post.CreatedAt,
77+
Firstname = claimsPrincipal.FirstName(),
78+
Lastname = claimsPrincipal.LastName()
79+
}
7780
}
78-
}
79-
};
81+
};
8082

8183
return Results.Created($"/posts/{post.Id}", response);
8284
}
@@ -131,7 +133,7 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i
131133
{
132134
return Results.Unauthorized();
133135
}
134-
var userClaimName = claimsPrincipal.Identity?.Name;
136+
var userClaimName = $"{claimsPrincipal.FirstName()} {claimsPrincipal.LastName()}";
135137

136138
var post = await postRepository.GetByIdWithIncludes(p => p.Include(a => a.Author)
137139
.Include(c => c.Comments)
@@ -224,8 +226,6 @@ public static async Task<IResult> DeletePost(IRepository<Post> postRepository, i
224226
Status = "success",
225227
Data = new PostDTO
226228
{
227-
//Id = post.Id,
228-
//AuthorId = post.AuthorId,
229229
Body = post.Body,
230230
CreatedAt = post.CreatedAt
231231
}

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ private static string CreateToken(User user, IConfigurationSettings configuratio
374374
new(ClaimTypes.Sid, user.Id.ToString()),
375375
new(ClaimTypes.Name, user.Username),
376376
new(ClaimTypes.Email, user.Email),
377-
new(ClaimTypes.Role, user.Role.ToString())
377+
new(ClaimTypes.Role, user.Role.ToString()),
378+
new("FirstName", user.FirstName),
379+
new("LastName", user.LastName)
378380
};
379381

380382
var tokenKey = Environment.GetEnvironmentVariable(Globals.EnvironmentEnvVariable) == "Staging"

exercise.wwwapi/Helpers/ClaimsPrincipalHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,15 @@ public static string UserId(this ClaimsPrincipal user)
3535
return int.Parse(claim?.Value);
3636
}
3737

38+
public static string? FirstName(this ClaimsPrincipal user)
39+
{
40+
Claim? claim = user.FindFirst("FirstName");
41+
return claim?.Value;
42+
}
43+
public static string? LastName(this ClaimsPrincipal user)
44+
{
45+
Claim? claim = user.FindFirst("LastName");
46+
return claim?.Value;
47+
}
48+
3849
}

exercise.wwwapi/Validators/PostValidators/CreatePostValidator.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ public class CreatePostValidator :AbstractValidator<CreatePostRequestDTO>
77
{
88
public CreatePostValidator()
99
{
10-
RuleFor(x => x.AuthorId)
11-
.GreaterThan(0)
12-
.WithMessage("AuthorId must be a valid user id.");
13-
1410
RuleFor(x => x.Body)
1511
.NotEmpty().WithMessage("Post body cannot be empty.")
1612
.MaximumLength(1000).WithMessage("Post body cannot exceed 1000 characters.")

0 commit comments

Comments
 (0)