Skip to content

Commit 857dc38

Browse files
committed
[FEAT] Hit 인기수
1 parent 3d1af39 commit 857dc38

File tree

4 files changed

+23
-64
lines changed

4 files changed

+23
-64
lines changed

src/main/java/com/sparta/hanghaeboard/domain/post/controller/PostController.java

+7-52
Original file line numberDiff line numberDiff line change
@@ -82,56 +82,7 @@ public ResponseEntity<?> getPostList() {
8282
return ResponseEntity.ok().body(ResponseDto.success("전체 게시글 조회 성공", postList));
8383
}
8484

85-
// 페이징 처리 + 검색 기능 : 은미
86-
// @GetMapping ("/posts/search")
87-
// public String searchPost (Model model,
88-
// @PageableDefault(page = 0, size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
89-
// String title) { // model : 데이터를 받아서 우리가 보는 페이지로 넘겨줄때 사용
90-
//
91-
// Page<Post> list = null;
92-
//
93-
// if (title == null) {
94-
// list = postService.searchPost(pageable);
95-
// } else {
96-
// list = postService.rightSearchPost(title, pageable);
97-
//
98-
// }
99-
//
100-
// int nowPage = list.getPageable().getPageNumber() +1;
101-
// int startPage = Math.max(nowPage -4, 1);
102-
// int endPage = Math.min(nowPage +5, list.getTotalPages());
103-
//
104-
// model.addAttribute("search", list);// search라는 이름으로 보낸다, 뒤에 있는 것을
105-
// model.addAttribute("nowPage",nowPage);
106-
// model.addAttribute("startPage", startPage);
107-
// model.addAttribute("endPage", endPage);
108-
//
109-
// return "searchPost";
110-
// }
111-
112-
113-
// // 성공함. 그러나 더 build up 해보기
114-
// @GetMapping("/posts/search")
115-
// public ResponseEntity<?> searchPost(
116-
//// @RequestParam(defaultValue = "0") int page,
117-
//// @RequestParam(defaultValue = "10") int size,
118-
// @RequestParam(required = false) String title) {
119-
//
120-
// int page = 0;
121-
// int size = 10;
122-
//
123-
// Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "id"));
124-
// Page<Post> postsPage;
125-
//
126-
// if (title == null || title.isEmpty()) {
127-
// postsPage = postService.searchPost(pageable);
128-
// } else {
129-
// postsPage = postService.rightSearchPost(title, pageable);
130-
// }
131-
//
132-
// return ResponseEntity.ok(postsPage);
133-
// }
134-
85+
// 검색
13586
@GetMapping ("/posts/search")
13687
public ResponseEntity<?> searchPost (
13788
@RequestParam (value = "title", required = false) String title,
@@ -176,13 +127,17 @@ public ResponseEntity<?> searchPost (
176127
return ResponseEntity.ok().body(ResponseDto.success("검색 성공", responseBody));
177128
}
178129

179-
180-
181130
@GetMapping("/posts/category/{category}")
182131
public ResponseEntity<?> getPostByCategoryList(@PathVariable String category) {
183132
List<GetPostListResponseDto> postList = postService.getPostByCategoryList(category);
184133
return ResponseEntity.ok().body(ResponseDto.success("전체 게시글 조회 성공", postList));
185134
}
186135

136+
// hit
137+
@GetMapping("/posts/read/{id}")
138+
public String readHit (@PathVariable Long id) {
139+
postService.updateHit(id); // views++
140+
return "success";
141+
}
187142

188143
}

src/main/java/com/sparta/hanghaeboard/domain/post/entity/Post.java

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public class Post extends Timestamped {
3030

3131
private String category;
3232

33+
@Column(columnDefinition = "integer default 0", nullable = false)
34+
private int hit;
35+
3336
// 연결 필요
3437
@ManyToOne
3538
@JoinColumn(name = "user_id")

src/main/java/com/sparta/hanghaeboard/domain/post/repository/PostRepository.java

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.springframework.data.domain.Page;
66
import org.springframework.data.domain.Pageable;
77
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Modifying;
9+
import org.springframework.data.jpa.repository.Query;
810

911
import java.util.List;
1012
import java.util.Optional;
@@ -16,4 +18,8 @@ public interface PostRepository extends JpaRepository<Post, Long> {
1618
Page<Post> findByTitleContaining (String title, Pageable pageable);
1719

1820
Optional<List<Post>> findAllByCategory(String category);
21+
22+
@Modifying
23+
@Query("update Post p set p.hit = p.hit + 1 where p.id = :id")
24+
int updateHit(Long id);
1925
}

src/main/java/com/sparta/hanghaeboard/domain/post/service/PostService.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,7 @@ public List<GetPostListResponseDto> getPostList() {
154154
return postRepository.findAll().stream().map(GetPostListResponseDto::new).toList();
155155
}
156156

157-
// 페이징 처리 + 검색 기능 : 은미
158-
// public Page<Post> searchPost(Pageable pageable) {
159-
//
160-
// return postRepository.findAll(pageable);
161-
// }
162-
//
163-
// public Page<Post> rightSearchPost(String title, Pageable pageable) {
164-
// return postRepository.findByTitleContaining(title,pageable);
165-
// }
166-
167-
168-
//buildup 해보기
157+
// 검색
169158
public Page<GetPostListResponseDto> searchPost(Pageable pageable) {
170159
Page<Post> postPage = postRepository.findAll(pageable);
171160
return postPage.map(GetPostListResponseDto::new);
@@ -183,4 +172,10 @@ public List<GetPostListResponseDto> getPostByCategoryList(String category) {
183172
new CustomException(ErrorCode.NOT_EXIST_POST));
184173
return postByCategoryList.stream().map(GetPostListResponseDto::new).toList();
185174
}
175+
176+
// hit 수
177+
@Transactional
178+
public int updateHit (Long id) {
179+
return postRepository.updateHit(id);
180+
}
186181
}

0 commit comments

Comments
 (0)