Skip to content

Commit df54bf7

Browse files
committed
Finished most parts, lacking authentication on removals/updatesa
1 parent e7321ce commit df54bf7

File tree

2 files changed

+139
-7
lines changed

2 files changed

+139
-7
lines changed

exercise.tests/IntegrationTests/PostTests.cs

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public async Task AddCommentToPost_Failure(int postId, int userId, string conten
361361
public async Task GetCommentsForPost_Success()
362362
{
363363
// Arrange
364-
int postId = 6;
364+
int postId = 5;
365365

366366
// Act
367367
var response = await _client.GetAsync($"/posts/{postId}/comments");
@@ -463,5 +463,101 @@ public async Task DeleteComment_SuccessAndThenNotFound()
463463
Assert.That(deleteAgainResponse.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
464464
}
465465

466+
467+
[Test]
468+
public async Task GetPostsByUser_Success()
469+
{
470+
// Arrange
471+
int userId = 1; // CHeck if this user still has posts if this test fails (manuially)
472+
473+
// Act
474+
var response = await _client.GetAsync($"/posts/user/{userId}");
475+
var contentString = await response.Content.ReadAsStringAsync();
476+
var message = JsonNode.Parse(contentString);
477+
Console.WriteLine(message);
478+
// Assert status
479+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
480+
481+
482+
Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("Success"));
483+
var data = message?["data"]?.AsArray();
484+
Assert.That(data, Is.Not.Null);
485+
Assert.That(data!.Count, Is.GreaterThan(0), "Expected user 1 to have at least one post.");
486+
487+
// Assert that ALL posts in the response belong to the correct user
488+
foreach (var post in data!)
489+
{
490+
Assert.That(post?["user"]?["id"]?.GetValue<int>(), Is.EqualTo(userId));
491+
}
492+
493+
var firstPost = data.First();
494+
Assert.That(firstPost?["id"]?.GetValue<int>(), Is.GreaterThan(0));
495+
Assert.That(firstPost?["content"]?.GetValue<string>(), Is.Not.Null);
496+
}
497+
498+
[Test]
499+
public async Task GetPostsByUser_NotFound()
500+
{
501+
// Arrange
502+
int nonExistentUserId = 99999;
503+
504+
// Act
505+
var response = await _client.GetAsync($"/posts/user/{nonExistentUserId}");
506+
var contentString = await response.Content.ReadAsStringAsync();
507+
var message = JsonNode.Parse(contentString);
508+
509+
// Assert
510+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
511+
Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("No posts found for this user"));
512+
Assert.That(message?["data"], Is.Null);
513+
}
514+
515+
[Test]
516+
public async Task GetCommentsByUser_Success()
517+
{
518+
// Arrange
519+
int userId = 1; // Check if this user still has comments if this fails
520+
521+
// Act
522+
var response = await _client.GetAsync($"/comments/user/{userId}");
523+
var contentString = await response.Content.ReadAsStringAsync();
524+
var message = JsonNode.Parse(contentString);
525+
Console.WriteLine(message);
526+
// Assert status
527+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
528+
529+
Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("Success"));
530+
var data = message?["data"]?.AsArray();
531+
Assert.That(data, Is.Not.Null);
532+
Assert.That(data!.Count, Is.GreaterThan(0), "Expected user 1 to have at least one comment.");
533+
534+
// Assert that ALL comments in the response belong to the correct user
535+
foreach (var comment in data!)
536+
{
537+
Assert.That(comment?["user"]?["id"]?.GetValue<int>(), Is.EqualTo(userId));
538+
}
539+
540+
var firstComment = data.First();
541+
Assert.That(firstComment?["id"]?.GetValue<int>(), Is.GreaterThan(0));
542+
Assert.That(firstComment?["content"]?.GetValue<string>(), Is.Not.Null.Or.Empty);
543+
}
544+
545+
[Test]
546+
public async Task GetCommentsByUser_NotFound()
547+
{
548+
// Arrange
549+
int nonExistentUserId = 99999;
550+
551+
// Act
552+
var response = await _client.GetAsync($"/comments/user/{nonExistentUserId}");
553+
var contentString = await response.Content.ReadAsStringAsync();
554+
var message = JsonNode.Parse(contentString);
555+
556+
// Assert
557+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
558+
Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("No comments found for this user"));
559+
Assert.That(message?["data"], Is.Null);
560+
}
561+
466562
}
467563
}

exercise.wwwapi/Endpoints/PostEndpoints.cs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public static void ConfigurePostEndpoints(this WebApplication app)
2121
posts.MapDelete("/{id}", DeletePost).WithSummary("Remove a certain post");
2222

2323
posts.MapPost("/{postId}/comments", AddCommentToPost).WithSummary("Add a new comment to a post");
24-
posts.MapGet("/{postId}/comments", GetCommentsForPost).WithSummary("Get comments for a specific post (with pagination)");
24+
posts.MapGet("/{postId}/comments", GetCommentsForPost).WithSummary("Get comments for a specific post");
2525

2626
// Standalone comment endpoints for editing/deleting
2727
var comments = app.MapGroup("comments");
2828
comments.MapPatch("/{id}", UpdateComment).WithSummary("Edit an existing comment");
2929
comments.MapDelete("/{id}", DeleteCommentById).WithSummary("Remove an existing comment");
3030

31+
// Endpoints to get by user
32+
posts.MapGet("/user/{userId}", GetPostsByUser).WithSummary("Get posts by a specific user");
33+
comments.MapGet("/user/{userId}", GetCommentsByUser).WithSummary("Get comments by a specific user");
3134
}
3235

3336

@@ -156,12 +159,13 @@ private static IResult AddCommentToPost(IRepository<PostComment> commentService,
156159
}
157160

158161
[ProducesResponseType(StatusCodes.Status200OK)]
159-
private static IResult GetCommentsForPost(IRepository<PostComment> service, IMapper mapper, int postId)
162+
private static IResult GetCommentsForPost(IRepository<Post> postservice, IMapper mapper, int postId)
160163
{
161-
PostComment? comment = service.GetById(postId, q => q.Include(c => c.User));
162-
if (comment == null) return TypedResults.NotFound(new ResponseDTO<Object> { Message = "Comment not found" });
163-
PostCommentDTO commentDTO = mapper.Map<PostCommentDTO>(comment);
164-
return TypedResults.Ok(new ResponseDTO<PostCommentDTO> { Message = "Success", Data = commentDTO });
164+
Post? post = postservice.GetById(postId, q => q.Include(p => p.User).Include(p => p.Comments).ThenInclude(c => c.User));
165+
if (post == null) return TypedResults.NotFound(new ResponseDTO<Object> { Message = "Post not found" });
166+
List<PostComment> comments = [.. post.Comments];
167+
List<PostCommentDTO> commentsDTO = mapper.Map<List<PostCommentDTO>>(comments);
168+
return TypedResults.Ok(new ResponseDTO<List<PostCommentDTO>> { Message = "Success", Data = commentsDTO });
165169
}
166170

167171
[ProducesResponseType(StatusCodes.Status200OK)]
@@ -205,5 +209,37 @@ private static IResult DeleteCommentById(IRepository<PostComment> service, int i
205209

206210
return TypedResults.Ok(new ResponseDTO<object> { Message = "Comment deleted successfully." });
207211
}
212+
213+
[ProducesResponseType(StatusCodes.Status200OK)]
214+
[ProducesResponseType(StatusCodes.Status404NotFound)]
215+
private static IResult GetPostsByUser(IRepository<Post> service, IMapper mapper, int userid)
216+
{
217+
IEnumerable<Post> results = service.GetWithIncludes(q => q.Where(p => p.UserId == userid).Include(p => p.User).Include(p => p.Comments).ThenInclude(c => c.User));
218+
if (results.Count() == 0) return TypedResults.NotFound(new ResponseDTO<Object> { Message = "No posts found for this user" });
219+
220+
IEnumerable<PostDTO> postDTOs = mapper.Map<IEnumerable<PostDTO>>(results);
221+
ResponseDTO<IEnumerable<PostDTO>> response = new ResponseDTO<IEnumerable<PostDTO>>()
222+
{
223+
Message = "Success",
224+
Data = postDTOs
225+
};
226+
return TypedResults.Ok(response);
227+
}
228+
229+
[ProducesResponseType(StatusCodes.Status200OK)]
230+
[ProducesResponseType(StatusCodes.Status404NotFound)]
231+
private static IResult GetCommentsByUser(IRepository<PostComment> service, IMapper mapper, int userid)
232+
{
233+
IEnumerable<PostComment> results = service.GetWithIncludes(q => q.Where(p => p.UserId == userid).Include(p => p.User));
234+
if (results.Count() == 0) return TypedResults.NotFound(new ResponseDTO<Object> { Message = "No comments found for this user" });
235+
236+
IEnumerable<PostCommentDTO> PostCommentDTOs = mapper.Map<IEnumerable<PostCommentDTO>>(results);
237+
ResponseDTO<IEnumerable<PostCommentDTO>> response = new ResponseDTO<IEnumerable<PostCommentDTO>>()
238+
{
239+
Message = "Success",
240+
Data = PostCommentDTOs
241+
};
242+
return TypedResults.Ok(response);
243+
}
208244
}
209245
}

0 commit comments

Comments
 (0)