Skip to content

Commit 1258ffe

Browse files
author
Oyvind Timian Dokk Husveg
committed
UpdatePost works as expected, except missing user object
1 parent fae0760 commit 1258ffe

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

exercise.tests/IntegrationTests/PostTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public async Task GetAllPostsStatusAndMessage()
199199

200200
var data = message["data"];
201201
Assert.That(data, Is.Not.Null);
202-
Assert.That(data!.AsArray().Count, Is.GreaterThanOrEqualTo(0));
202+
Assert.That(data.AsArray(), Has.Count.GreaterThanOrEqualTo(0));
203203

204204
foreach (var post in data.AsArray())
205205
{
@@ -256,7 +256,7 @@ public async Task UpdatePostById(int postId, string newContent, HttpStatusCode e
256256
var requestBody = new StringContent(json, Encoding.UTF8, "application/json");
257257

258258
// Act
259-
var response = await _client.PutAsync($"/posts/{postId}", requestBody);
259+
var response = await _client.PatchAsync($"/posts/{postId}", requestBody);
260260
var contentString = await response.Content.ReadAsStringAsync();
261261

262262
JsonNode? message = null;

exercise.wwwapi/Endpoints/PostEndpoints.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void ConfigurePostEndpoints(this WebApplication app)
1616
var posts = app.MapGroup("posts");
1717
posts.MapPost("/", CreatePost).WithSummary("Create post");
1818
posts.MapGet("/", GetAllPosts).WithSummary("Get all posts");
19-
posts.MapPatch("/{id}", GetAllPosts).WithSummary("Update a certain post");
19+
posts.MapPatch("/{id}", UpdatePost).WithSummary("Update a certain post");
2020
posts.MapDelete("/{id}", GetAllPosts).WithSummary("Remove a certain post");
2121
}
2222
[ProducesResponseType(StatusCodes.Status200OK)]
@@ -65,21 +65,28 @@ public static IResult GetAllPosts(IRepository<Post> service, IMapper mapper)
6565
}
6666

6767
[ProducesResponseType(StatusCodes.Status200OK)]
68+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
6869
public static IResult UpdatePost(IRepository<Post> service, IMapper mapper, int id, UpdatePostDTO request)
6970
{
70-
if (request.GetType().GetProperties().Length > 0 && request.GetType().GetProperties().All((p) => p.GetValue(request) == null)) return TypedResults.NoContent();
71-
71+
if (string.IsNullOrWhiteSpace(request.Content)) return TypedResults.BadRequest(new ResponseDTO<object>{
72+
Message = "Content cannot be empty"
73+
});
74+
75+
// TODO: Add new getbyid that uses includes
7276
var post = service.GetById(id);
7377

74-
if (post == null) return TypedResults.NotFound(new ResponseDTO<Object> { Message = "Missing post" });
78+
if (post == null) return TypedResults.NotFound(new ResponseDTO<Object> { Message = "Post not found" });
7579

76-
if (string.IsNullOrEmpty(request.Content)) post.Content = request.Content;
80+
post.Content = request.Content;
7781
post.UpdatedAt = DateTime.UtcNow;
7882

7983
service.Update(post);
8084
service.Save();
8185

82-
return TypedResults.Ok(request);
86+
PostDTO postDTO = mapper.Map<PostDTO>(post);
87+
88+
89+
return TypedResults.Ok(new ResponseDTO<PostDTO> { Message = "Success", Data = postDTO });
8390
}
8491
}
8592
}

0 commit comments

Comments
 (0)