Skip to content

Commit 253a45b

Browse files
authored
Merge pull request #75 from boolean-uk/temp_main_post
Temp main post ok -mona
2 parents 37ac1f1 + fa57001 commit 253a45b

File tree

13 files changed

+59
-74
lines changed

13 files changed

+59
-74
lines changed

api.tests/PostEndpointTests/UpdatePostTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ public async Task UpdatePostPassesTest()
8585
Assert.That(updatedResult, Is.Not.Null, "Update Failed");
8686
Assert.That(patchResponse.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK));
8787
Assert.That(updatedResult!.Data.Body, Is.EqualTo(newBody));
88-
Assert.That(updatedResult!.Data.Id, Is.EqualTo(1));
89-
Assert.That(updatedResult!.Data.AuthorId, Is.EqualTo(1));
9088
}
9189
}
9290
}

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/DTOs/Posts/GetPosts/CommentDTO.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@ namespace exercise.wwwapi.DTOs.Posts.GetPosts
44
{
55
public class CommentDTO
66
{
7-
public int Id { get; set; }
8-
public int UserId { get; set; }
9-
public string Body { get; set; } = string.Empty;
10-
public DateTime CreatedAt { get; set; }
117
public string? firstName { get; set; }
128
public string? lastName { get; set; }
13-
public CommentDTO()
14-
{
15-
}
9+
public string Body { get; set; } = string.Empty;
10+
public DateTime CreatedAt { get; set; }
11+
public DateTime? UpdatedAt { get; set; }
12+
public string? UpdatedBy { get; set; }
13+
public CommentDTO() { }
1614
public CommentDTO(Comment model)
1715
{
18-
Id = model.Id;
19-
UserId = model.UserId;
2016
Body = model.Body;
2117
CreatedAt = model.CreatedAt;
2218
if (model.User != null)
2319
{
2420
firstName = model.User.FirstName;
2521
lastName = model.User.LastName;
2622
}
23+
UpdatedAt = model.UpdatedAt;
24+
UpdatedBy = model.UpdatedBy;
2725
}
2826
}
2927
}

exercise.wwwapi/DTOs/Posts/GetPosts/PostDTO.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,26 @@ namespace exercise.wwwapi.DTOs.Posts.GetPosts
55
{
66
public class PostDTO
77
{
8-
public int Id { get; set; }
9-
public int AuthorId { get; set; }
108
public string Firstname { get; set; }
119
public string Lastname { get; set; }
1210
public string Body { get; set; } = string.Empty;
1311
public DateTime CreatedAt { get; set; }
12+
public DateTime? UpdatedAt { get; set; }
13+
public string? UpdatedBy { get; set; }
1414
public List<CommentDTO> Comments { get; set; } = new List<CommentDTO>();
1515
public List<LikeDTO> Likes { get; set; } = new List<LikeDTO>();
1616

17-
public PostDTO()
18-
{
19-
20-
}
17+
public PostDTO() { }
2118
public PostDTO(Post model)
2219
{
23-
Id = model.Id;
24-
AuthorId = model.AuthorId;
25-
Body = model.Body;
26-
CreatedAt = model.CreatedAt;
2720
Firstname = model.Author.FirstName;
2821
Lastname = model.Author.LastName;
22+
Body = model.Body;
23+
CreatedAt = model.CreatedAt;
2924
Comments = model.Comments.Select(c => new CommentDTO(c)).ToList();
3025
Likes = model.Likes.Select(l => new LikeDTO(l)).ToList();
26+
UpdatedAt = model.UpdatedAt;
27+
UpdatedBy = model.UpdatedBy;
3128
}
3229
}
3330
}

exercise.wwwapi/DTOs/Posts/UpdatePost/UpdatePostSuccessDTO.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ namespace exercise.wwwapi.DTOs.Posts.UpdatePost
44
{
55
public class UpdatePostSuccessDTO
66
{
7-
[JsonPropertyName("id")]
8-
public int Id { get; set; }
9-
10-
[JsonPropertyName("author_id")]
11-
public int AuthorId { get; set; }
12-
137
[JsonPropertyName("body")]
148
public string Body { get; set; }
159

@@ -19,6 +13,6 @@ public class UpdatePostSuccessDTO
1913
[JsonPropertyName("created_at")]
2014
public DateTime CreatedAt { get; set; }
2115
public DateTime? UpdatedAt { get; set; }
22-
public int? UpdatedById { get; set; }
16+
public string? UpdatedBy { get; set; }
2317
}
2418
}

exercise.wwwapi/Endpoints/CommentEndpoints.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public static async Task<IResult> UpdateComment(
119119
{
120120
return Results.Unauthorized();
121121
}
122+
var userClaimName = claimsPrincipal.Identity?.Name;
122123

123124
var comment = await commentRepository.GetByIdWithIncludes(c => c.Include(u => u.User), id);
124125

@@ -127,12 +128,14 @@ public static async Task<IResult> UpdateComment(
127128
return TypedResults.NotFound();
128129
}
129130

130-
if (comment.UserId != userIdClaim)
131+
if (comment.UserId == userIdClaim || claimsPrincipal.IsInRole("Teacher"))
131132
{
132-
return Results.Unauthorized();
133+
comment.UpdatedAt = DateTime.UtcNow;
134+
comment.UpdatedBy = userClaimName;
133135
}
136+
else { return Results.Unauthorized(); }
134137

135-
var validation = await validator.ValidateAsync(request);
138+
var validation = await validator.ValidateAsync(request);
136139
if (!validation.IsValid)
137140
{
138141
var failureDto = new UpdateCommentFailureDTO();

exercise.wwwapi/Endpoints/PostEndpoints.cs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using exercise.wwwapi.DTOs.Posts.GetPosts;
55
using exercise.wwwapi.DTOs.Posts.UpdatePost;
66
using exercise.wwwapi.Helpers;
7+
using exercise.wwwapi.Models;
78
using exercise.wwwapi.Repository;
89
using FluentValidation;
910
using Microsoft.AspNetCore.Authorization;
@@ -64,20 +65,20 @@ public static async Task<IResult> CreatePost(
6465
postRepository.Insert(post);
6566
await postRepository.SaveAsync();
6667

67-
var response = new ResponseDTO<CreatePostSuccessDTO>
68-
{
69-
Status = "success",
70-
Data = new CreatePostSuccessDTO
68+
var response = new ResponseDTO<CreatePostSuccessDTO>
7169
{
72-
Posts = new PostDTO
70+
Status = "success",
71+
Data = new CreatePostSuccessDTO
7372
{
74-
Id = post.Id,
75-
AuthorId = post.AuthorId,
76-
Body = post.Body,
77-
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+
}
7880
}
79-
}
80-
};
81+
};
8182

8283
return Results.Created($"/posts/{post.Id}", response);
8384
}
@@ -132,33 +133,23 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i
132133
{
133134
return Results.Unauthorized();
134135
}
136+
var userClaimName = $"{claimsPrincipal.FirstName()} {claimsPrincipal.LastName()}";
135137

136138
var post = await postRepository.GetByIdWithIncludes(p => p.Include(a => a.Author)
137-
.Include(c => c.Comments)
138-
.Include(l => l.Likes), id);
139+
.Include(c => c.Comments)
140+
.Include(l => l.Likes), id);
139141

140142
if (post == null)
141143
{
142144
return TypedResults.NotFound();
143145
}
144146

145-
if (post.AuthorId != userIdClaim)
146-
{
147-
if (claimsPrincipal.IsInRole("Teacher"))
148-
{
149-
post.UpdatedAt = DateTime.UtcNow;
150-
post.UpdatedById = userIdClaim;
151-
}
152-
else
153-
{
154-
return Results.Unauthorized();
155-
}
156-
}
157-
else
147+
if (post.AuthorId == userIdClaim || claimsPrincipal.IsInRole("Teacher"))
158148
{
159149
post.UpdatedAt = DateTime.UtcNow;
160-
post.UpdatedById = userIdClaim;
150+
post.UpdatedBy = userClaimName;
161151
}
152+
else { return Results.Unauthorized(); }
162153

163154
var validation = await validator.ValidateAsync(request);
164155
if (!validation.IsValid)
@@ -188,12 +179,10 @@ public static async Task<IResult> UpdatePost(IRepository<Post> postRepository, i
188179
Status = "success",
189180
Data = new UpdatePostSuccessDTO
190181
{
191-
Id = post.Id,
192-
AuthorId = post.AuthorId,
193182
Body = post.Body,
194183
CreatedAt = post.CreatedAt,
195184
UpdatedAt = post.UpdatedAt,
196-
UpdatedById = post.UpdatedById
185+
UpdatedBy = post.UpdatedBy
197186

198187
}
199188
};
@@ -237,8 +226,6 @@ public static async Task<IResult> DeletePost(IRepository<Post> postRepository, i
237226
Status = "success",
238227
Data = new PostDTO
239228
{
240-
Id = post.Id,
241-
AuthorId = post.AuthorId,
242229
Body = post.Body,
243230
CreatedAt = post.CreatedAt
244231
}

exercise.wwwapi/Endpoints/UserEndpoints.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ private static string CreateToken(User user, IConfigurationSettings configuratio
379379
new(ClaimTypes.Sid, user.Id.ToString()),
380380
new(ClaimTypes.Name, user.Username),
381381
new(ClaimTypes.Email, user.Email),
382-
new(ClaimTypes.Role, user.Role.ToString())
382+
new(ClaimTypes.Role, user.Role.ToString()),
383+
new("FirstName", user.FirstName),
384+
new("LastName", user.LastName)
383385
};
384386

385387
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/Models/Comment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class Comment : IEntity
2626

2727
[Column("created_at")]
2828
public DateTime CreatedAt { get; set; }
29+
public DateTime? UpdatedAt { get; set; }
30+
public string? UpdatedBy { get; set; }
2931

3032
[JsonIgnore]
3133
public Post Post { get; set; }

0 commit comments

Comments
 (0)