MME-8 Add ability to get owned images count from /images endpoint via request param.

This commit is contained in:
Jesse Brault 2026-02-07 21:29:29 -06:00
parent ba40c48719
commit c8e660b596
4 changed files with 19 additions and 17 deletions

View File

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

View File

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

View File

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

View File

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