From 5265b10a74df7802d6cb354eef49f9576f0b2774 Mon Sep 17 00:00:00 2001 From: sleeg00 Date: Sat, 15 Jul 2023 18:23:21 +0900 Subject: [PATCH] PostTestAPI --- .../Controller/PostController.java | 8 ++++++ .../Pick_Read_Me/Service/PostService.java | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java b/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java index d7d5937..d5928a4 100644 --- a/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java +++ b/src/main/java/com/example/Pick_Read_Me/Controller/PostController.java @@ -72,7 +72,15 @@ public ResponseEntity createPost(Authentication authentication, return postService.createPost(authentication, postsDTO, file); } + @PostMapping(value = "/post/test/posts") + @Operation(summary = "글 작성 Test API", description = "api/post/posts랑 같지만 File을 안 넘겨도 글을 쓸수 있습니다!\n"+"" + + "즉 Readme없어도 됨 내가 테스트 할려고 만듬..") + public ResponseEntity createPost(Authentication authentication, + @ModelAttribute PostsDTO postsDTO) throws IOException, TranscoderException { + // Post 작성 서비스 호출 + return postService.createPostTest(authentication, postsDTO); + } @Operation(summary = "모든 사용자의 전체 글을 조회하는 API") diff --git a/src/main/java/com/example/Pick_Read_Me/Service/PostService.java b/src/main/java/com/example/Pick_Read_Me/Service/PostService.java index 8fda92c..92cfbeb 100644 --- a/src/main/java/com/example/Pick_Read_Me/Service/PostService.java +++ b/src/main/java/com/example/Pick_Read_Me/Service/PostService.java @@ -391,4 +391,30 @@ public String extractImageUrlsFromHtml(String html, String repoName) { } + public ResponseEntity createPostTest(Authentication authentication, PostsDTO postsDTO) { + log.info(String.valueOf(authentication)); + Long github_id = Long.valueOf(authentication.getName()); + Member member = memberRepository.findById(Long.valueOf(github_id)) + .orElseThrow(() -> new MemberNotFoundException("Member not found with id: " + github_id)); + + + if (postsDTO.getTitle() == null || postsDTO.getTitle().isEmpty()) + return ResponseEntity.badRequest().body(null); + + Post post = new Post(); + post.setContent(postsDTO.getContent()); + post.setTitle(postsDTO.getTitle()); + post.setPostCreatedAt(new Date()); + post.setPostUpdatedAt(new Date()); + post.setRepo(postsDTO.getRepo()); + post.setPost_like(0L); + post.setMember(member); + + member.getPosts().add(post); + + postRepository.save(post); + memberRepository.save(member); + + return ResponseEntity.ok().body(post); + } }