Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3/19] 코드 리펙토링 및 Top3Tags 조회 기능 생성 #18

Merged
merged 5 commits into from
Mar 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,120 @@ public class PostApiController {
private final PostsService postsService;
private final AuthService authService;

@GetMapping("/posts")
public ResponseEntity<? super GetPostsAllResponseDto> getAllPost() {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getAllPost();
@GetMapping("/posts/all")
public ResponseEntity<? super GetPostsAllResponseDto> getAllPost(@RequestParam("page")int page) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getAllPost(page);
return response;
}

@PostMapping("/posts")
public ResponseEntity<? super PostsResponseDto> save(@RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) {
Long kakao_uid = authService.getKakaoUserInfo(token).getId();
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}

ResponseEntity<? super PostsResponseDto> response = postsService.savePost(kakao_uid, requestDto);
return response;
}

@GetMapping("/posts/{postId}")
public ResponseEntity<? super GetPostsResponseDto> getPost(@PathVariable Long postId) {
@GetMapping("/posts")
public ResponseEntity<? super GetPostsResponseDto> getPost(@RequestParam("postId") Long postId) {
ResponseEntity<? super GetPostsResponseDto> response = postsService.getPost(postId);
return response;
}

@PatchMapping("/posts/{postId}")
public ResponseEntity<? super PostsResponseDto> patchPost(@PathVariable Long postId, @RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) {
Long kakao_uid = authService.getKakaoUserInfo(token).getId();
@PatchMapping("/posts")
public ResponseEntity<? super PostsResponseDto> patchPost(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}

ResponseEntity<? super PostsResponseDto> response = postsService.patchPost(postId, kakao_uid, requestDto);
return response;
}

@DeleteMapping("/posts/{postId}")
public ResponseEntity<? super PostsResponseDto> deletePost(@PathVariable Long postId, @RequestHeader(value="Authorization") String token) {
Long kakao_uid = authService.getKakaoUserInfo(token).getId();
@DeleteMapping("/posts")
public ResponseEntity<? super PostsResponseDto> deletePost(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super PostsResponseDto> response = postsService.deletePost(postId, kakao_uid);
return response;
}

@GetMapping("/posts/my-post-list")
public ResponseEntity<? super GetPostsAllResponseDto> getMyPost(@RequestHeader(value="Authorization") String token) {
Long kakao_uid = authService.getKakaoUserInfo(token).getId();
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getMyPost(kakao_uid);
public ResponseEntity<? super GetPostsAllResponseDto> getMyPost(@RequestHeader(value="Authorization") String token, @RequestParam("page")int page) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getMyPost(kakao_uid, page);
return response;
}

@GetMapping("/posts/search-list/{searchWord}")
public ResponseEntity<? super GetPostsAllResponseDto> getSearchPost(@PathVariable String searchWord) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getSearchPost(searchWord);
@GetMapping("/posts/search-list")
public ResponseEntity<? super GetPostsAllResponseDto> getSearchPost(@RequestParam("searchWord") String searchWord, @RequestParam("page")int page) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getSearchPost(searchWord, page);
return response;
}

@GetMapping("/posts/category-list")
public ResponseEntity<? super GetPostsAllResponseDto> searchPostsByCategory(@RequestParam("category") List<Long> categories) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getCategorySearchPost(categories);
public ResponseEntity<? super GetPostsAllResponseDto> searchPostsByCategory(@RequestParam("categoryId") List<Long> categories, @RequestParam("page")int page) {
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getCategorySearchPost(categories, page);
return response;
}

// 스크랩
@PutMapping("/posts/{postId}/scrap")
public ResponseEntity<? super PutScrapResponseDto> putScrap(@PathVariable("postId") Long postId, @RequestHeader(value="Authorization") String token) {
Long kakao_uid = authService.getKakaoUserInfo(token).getId();
@PutMapping("/posts/scrap")
public ResponseEntity<? super PutScrapResponseDto> putScrap(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super PutScrapResponseDto> response = postsService.putScrap(postId, kakao_uid);
return response;
}

@GetMapping("/posts/scrap-list")
public ResponseEntity<? super GetPostsAllResponseDto> getAllScrapPost(@RequestHeader(value="Authorization") String token) {
Long kakao_uid = authService.getKakaoUserInfo(token).getId();
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getAllScrapPost(kakao_uid);
public ResponseEntity<? super GetPostsAllResponseDto> getAllScrapPost(@RequestHeader(value="Authorization") String token, @RequestParam("page")int page) {
Long kakao_uid;
try {
kakao_uid = authService.getKakaoUserInfo(token).getId();
if (kakao_uid == null)
return PostsResponseDto.noAuthentication();
} catch (Exception exception) {
log.info(exception.getMessage());
return PostsResponseDto.databaseError();
}
ResponseEntity<? super GetPostsAllResponseDto> response = postsService.getAllScrapPost(kakao_uid, page);
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
Expand All @@ -19,8 +20,8 @@ public class SummarizationController {
/**
* 사용자가 작성한 글에 대해 AI가 요약한 내용을 반환한다.
*/
@GetMapping("summary/{postId}")
public ResponseEntity<? super GetSummaryResponseDto> summary(@PathVariable("postId") Long postId) {
@GetMapping("/summary")
public ResponseEntity<? super GetSummaryResponseDto> summary(@RequestParam("postId") Long postId) {
// 포스트 아이디로부터 포스트 얻어오기
ResponseEntity<? super GetSummaryResponseDto> response = postsService.getSummary(postId);
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ public ResponseEntity<?> generateQuiz(@RequestBody GenerateQuizRequestDto dto) {
}

// 태그별 퀴즈 출력
@GetMapping("/quiz/tag-quiz/{tagName}")
public ResponseEntity<? super GetPostQuizResponseDto> getTagQuiz(@PathVariable String tagName) {
@GetMapping("/quiz/tag-quiz")
public ResponseEntity<? super GetPostQuizResponseDto> getTagQuiz(@RequestParam String tagName) {
ResponseEntity<? super GetPostQuizResponseDto> response = quizService.getTagQuiz(tagName);
return response;
}

// 게시글 별 퀴즈 출력
@GetMapping("/quiz/post-quiz/{postId}")
public ResponseEntity<? super GetPostQuizResponseDto> getPostQuiz(@PathVariable Long postId) {
@GetMapping("/quiz/post-quiz")
public ResponseEntity<? super GetPostQuizResponseDto> getPostQuiz(@RequestParam Long postId) {
ResponseEntity<? super GetPostQuizResponseDto> response = quizService.getPostQuiz(postId);
return response;
}


// 정답 채점
@PostMapping("/quiz/grade")
public ResponseEntity<? super QuizGradeResponseDto> gradeQuiz(@RequestBody GradeQuizRequestDto dto) {
public ResponseEntity<? super QuizGradeResponseDto> gradeQuiz(@RequestHeader(value="Authorization") String token, @RequestBody GradeQuizRequestDto dto) {
// uuid 받아서 사용자 조회 필요 -> 오답노트 엔티티에 저장해야함
ResponseEntity<? super QuizGradeResponseDto> response = quizService.gradeQuiz(dto);
return response;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.beotkkot.qtudy.controller.tag;

import com.beotkkot.qtudy.dto.response.tags.GetTop3TagsDto;
import com.beotkkot.qtudy.service.tag.TagService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
public class TagApiController {
private final TagService tagService;

@GetMapping("/tag/top3")
public ResponseEntity<? super GetTop3TagsDto> getTop3Tags() {
ResponseEntity<? super GetTop3TagsDto> response = tagService.getTop3Tags();
return response;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/beotkkot/qtudy/domain/posts/Posts.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Posts {
private Long postId;

@Column(nullable = false)
private Long userUid;
private Long kakaoId;

@Column(nullable = false)
private String title;
Expand All @@ -34,7 +34,7 @@ public class Posts {

// AI 요약본
@Column(columnDefinition = "TEXT")
private String aiScript;
private String summary;

private int commentCount;

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/beotkkot/qtudy/dto/object/TagListItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.beotkkot.qtudy.dto.object;

import com.beotkkot.qtudy.domain.tags.Tags;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class TagListItem {
private String name;
private int count;

public static TagListItem of(Tags tag) {

return TagListItem.builder()
.name(tag.getName())
.count(tag.getCount())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public Posts toEntity(Long uid) {
String tagString = String.join(",", tag);

return Posts.builder()
.userUid(uid)
.kakaoId(uid)
.title(title)
.content(content)
.aiScript(summary)
.summary(summary)
.tag(tagString)
.createdAt(writeDatetime)
.commentCount(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@

@Getter
public class GetPostsAllResponseDto extends ResponseDto {
private int page;
private List<PostListItem> postList;

public GetPostsAllResponseDto(List<PostListItem> PostListItem) {
public GetPostsAllResponseDto(List<PostListItem> PostListItem, int page) {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
this.page = page;
this.postList = PostListItem;
}

public static ResponseEntity<GetPostsAllResponseDto> success(List<PostListItem> PostListItem) {
GetPostsAllResponseDto result = new GetPostsAllResponseDto(PostListItem);
public static ResponseEntity<GetPostsAllResponseDto> success(List<PostListItem> PostListItem, int page) {
GetPostsAllResponseDto result = new GetPostsAllResponseDto(PostListItem, page);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.beotkkot.qtudy.dto.response.tags;

import com.beotkkot.qtudy.common.ResponseCode;
import com.beotkkot.qtudy.common.ResponseMessage;
import com.beotkkot.qtudy.dto.object.TagListItem;
import com.beotkkot.qtudy.dto.response.ResponseDto;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

@Getter
public class GetTop3TagsDto extends ResponseDto {
private List<TagListItem> top3List;

public GetTop3TagsDto(List<TagListItem> TagListItem) {
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS);
this.top3List = TagListItem;
}

public static ResponseEntity<GetTop3TagsDto> success(List<TagListItem> top3List) {
GetTop3TagsDto result = new GetTop3TagsDto(top3List);
return ResponseEntity.status(HttpStatus.OK).body(result);
}

public static ResponseEntity<ResponseDto> notExistedPost(){
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.beotkkot.qtudy.repository.posts;

import com.beotkkot.qtudy.domain.posts.Posts;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -10,12 +11,12 @@

@Repository
public interface PostsRepository extends JpaRepository<Posts, Long> {
List<Posts> findAllByUserUid(Long uuid);
List<Posts> findAllByKakaoId(Long kakaoId, PageRequest pageRequest);

@Query("SELECT p FROM Posts p WHERE p.title LIKE %:searchWord% OR p.content LIKE %:searchWord% OR p.tag LIKE %:searchWord%")
List<Posts> findBySearchWord(String searchWord);
List<Posts> findBySearchWord(String searchWord, PageRequest pageRequest);

List<Posts> findByCategoryId(Long categoryId);
List<Posts> findByCategoryId(Long categoryId, PageRequest pageRequest);

Posts findByPostId(Long postId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.beotkkot.qtudy.domain.scrap.Scrap;
import com.beotkkot.qtudy.domain.primaryKey.ScrapPk;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -15,7 +16,7 @@ public interface ScrapRepository extends JpaRepository<Scrap, ScrapPk> {
Scrap findByPostIdAndUserId(Long postId, Long uid);

@Query("SELECT s.postId FROM Scrap s WHERE s.userId = :userId")
List<Long> findPostIdsByUserId(@Param("userId") Long userId);
List<Long> findPostIdsByUserId(@Param("userId") Long userId, PageRequest pageRequest);

@Transactional
void deleteByPostId(Long postId);
Expand Down
Loading