Added ImageView to FullRecipeView and RecipeInfoView.
This commit is contained in:
parent
ff0f22b9e4
commit
67b5452d82
@ -6,7 +6,6 @@ import app.mealsmadeeasy.api.image.spec.ImageUpdateInfoSpec;
|
||||
import app.mealsmadeeasy.api.image.view.ImageView;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
import app.mealsmadeeasy.api.user.UserService;
|
||||
import app.mealsmadeeasy.api.user.view.UserInfoView;
|
||||
import app.mealsmadeeasy.api.util.AccessDeniedView;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -20,7 +19,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -28,33 +26,6 @@ import java.util.stream.Collectors;
|
||||
@RequestMapping("/images")
|
||||
public class ImageController {
|
||||
|
||||
private static ImageView getView(Image image, User owner) {
|
||||
final ImageView imageView = new ImageView();
|
||||
imageView.setCreated(image.getCreated());
|
||||
imageView.setModified(image.getModified());
|
||||
imageView.setFilename(image.getUserFilename());
|
||||
imageView.setMimeType(image.getMimeType());
|
||||
imageView.setAlt(image.getAlt());
|
||||
imageView.setCaption(image.getCaption());
|
||||
imageView.setIsPublic(image.isPublic());
|
||||
|
||||
final UserInfoView userInfoView = new UserInfoView();
|
||||
userInfoView.setId(owner.getId());
|
||||
userInfoView.setUsername(owner.getUsername());
|
||||
imageView.setOwner(userInfoView);
|
||||
|
||||
final Set<UserInfoView> viewers = new HashSet<>();
|
||||
for (final User viewer : image.getViewers()) {
|
||||
final UserInfoView viewerView = new UserInfoView();
|
||||
viewerView.setId(viewer.getId());
|
||||
viewerView.setUsername(viewer.getUsername());
|
||||
viewers.add(viewerView);
|
||||
}
|
||||
imageView.setViewers(viewers);
|
||||
|
||||
return imageView;
|
||||
}
|
||||
|
||||
private final ImageService imageService;
|
||||
private final UserService userService;
|
||||
|
||||
@ -140,7 +111,7 @@ public class ImageController {
|
||||
image.getSize(),
|
||||
createSpec
|
||||
);
|
||||
return ResponseEntity.status(201).body(getView(saved, principal));
|
||||
return ResponseEntity.status(201).body(ImageUtil.toImageView(saved));
|
||||
}
|
||||
|
||||
@PostMapping("/{username}/{filename}")
|
||||
@ -156,7 +127,7 @@ public class ImageController {
|
||||
final User owner = this.userService.getUser(username);
|
||||
final Image image = this.imageService.getByOwnerAndFilename(owner, filename, principal);
|
||||
final Image updated = this.imageService.update(image, principal, this.getImageUpdateSpec(body));
|
||||
return ResponseEntity.ok(getView(updated, owner));
|
||||
return ResponseEntity.ok(ImageUtil.toImageView(updated));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{username}/{filename}")
|
||||
|
42
src/main/java/app/mealsmadeeasy/api/image/ImageUtil.java
Normal file
42
src/main/java/app/mealsmadeeasy/api/image/ImageUtil.java
Normal file
@ -0,0 +1,42 @@
|
||||
package app.mealsmadeeasy.api.image;
|
||||
|
||||
import app.mealsmadeeasy.api.image.view.ImageView;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
import app.mealsmadeeasy.api.user.view.UserInfoView;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public final class ImageUtil {
|
||||
|
||||
public static ImageView toImageView(Image image) {
|
||||
final ImageView imageView = new ImageView();
|
||||
imageView.setCreated(image.getCreated());
|
||||
imageView.setModified(image.getModified());
|
||||
imageView.setFilename(image.getUserFilename());
|
||||
imageView.setMimeType(image.getMimeType());
|
||||
imageView.setAlt(image.getAlt());
|
||||
imageView.setCaption(image.getCaption());
|
||||
imageView.setIsPublic(image.isPublic());
|
||||
|
||||
final User owner = image.getOwner();
|
||||
final UserInfoView userInfoView = new UserInfoView();
|
||||
userInfoView.setId(owner.getId());
|
||||
userInfoView.setUsername(owner.getUsername());
|
||||
imageView.setOwner(userInfoView);
|
||||
|
||||
final Set<UserInfoView> viewers = new HashSet<>();
|
||||
for (final User viewer : image.getViewers()) {
|
||||
final UserInfoView viewerView = new UserInfoView();
|
||||
viewerView.setId(viewer.getId());
|
||||
viewerView.setUsername(viewer.getUsername());
|
||||
viewers.add(viewerView);
|
||||
}
|
||||
imageView.setViewers(viewers);
|
||||
|
||||
return imageView;
|
||||
}
|
||||
|
||||
private ImageUtil() {}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package app.mealsmadeeasy.api.recipe;
|
||||
|
||||
import app.mealsmadeeasy.api.image.ImageUtil;
|
||||
import app.mealsmadeeasy.api.image.S3ImageEntity;
|
||||
import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec;
|
||||
import app.mealsmadeeasy.api.recipe.spec.RecipeUpdateSpec;
|
||||
@ -109,6 +110,7 @@ public class RecipeServiceImpl implements RecipeService {
|
||||
view.setOwnerUsername(recipe.getOwner().getUsername());
|
||||
view.setStarCount(this.getStarCount(recipe));
|
||||
view.setViewerCount(this.getViewerCount(recipe.getId()));
|
||||
view.setMainImage(ImageUtil.toImageView(recipe.getMainImage()));
|
||||
return view;
|
||||
}
|
||||
|
||||
@ -127,6 +129,7 @@ public class RecipeServiceImpl implements RecipeService {
|
||||
view.setOwnerUsername(entity.getOwner().getUsername());
|
||||
view.setPublic(entity.isPublic());
|
||||
view.setStarCount(this.getStarCount(entity));
|
||||
view.setMainImage(ImageUtil.toImageView(entity.getMainImage()));
|
||||
return view;
|
||||
});
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package app.mealsmadeeasy.api.recipe.view;
|
||||
|
||||
import app.mealsmadeeasy.api.image.view.ImageView;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@ -15,6 +16,7 @@ public class FullRecipeView {
|
||||
private String ownerUsername;
|
||||
private int starCount;
|
||||
private int viewerCount;
|
||||
private ImageView mainImage;
|
||||
|
||||
public long getId() {
|
||||
return this.id;
|
||||
@ -88,4 +90,12 @@ public class FullRecipeView {
|
||||
this.viewerCount = viewerCount;
|
||||
}
|
||||
|
||||
public ImageView getMainImage() {
|
||||
return this.mainImage;
|
||||
}
|
||||
|
||||
public void setMainImage(ImageView mainImage) {
|
||||
this.mainImage = mainImage;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package app.mealsmadeeasy.api.recipe.view;
|
||||
|
||||
import app.mealsmadeeasy.api.image.view.ImageView;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public final class RecipeInfoView {
|
||||
@ -11,6 +13,7 @@ public final class RecipeInfoView {
|
||||
private String ownerUsername;
|
||||
private boolean isPublic;
|
||||
private int starCount;
|
||||
private ImageView mainImage;
|
||||
|
||||
public long getId() {
|
||||
return this.id;
|
||||
@ -68,4 +71,12 @@ public final class RecipeInfoView {
|
||||
this.starCount = starCount;
|
||||
}
|
||||
|
||||
public ImageView getMainImage() {
|
||||
return this.mainImage;
|
||||
}
|
||||
|
||||
public void setMainImage(ImageView mainImage) {
|
||||
this.mainImage = mainImage;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user