Skip to content

Commit 61228b8

Browse files
author
Oyvind Timian Dokk Husveg
committed
Check that the get returns as expected and added Comments in the post
1 parent 9c73cf1 commit 61228b8

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

exercise.tests/IntegrationTests/PostTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,55 @@ public void TearDown()
3333
_factory.Dispose();
3434
}
3535

36+
[Test]
37+
public async Task GetAllPosts()
38+
{
39+
// Act: get all posts
40+
var response = await _client.GetAsync("/posts");
41+
var contentString = await response.Content.ReadAsStringAsync();
42+
var message = string.IsNullOrWhiteSpace(contentString) ? null : JsonNode.Parse(contentString);
43+
44+
// Assert status
45+
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
46+
47+
// Assert JSON structure
48+
Assert.That(message, Is.Not.Null);
49+
Assert.That(message?["message"]?.GetValue<string>(), Is.EqualTo("success"));
50+
51+
var data = message?["data"]?.AsArray();
52+
Assert.That(data, Is.Not.Null);
53+
Assert.That(data!.Count, Is.GreaterThan(0));
54+
55+
56+
Console.WriteLine("Message: " + message);
57+
58+
// Check first post for structure
59+
var post = data!.First();
60+
Assert.That(post["id"]?.GetValue<int>(), Is.GreaterThan(0));
61+
Assert.That(post["content"]?.GetValue<string>(), Is.Not.Null);
62+
Assert.That(post["numLikes"]?.GetValue<int>(), Is.Not.Null);
63+
Assert.That(post["createdAt"], Is.Not.Null);
64+
65+
// Check nested user
66+
var user = post["user"];
67+
Assert.That(user, Is.Not.Null);
68+
Assert.That(user!["id"]?.GetValue<int>(), Is.GreaterThan(0));
69+
Assert.That(user["firstName"]?.GetValue<string>(), Is.Not.Null);
70+
Assert.That(user["lastName"]?.GetValue<string>(), Is.Not.Null);
71+
Assert.That(user["photo"], Is.Not.Null);
72+
73+
// Check nested comments
74+
var comments = post["comments"]?.AsArray();
75+
Assert.That(comments, Is.Not.Null);
76+
// Check comments
77+
if (comments!.Count > 0)
78+
{
79+
var comment = comments!.First();
80+
Assert.That(comment["content"]?.GetValue<string>(), Is.Not.Null);
81+
Assert.That(comment["id"]?.GetValue<int>(), Is.GreaterThan(0));
82+
}
83+
}
84+
3685
[Test]
3786
public async Task SuccessfulCreatePostStatus()
3887
{

exercise.wwwapi/Data/DataContext.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2929
.Property(u => u.Role)
3030
.HasConversion<string>();
3131

32+
33+
//modelBuilder.Entity<User>()
34+
// .HasMany(u => u.Post)
35+
// .WithOne(p => p.User)
36+
// .HasForeignKey(p => p.UserId)
37+
// .OnDelete(DeleteBehavior.Cascade);
38+
39+
modelBuilder.Entity<Post>()
40+
.HasMany(p => p.Comments)
41+
.WithOne(c => c.Post)
42+
.HasForeignKey(c => c.PostId)
43+
.OnDelete(DeleteBehavior.Cascade);
44+
3245
//modelBuilder.Entity<UserCohort>()
3346
// .HasKey(tc => new { tc.UserId, tc.CohortId });
3447

exercise.wwwapi/Endpoints/PostEndpoints.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static IResult CreatePost(IRepository<User> userservice, IRepository<Post
5656
[ProducesResponseType(StatusCodes.Status200OK)]
5757
public static IResult GetAllPosts(IRepository<Post> service, IMapper mapper)
5858
{
59-
IEnumerable<Post> results = service.GetWithIncludes(q => q.Include(p => p.User));
59+
IEnumerable<Post> results = service.GetWithIncludes(q => q.Include(p => p.User).Include(p => p.Comments).ThenInclude(c => c.User));
6060
IEnumerable<PostDTO> postDTOs = mapper.Map<IEnumerable<PostDTO>>(results);
6161
ResponseDTO<IEnumerable<PostDTO>> response = new ResponseDTO<IEnumerable<PostDTO>>()
6262
{
@@ -94,7 +94,7 @@ public static IResult UpdatePost(IRepository<Post> service, IMapper mapper, int
9494
[ProducesResponseType(StatusCodes.Status404NotFound)]
9595
private static IResult DeletePost(IRepository<Post> service, int id)
9696
{
97-
Post? post = service.GetById(id, q => q.Include(p => p.User));
97+
Post? post = service.GetById(id, q => q.Include(p => p.User).Include(p => p.Comments).ThenInclude(c => c.User));
9898
if (post == null) return TypedResults.NotFound(new ResponseDTO<Object> { Message = "Post not found" });
9999

100100
service.Delete(id);

exercise.wwwapi/Tools/MappingProfile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public MappingProfile()
1717
CreateMap<User, UserBasicDTO>();
1818
CreateMap<Post, PostDTO>();
1919

20+
CreateMap<PostComment, PostCommentDTO>()
21+
.ForMember(dest => dest.User, opt => opt.MapFrom(src => src.User));
22+
2023
}
2124
}
2225
}

0 commit comments

Comments
 (0)