Skip to content

Commit

Permalink
feat: 조회 수 기능 구현(#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
acceptor-gyu committed Jun 14, 2023
1 parent f09cc02 commit b2be4ea
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 45 deletions.
2 changes: 0 additions & 2 deletions be/src/main/java/com/secondhand/post/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ public ResponseEntity<CustomResponse<PostDetailPageDto>> getPostDetail(@PathVari

LoggedInUser loggedInUser = jwtUtil.extractedUserFromToken(token);



return ResponseEntity
.ok()
.body(new CustomResponse(
Expand Down
9 changes: 7 additions & 2 deletions be/src/main/java/com/secondhand/post/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,21 @@ public Page<PostMetaDto> findInterestPosts(Pageable pageable, LoggedInUser logge
return interestRepository.findMyInterestsPosts(pageable, loggedInUser.getId());
}

@Transactional
public PostDetailPageDto findPostDetailPage(long postId, LoggedInUser loggedInUser) {

PostDetailPageDto postDetailPage = postMetaRepository.findPostDetailPage(postId);

PostMeta postMeta = postMetaRepository.findById(postId).orElseThrow();
PostDetail postDetail = postDetailRepository.findById(postId).orElseThrow();
User user = userRepository.findById(loggedInUser.getId()).orElseThrow();

postMeta.updateViewCount();
PostDetailPageDto postDetailPage = new PostDetailPageDto(postMeta);

if (loggedInUser.getId() == user.getId()) {
postDetailPage.setSeller(true);
}

postDetailPage.setContent(postDetail.getContent());
postDetailPage.setPhotoUrls(postPhotoRepository.findAllPhotoUrlsByPostMetaId(postId));

return postDetailPage;
Expand Down
25 changes: 12 additions & 13 deletions be/src/main/java/com/secondhand/post/dto/PostDetailPageDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.secondhand.post.dto;

import com.querydsl.core.annotations.QueryProjection;
import com.secondhand.post.entity.PostMeta;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down Expand Up @@ -28,19 +28,18 @@ public class PostDetailPageDto {
private boolean isSeller;
private List<String> photoUrls;

@QueryProjection
public PostDetailPageDto(long id, long sellerId, String sellerName, String title, String category, LocalDateTime postedAt, String content, long viewCount, long price, String postState) {
this.id = id;
this.sellerId = sellerId;
this.sellerName = sellerName;
this.title = title;
this.category = category;
this.postedAt = postedAt;
this.content = content;

public PostDetailPageDto(PostMeta postMeta) {
this.id = postMeta.getId();
this.sellerId = postMeta.getSeller().getId();
this.sellerName = postMeta.getSeller().getLoginId();
this.title = postMeta.getTitle();
this.category = postMeta.findPostCategoryName();
this.postedAt = postMeta.getPostedAt();
this.chatCount = 0;
this.interestCount = 0;
this.viewCount = viewCount;
this.price = price;
this.postState = postState;
this.viewCount = postMeta.getViewCount();
this.price = postMeta.getPrice();
this.postState = postMeta.findPostMetaState();
}
}
2 changes: 2 additions & 0 deletions be/src/main/java/com/secondhand/post/entity/PostDetail.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.secondhand.post.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.Id;

@Getter
@Entity
@NoArgsConstructor
@AllArgsConstructor
Expand Down
12 changes: 12 additions & 0 deletions be/src/main/java/com/secondhand/post/entity/PostMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@ public void deletePost() {
public void updateBadge(Badge badge) {
this.badge = badge;
}

public void updateViewCount() {
this.viewCount++;
}

public String findPostMetaState() {
return this.badge.getState();
}

public String findPostCategoryName() {
return this.category.getName();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.secondhand.post.repository;

import com.secondhand.post.dto.PostDetailPageDto;
import com.secondhand.post.dto.PostMetaDto;
import com.secondhand.post.dto.SearchCondition;
import org.springframework.data.domain.Page;
Expand All @@ -9,6 +8,4 @@
public interface PostMetaRepositoryCustom {

Page<PostMetaDto> findMainPage(Pageable pageable, SearchCondition searchCondition);

PostDetailPageDto findPostDetailPage(long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.secondhand.post.dto.*;
import com.secondhand.post.dto.PostMetaDto;
import com.secondhand.post.dto.QPostMetaDto;
import com.secondhand.post.dto.SearchCondition;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import java.util.List;

import static com.secondhand.post.entity.QPostDetail.postDetail;
import static com.secondhand.post.entity.QPostMeta.postMeta;

public class PostMetaRepositoryImpl implements PostMetaRepositoryCustom {
Expand Down Expand Up @@ -48,29 +49,6 @@ public Page<PostMetaDto> findMainPage(Pageable pageable, SearchCondition searchC
return new PageImpl<>(content, pageable, total);
}

@Override
public PostDetailPageDto findPostDetailPage(long postId) {

return queryFactory
.select(new QPostDetailPageDto(
postMeta.id,
postMeta.seller.id,
postMeta.seller.loginId,
postMeta.title,
postMeta.category.name,
postMeta.postedAt,
postDetail.content,
postMeta.viewCount,
postMeta.price,
postMeta.badge.state
))
.from(postMeta)
.join(postDetail)
.on(postMeta.id.eq(postDetail.id))
.where(postMeta.id.eq(postId))
.fetchOne();
}

private BooleanExpression categoryEq(Integer category) {

if (category == null) {
Expand Down

0 comments on commit b2be4ea

Please sign in to comment.