Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/docs/asciidoc/post-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ include::{snippetsDir}/createProductLike/2/http-response.adoc[]
==== Request
include::{snippetsDir}/loadPostProducts/1/http-request.adoc[]

==== Request Query Parameters
include::{snippetsDir}/loadPostProducts/1/query-parameters.adoc[]

==== Request Body Parameters
include::{snippetsDir}/loadPostProducts/1/request-fields.adoc[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ftm.server.application.vo.post.PostDetailVo;
import com.ftm.server.domain.enums.PostHashtag;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.Getter;
Expand Down Expand Up @@ -33,7 +34,12 @@ private LoadPostDetailResponse(PostDetailVo postDetailVo) {
this.postId = postDetailVo.getPostId();
this.title = postDetailVo.getTitle();
this.content = postDetailVo.getContent();
this.hashtags = Arrays.stream(postDetailVo.getHashtags()).map(PostHashtag::getTag).toList();
this.hashtags =
postDetailVo.getHashtags() == null || postDetailVo.getHashtags().length == 0
? new ArrayList<>()
: Arrays.stream(postDetailVo.getHashtags())
.map(PostHashtag::getTag)
.toList();
this.viewCount = postDetailVo.getViewCount();
this.likeCount = postDetailVo.getLikeCount();
this.createdAt = postDetailVo.getCreatedAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ftm.server.application.vo.post.PostProductDetailVo;
import com.ftm.server.domain.enums.ProductHashtag;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.Getter;
Expand All @@ -12,19 +13,26 @@ public class PostProductResponse {
private final Long postProductId;
private final String name;
private final String brand;
private final Long recommendedCount;
private final List<String> hashtags;
private final PostProductImageResponse postProductImage;

private PostProductResponse(PostProductDetailVo postProductDetailVo) {
this.postProductId = postProductDetailVo.getPostProductId();
this.name = postProductDetailVo.getName();
this.brand = postProductDetailVo.getBrand();
this.recommendedCount = postProductDetailVo.getRecommendedCount();
this.hashtags =
Arrays.stream(postProductDetailVo.getHashtags())
.map(ProductHashtag::getTag)
.toList();
postProductDetailVo.getHashtags() == null
|| postProductDetailVo.getHashtags().length == 0
? new ArrayList<>()
: Arrays.stream(postProductDetailVo.getHashtags())
.map(ProductHashtag::getTag)
.toList();
this.postProductImage =
PostProductImageResponse.from(postProductDetailVo.getPostProductImage());
postProductDetailVo.getPostProductImage() == null
? null
: PostProductImageResponse.from(postProductDetailVo.getPostProductImage());
}

public static PostProductResponse from(PostProductDetailVo postProductDetailVo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,19 @@ public void updatePostProducts(List<PostProduct> postProducts) {
}
}

@Override
public void updatePostProduct(PostProduct postProduct) {
PostProductJpaEntity postProductJpaEntity =
postProductRepository
.findById(postProduct.getId())
.orElseThrow(
() ->
new CustomException(
ErrorResponseCode.POST_PRODUCT_NOT_FOUND));

postProductJpaEntity.updatePostProductForDomainEntity(postProduct);
}

@Override
public void updatePostProductImages(List<PostProductImage> postProductImages) {
List<Long> ids = postProductImages.stream().map(PostProductImage::getId).toList();
Expand Down Expand Up @@ -444,7 +457,7 @@ public List<LoadProductAndUserLikeVo> findProductLikeByUser(

@Override
public void deletePostLike(Long postLikeId) {
productLikeRepository.deleteById(postLikeId);
postLikeRepository.deleteById(postLikeId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface UpdatePostProductPort {

void updatePostProducts(List<PostProduct> postProducts);

void updatePostProduct(PostProduct postProducts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public LoadProductsByHashTagVoWrapper execute(
} else {
loadProductAndUserLikeVos =
loadProductLikePort.findProductLikeByUser(userId, productIds);
System.out.println("!!!!!!!!!!!!!!\n\n\\n");
System.out.println(loadProductAndUserLikeVos.size());
System.out.println(loadProductAndUserLikeVos.get(0));
}
Map<Long, LoadProductAndUserLikeVo> productLikeMap =
loadProductAndUserLikeVos.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.ftm.server.application.service.post.product;

import com.ftm.server.application.port.in.post.CreateProductLikeUseCase;
import com.ftm.server.application.port.out.persistence.post.DeleteProductLikePort;
import com.ftm.server.application.port.out.persistence.post.LoadProductLikePort;
import com.ftm.server.application.port.out.persistence.post.SaveProductLikePort;
import com.ftm.server.application.port.out.persistence.post.*;
import com.ftm.server.application.query.FindByIdsQuery;
import com.ftm.server.common.exception.CustomException;
import com.ftm.server.common.response.enums.ErrorResponseCode;
import com.ftm.server.domain.entity.PostProduct;
import com.ftm.server.domain.entity.ProductLike;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -15,12 +18,24 @@
public class CreateProductLikeService implements CreateProductLikeUseCase {

private final LoadProductLikePort loadProductLikePort;
private final LoadPostProductPort loadPostProductPort;
private final SaveProductLikePort saveProductLikePort;
private final DeleteProductLikePort deleteProductLikePort;
private final UpdatePostProductPort updatePostProductPort;

@Transactional
public Boolean execute(Long userId, Long productId) {

PostProduct postProduct =
loadPostProductPort
.loadPostProductsByIds(FindByIdsQuery.from(List.of(productId)))
.stream()
.findFirst()
.orElseThrow(
() ->
new CustomException(
ErrorResponseCode.POST_PRODUCT_NOT_FOUND));

// 이미 등록된 좋아요 있는지 확인
Optional<Long> optionalProductLikeId =
loadProductLikePort.findOneByUserAndProduct(userId, productId);
Expand All @@ -29,12 +44,18 @@ public Boolean execute(Long userId, Long productId) {
if (optionalProductLikeId.isEmpty()) {
ProductLike productLike = ProductLike.create(productId, userId);
saveProductLikePort.saveProductLike(productLike);

postProduct.plusRecommendedCountByOne();
updatePostProductPort.updatePostProduct(postProduct);
return true;
}

// 있으면 삭제
else {
deleteProductLikePort.deleteProductLike(optionalProductLikeId.get());

postProduct.minusRecommendedCountByOne();
updatePostProductPort.updatePostProduct(postProduct);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class PostProductDetailVo {
private final LocalDateTime createdAt;
private final LocalDateTime updatedAt;
private final PostProductImage postProductImage;
private final Long recommendedCount;

private PostProductDetailVo(PostProduct postProduct, PostProductImage postProductImage) {
this.postProductId = postProduct.getId();
Expand All @@ -27,6 +28,7 @@ private PostProductDetailVo(PostProduct postProduct, PostProductImage postProduc
this.createdAt = postProduct.getCreatedAt();
this.updatedAt = postProduct.getUpdatedAt();
this.postProductImage = postProductImage;
this.recommendedCount = postProduct.getRecommendedCount();
}

public static List<PostProductDetailVo> listFrom(
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/ftm/server/domain/entity/PostProduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ public void validatePost(Long postId) {
throw new CustomException(ErrorResponseCode.UNAUTHORIZED_POST_PRODUCT_ACCESS);
}
}

public void plusRecommendedCountByOne() {
this.recommendedCount += 1;
}

public void minusRecommendedCountByOne() {
this.recommendedCount -= 1;
}
}
3 changes: 3 additions & 0 deletions src/test/java/com/ftm/server/post/LoadPostDetailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public class LoadPostDetailTest extends BaseTest {
fieldWithPath("data.postProducts[].brand")
.type(STRING)
.description("게시글 상품 브랜드"),
fieldWithPath("data.postProducts[].recommendedCount")
.type(NUMBER)
.description("게시글 상품 추천수"),
fieldWithPath("data.postProducts[].hashtags[]")
.type(ARRAY)
.description("게시글 상품 해시태그 목록"),
Expand Down
Loading