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.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;
@ -26,6 +25,7 @@ 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,16 +54,21 @@ public class ImageController {
}
@GetMapping
public ResponseEntity<SliceView<ImageView>> getOwnedImages(
public ResponseEntity<Object> 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)
);
final int count = this.imageService.countOwnedImages(principal);
return ResponseEntity.ok(this.sliceViewService.getSliceView(imageViews, count));
return ResponseEntity.ok(this.sliceViewService.getSliceView(imageViews));
}
}
@GetMapping("/{username}/{filename}")

View File

@ -88,15 +88,13 @@ public class RecipesController {
final Slice<RecipeInfoView> publicRecipeInfoViews = publicRecipes.map(
recipe -> this.recipeToInfoViewConverter.convert(recipe, null)
);
final int count = this.recipeService.countPublicRecipes();
return ResponseEntity.ok(this.sliceViewService.getSliceView(publicRecipeInfoViews, count));
return ResponseEntity.ok(this.sliceViewService.getSliceView(publicRecipeInfoViews));
} else {
final Slice<Recipe> recipes = this.recipeService.getViewableBy(pageable, user);
final Slice<RecipeInfoView> recipeInfoViews = recipes.map(
recipe -> this.recipeToInfoViewConverter.convert(recipe, user)
);
final int count = this.recipeService.countViewableBy(user);
return ResponseEntity.ok(this.sliceViewService.getSliceView(recipeInfoViews, count));
return ResponseEntity.ok(this.sliceViewService.getSliceView(recipeInfoViews));
}
}
@ -166,8 +164,7 @@ public class RecipesController {
pageable,
principal
);
final int count = this.recipeCommentService.countComments(username, slug);
return ResponseEntity.ok(this.sliceViewService.getSliceView(slice, count));
return ResponseEntity.ok(this.sliceViewService.getSliceView(slice));
}
@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, Integer count) {
public record SliceView<T>(List<T> content, SliceViewMeta slice) {
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, int count) {
public <T> SliceView<T> getSliceView(Slice<T> slice) {
final SliceView.SliceViewMeta meta = new SliceView.SliceViewMeta(
slice.getSize(),
slice.getNumber(),
slice.hasNext()
);
return new SliceView<>(slice.getContent(), meta, count);
return new SliceView<>(slice.getContent(), meta);
}
}