Skip to content

Commit

Permalink
Merge pull request #121 from IxxP-Girls/modify
Browse files Browse the repository at this point in the history
#7 #13 #25 Fix: 게시글 개수 한번만 가져오게 수정
  • Loading branch information
diddnwjd committed Mar 12, 2024
2 parents 6bef0c4 + 3a7e161 commit 7baa14e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/ixxp/culpop/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ResponseEntity<StatusResponse> createPost(@AuthenticationPrincipal UserDe

// 게시글 전체 조회
@GetMapping()
public ResponseEntity<List<PostResponse>> getPost(@RequestParam(name = "category", defaultValue = "전체") String category
public ResponseEntity<PostResponse> getPost(@RequestParam(name = "category", defaultValue = "전체") String category
, @RequestParam(name = "page", defaultValue = "1") int page) {
return ResponseEntity.ok(postService.getPost(category, page));
}
Expand Down Expand Up @@ -85,9 +85,9 @@ public ResponseEntity<StatusResponse> unlikePost(@AuthenticationPrincipal UserDe

// 게시글 검색
@GetMapping("/search")
public ResponseEntity<List<PostResponse>> getSearchPost(@RequestParam("word") String word,
public ResponseEntity<PostResponse> getSearchPost(@RequestParam("word") String word,
@RequestParam(name = "page", defaultValue = "1") int page) {
List<PostResponse> postResponses = postService.getSearchPost(word, page);
PostResponse postResponses = postService.getSearchPost(word, page);
return ResponseEntity.ok(postResponses);
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/ixxp/culpop/dto/post/PostList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ixxp.culpop.dto.post;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
public class PostList {
private int postId;
private String username;
private String title;
private String cateName;
private int viewCount;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDateTime createdAt;

public PostList(int postId, String username, String title, String cateName, int viewCount, LocalDateTime createdAt) {
this.postId = postId;
this.username = username;
this.title = title;
this.cateName = cateName;
this.viewCount = viewCount;
this.createdAt = createdAt;
}
}
24 changes: 6 additions & 18 deletions src/main/java/com/ixxp/culpop/dto/post/PostResponse.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package com.ixxp.culpop.dto.post;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Getter
@NoArgsConstructor
public class PostResponse {
private int postId;
private String username;
private String title;
private String cateName;
private int viewCount;
private int postCount;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDateTime createdAt;
private List<PostList> data;
private int total;

public PostResponse(int postId, String username, String title, String cateName, int viewCount, int postCount, LocalDateTime createdAt) {
this.postId = postId;
this.username = username;
this.title = title;
this.cateName = cateName;
this.viewCount = viewCount;
this.postCount = postCount;
this.createdAt = createdAt;
public PostResponse(List<PostList> data, int total) {
this.data = data;
this.total = total;
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/ixxp/culpop/dto/user/ProfileResponse.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ixxp.culpop.dto.user;

import com.ixxp.culpop.dto.post.PostList;
import com.ixxp.culpop.dto.post.PostResponse;
import com.ixxp.culpop.entity.User;
import lombok.Getter;
Expand All @@ -13,9 +14,9 @@ public class ProfileResponse {
private int userId;
private String username;
private String email;
private List<PostResponse> postList;
private PostResponse postList;

public ProfileResponse(User user, List<PostResponse> postList) {
public ProfileResponse(User user, PostResponse postList) {
this.userId = user.getId();
this.username = user.getUsername();
this.email = user.getEmail();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/ixxp/culpop/mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public interface PostMapper {
Post selectPostDetail(int postId);
List<Post> selectSearchPost(String word, int offset);
List<Post> selectPostByUserId(int userId, int offset);
int selectCategoryPostCount(String category);
int selectWordPostCount(String word);
int selectUserPostCount(int userId);
int selectPostViewCount(int postId);
void updatePostViewCount(int postId);
void updatePost(Post post);
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/com/ixxp/culpop/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.ixxp.culpop.dto.post.PostDetailResponse;
import com.ixxp.culpop.dto.post.PostRequest;
import com.ixxp.culpop.dto.post.PostList;
import com.ixxp.culpop.dto.post.PostResponse;
import com.ixxp.culpop.dto.user.ProfileResponse;
import com.ixxp.culpop.entity.Category;
import com.ixxp.culpop.entity.Post;
import com.ixxp.culpop.entity.PostLike;
Expand Down Expand Up @@ -46,12 +48,15 @@ public void createPost(User user, PostRequest postRequest) {
}

// 게시글 전체 조회
public List<PostResponse> getPost(String category, int page) {
public PostResponse getPost(String category, int page) {
int offset = (page - 1) * 10;
List<Post> posts = postMapper.selectPost(category, offset);
return posts.stream().map(post ->
new PostResponse(post.getId(), post.getUser().getUsername(), post.getTitle(), post.getCategory().getCateName(), postMapper.selectPostViewCount(post.getId()), posts.size(), post.getCreatedAt())
List<PostList> postList = posts.stream().map(post ->
new PostList(post.getId(), post.getUser().getUsername(), post.getTitle(), post.getCategory().getCateName(), postMapper.selectPostViewCount(post.getId()), post.getCreatedAt())
).collect(Collectors.toList());

int total = postMapper.selectCategoryPostCount(category);
return new PostResponse(postList, total);
}

// 게시글 개별 조회
Expand Down Expand Up @@ -145,11 +150,13 @@ public void unlikePost(User user, int postId) {
}

// 게시글 검색
public List<PostResponse> getSearchPost(String word, int page) {
public PostResponse getSearchPost(String word, int page) {
int offset = (page - 1) * 10;
List<Post> posts = postMapper.selectSearchPost(word, offset);
return posts.stream().map(post ->
new PostResponse(post.getId(), post.getUser().getUsername(), post.getTitle(), post.getCategory().getCateName(), postMapper.selectPostViewCount(post.getId()), posts.size(), post.getCreatedAt())
List<PostList> postList = posts.stream().map(post ->
new PostList(post.getId(), post.getUser().getUsername(), post.getTitle(), post.getCategory().getCateName(), postMapper.selectPostViewCount(post.getId()), post.getCreatedAt())
).collect(Collectors.toList());
int total = postMapper.selectWordPostCount(word);
return new PostResponse(postList, total);
}
}
14 changes: 9 additions & 5 deletions src/main/java/com/ixxp/culpop/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ixxp.culpop.service;

import com.ixxp.culpop.dto.popup.PopupResponse;
import com.ixxp.culpop.dto.post.PostList;
import com.ixxp.culpop.dto.post.PostResponse;
import com.ixxp.culpop.dto.user.ProfileResponse;
import com.ixxp.culpop.dto.user.ProfileUpdateRequest;
Expand All @@ -15,7 +16,6 @@
import com.ixxp.culpop.mapper.UserMapper;
import com.ixxp.culpop.util.jwtutil.JwtUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseCookie;
Expand All @@ -25,7 +25,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -94,11 +93,16 @@ public void updateProfile(int userId, ProfileUpdateRequest profileUpdateRequest)
public ProfileResponse getProfile(int userId, int page) {
int offset = (page - 1) * 5;
User user = userMapper.getProfile(userId);

List<Post> posts = postMapper.selectPostByUserId(userId, offset);
List<PostResponse> postList = posts.stream().map(post ->
new PostResponse(post.getId(), post.getUser().getUsername(), post.getTitle(), post.getCategory().getCateName(), postMapper.selectPostViewCount(post.getId()), posts.size(), post.getCreatedAt())
List<PostList> postList = posts.stream().map(post ->
new PostList(post.getId(), post.getUser().getUsername(), post.getTitle(), post.getCategory().getCateName(), postMapper.selectPostViewCount(post.getId()), post.getCreatedAt())
).collect(Collectors.toList());
return new ProfileResponse(user, postList);

int total = postMapper.selectUserPostCount(userId);
PostResponse postResponse = new PostResponse(postList, total);

return new ProfileResponse(user, postResponse);
}

// 프로필 관심 팝업 조회
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/mapper/post-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@
ORDER BY p.createdAt DESC
LIMIT 5 OFFSET #{offset};
</select>
<select id="selectCategoryPostCount" parameterType="string" resultType="int">
SELECT COUNT(*) FROM Post p
LEFT JOIN Category c ON p.categoryId = c.id
<where>
<if test="category != '전체' and category != ''">
cateName = #{category}
</if>
</where>
</select>
<select id="selectWordPostCount" parameterType="string" resultType="int">
SELECT COUNT(*) FROM Post p
WHERE p.title LIKE CONCAT('%', #{word}, '%')
</select>
<select id="selectUserPostCount" parameterType="int" resultType="int">
SELECT COUNT(*) FROM Post WHERE userId = #{userId}
</select>
<select id="selectPostViewCount" parameterType="int" resultType="int">
SELECT viewCount FROM Post WHERE id = #{postId};
</select>
Expand Down

0 comments on commit 7baa14e

Please sign in to comment.