Compare commits

..

No commits in common. "d1df876bb35d1b80216e6f993ec0a61476c81bf1" and "ba40c48719a59fdad2057eb0dace15ad38956522" have entirely different histories.

6 changed files with 20 additions and 19 deletions

View File

@ -139,6 +139,7 @@ public class ImageControllerTests {
.header("Authorization", "Bearer " + this.getAccessToken(owner))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.count").value(1))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content").isNotEmpty())
.andExpect(jsonPath("$.content[0].url").isNotEmpty())

View File

@ -182,6 +182,7 @@ public class RecipesControllerTests {
final Recipe recipe = this.createTestRecipe(owner, true);
this.mockMvc.perform(get("/recipes"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.count").isNotEmpty())
.andExpect(jsonPath("$.slice.number").value(0))
.andExpect(jsonPath("$.slice.size").value(20))
.andExpect(jsonPath("$.content").isArray())
@ -200,6 +201,7 @@ public class RecipesControllerTests {
.header("Authorization", "Bearer " + loginDetails.getAccessToken().getToken())
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.count").isNotEmpty())
.andExpect(jsonPath("$.slice.number").value(0))
.andExpect(jsonPath("$.slice.size").value(20))
.andExpect(jsonPath("$.content").isArray())

View File

@ -5,6 +5,7 @@ import app.mealsmadeeasy.api.image.converter.ImageToViewConverter;
import app.mealsmadeeasy.api.image.converter.ImageUpdateBodyToSpecConverter;
import app.mealsmadeeasy.api.image.spec.ImageCreateSpec;
import app.mealsmadeeasy.api.image.view.ImageView;
import app.mealsmadeeasy.api.sliceview.SliceView;
import app.mealsmadeeasy.api.sliceview.SliceViewService;
import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.UserService;
@ -25,7 +26,6 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@ -54,21 +54,16 @@ public class ImageController {
}
@GetMapping
public ResponseEntity<Object> getOwnedImages(
public ResponseEntity<SliceView<ImageView>> getOwnedImages(
@AuthenticationPrincipal User principal,
@RequestParam(name = "count", defaultValue = "false") boolean doCount,
Pageable pageable
) {
if (doCount) {
final int count = this.imageService.countOwnedImages(principal);
return ResponseEntity.ok(Map.of("count", count));
} else {
final Slice<Image> images = this.imageService.getOwnedImages(principal, pageable);
final Slice<ImageView> imageViews = images.map(image ->
this.imageToViewConverter.convert(image, principal, false)
);
return ResponseEntity.ok(this.sliceViewService.getSliceView(imageViews));
}
final int count = this.imageService.countOwnedImages(principal);
return ResponseEntity.ok(this.sliceViewService.getSliceView(imageViews, count));
}
@GetMapping("/{username}/{filename}")

View File

@ -88,13 +88,15 @@ public class RecipesController {
final Slice<RecipeInfoView> publicRecipeInfoViews = publicRecipes.map(
recipe -> this.recipeToInfoViewConverter.convert(recipe, null)
);
return ResponseEntity.ok(this.sliceViewService.getSliceView(publicRecipeInfoViews));
final int count = this.recipeService.countPublicRecipes();
return ResponseEntity.ok(this.sliceViewService.getSliceView(publicRecipeInfoViews, count));
} else {
final Slice<Recipe> recipes = this.recipeService.getViewableBy(pageable, user);
final Slice<RecipeInfoView> recipeInfoViews = recipes.map(
recipe -> this.recipeToInfoViewConverter.convert(recipe, user)
);
return ResponseEntity.ok(this.sliceViewService.getSliceView(recipeInfoViews));
final int count = this.recipeService.countViewableBy(user);
return ResponseEntity.ok(this.sliceViewService.getSliceView(recipeInfoViews, count));
}
}
@ -164,7 +166,8 @@ public class RecipesController {
pageable,
principal
);
return ResponseEntity.ok(this.sliceViewService.getSliceView(slice));
final int count = this.recipeCommentService.countComments(username, slug);
return ResponseEntity.ok(this.sliceViewService.getSliceView(slice, count));
}
@PostMapping("/{username}/{slug}/comments")

View File

@ -2,7 +2,7 @@ package app.mealsmadeeasy.api.sliceview;
import java.util.List;
public record SliceView<T>(List<T> content, SliceViewMeta slice) {
public record SliceView<T>(List<T> content, SliceViewMeta slice, Integer count) {
public record SliceViewMeta(Integer size, Integer number, Boolean hasNext) {}

View File

@ -6,13 +6,13 @@ import org.springframework.stereotype.Service;
@Service
public class SliceViewService {
public <T> SliceView<T> getSliceView(Slice<T> slice) {
public <T> SliceView<T> getSliceView(Slice<T> slice, int count) {
final SliceView.SliceViewMeta meta = new SliceView.SliceViewMeta(
slice.getSize(),
slice.getNumber(),
slice.hasNext()
);
return new SliceView<>(slice.getContent(), meta);
return new SliceView<>(slice.getContent(), meta, count);
}
}