From 60ff5d232cb7636cd557f30a1c849db84061e46d Mon Sep 17 00:00:00 2001 From: Junmo Date: Tue, 4 Mar 2025 09:07:59 +0900 Subject: [PATCH 1/2] feat: Post API generate --- post-service/build.gradle | 8 +++ .../whalewatch/controller/PostController.java | 53 ++++++++++++++---- .../main/java/com/whalewatch/domain/Post.java | 55 +++++++++++++------ .../main/java/com/whalewatch/dto/PostDto.java | 49 ++++++++++++----- .../com/whalewatch/mapper/PostMapper.java | 13 ++--- .../whalewatch/repository/PostRepository.java | 4 ++ .../com/whalewatch/service/PostService.java | 26 ++++++++- 7 files changed, 158 insertions(+), 50 deletions(-) diff --git a/post-service/build.gradle b/post-service/build.gradle index 3069e1f..c5c8c1c 100644 --- a/post-service/build.gradle +++ b/post-service/build.gradle @@ -11,6 +11,14 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + //Security + implementation 'org.springframework.boot:spring-boot-starter-security' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + + // MapStruct + implementation 'org.mapstruct:mapstruct:1.5.5.Final' + annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' } diff --git a/post-service/src/main/java/com/whalewatch/controller/PostController.java b/post-service/src/main/java/com/whalewatch/controller/PostController.java index efbdb34..d4bd6a1 100644 --- a/post-service/src/main/java/com/whalewatch/controller/PostController.java +++ b/post-service/src/main/java/com/whalewatch/controller/PostController.java @@ -4,6 +4,8 @@ import com.whalewatch.dto.PostDto; import com.whalewatch.mapper.PostMapper; import com.whalewatch.service.PostService; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -14,30 +16,59 @@ public class PostController { private final PostService postService; private final PostMapper postMapper; + private final CommentMapper commentMapper; - public PostController(PostService postService,PostMapper postMapper) { + public PostController(PostService postService, PostMapper postMapper, CommentMapper commentMapper) { this.postService = postService; this.postMapper = postMapper; + this.commentMapper = commentMapper; } + // 전체 게시글 조회 @GetMapping - public List getPosts(){ + public List getPosts() { return postService.getAllPosts().stream() - .map(postMapper::toDto) //entity -> dto + .map(entity -> postMapper.toDto(entity, commentMapper)) .collect(Collectors.toList()); } + // 특정 게시글 조회 (조회 시 viewCount 1 증가, 댓글 포함) + @GetMapping("/{id}") + public PostDto getPost(@PathVariable int id) { + Post post = postService.incrementViewCount(id); + return postMapper.toDto(post, commentMapper); + } + + // 내 게시글 조회 (로그인시) + @GetMapping("/my") + public List getMyPosts(@AuthenticationPrincipal UserDetails userDetails) { + String username = userDetails.getUsername(); + return postService.getPostsByUsername(username).stream() + .map(entity -> postMapper.toDto(entity, commentMapper)) + .collect(Collectors.toList()); + } + + @PostMapping - public PostDto createPost(@RequestBody PostDto post) { - Post entity = postMapper.toEntity(post); // Dto -> entity + public PostDto createPost(@RequestBody PostDto postDto, + @AuthenticationPrincipal UserDetails userDetails) { + postDto.setUsername(userDetails.getUsername()); + Post entity = postMapper.toEntity(postDto); Post saved = postService.createPost(entity); - return postMapper.toDto(saved); + return postMapper.toDto(saved, commentMapper); + } + + // 게시글 수정 + @PutMapping("/{id}") + public PostDto updatePost(@PathVariable int id, @RequestBody PostDto postDto) { + Post entity = postMapper.toEntity(postDto); + Post updated = postService.updatePost(id, entity); + return postMapper.toDto(updated, commentMapper); } - @PostMapping("/{id}") - public PostDto updatePost(@PathVariable int id, @RequestBody PostDto post) { - Post entity = postMapper.toEntity(post); - Post updated = postService.updatePost(id,entity); - return postMapper.toDto(entity); + // 게시글 삭제 + @DeleteMapping("/{id}") + public void deletePost(@PathVariable int id) { + postService.deletePost(id); } } diff --git a/post-service/src/main/java/com/whalewatch/domain/Post.java b/post-service/src/main/java/com/whalewatch/domain/Post.java index 1030917..4929e4c 100644 --- a/post-service/src/main/java/com/whalewatch/domain/Post.java +++ b/post-service/src/main/java/com/whalewatch/domain/Post.java @@ -2,6 +2,9 @@ import jakarta.persistence.*; +import java.time.LocalDateTime; +import java.util.ArrayList; + @Entity @Table(name = "post") public class Post { @@ -12,31 +15,47 @@ public class Post { private String title; private String content; + private String username; + private int recommendedCount; + private int viewCount; + private LocalDateTime createdDate; + + // 댓글 리스트 양방향 연관 + @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true) + private List comments = new ArrayList<>(); - protected Post(){} + protected Post() {} - public Post(String title, String content){ + public Post(String title, String content, String username) { this.title = title; this.content = content; + this.username = username; + this.recommendedCount = 0; + this.viewCount = 0; + this.createdDate = LocalDateTime.now(); } - public void setTitle(String title) { - this.title = title; - } + // Getter/Setter + public int getId() { return id; } - public void setContent(String content) { - this.content = content; - } + public String getTitle() { return title; } + public void setTitle(String title) { this.title = title; } - public int getId() { - return id; - } + public String getContent() { return content; } + public void setContent(String content) { this.content = content; } - public String getTitle() { - return title; - } + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } - public String getContent() { - return content; - } -} + public int getRecommendedCount() { return recommendedCount; } + public void setRecommendedCount(int recommendedCount) { this.recommendedCount = recommendedCount; } + + public int getViewCount() { return viewCount; } + public void setViewCount(int viewCount) { this.viewCount = viewCount; } + + public LocalDateTime getCreatedDate() { return createdDate; } + public void setCreatedDate(LocalDateTime createdDate) { this.createdDate = createdDate; } + + public List getComments() { return comments; } + public void setComments(List comments) { this.comments = comments; } +} \ No newline at end of file diff --git a/post-service/src/main/java/com/whalewatch/dto/PostDto.java b/post-service/src/main/java/com/whalewatch/dto/PostDto.java index 6c780e8..426c711 100644 --- a/post-service/src/main/java/com/whalewatch/dto/PostDto.java +++ b/post-service/src/main/java/com/whalewatch/dto/PostDto.java @@ -1,30 +1,53 @@ package com.whalewatch.dto; +import java.time.LocalDateTime; + public class PostDto { private int id; private String title; private String content; + private String username; + private int recommendedCount; + private int viewCount; + private LocalDateTime createdDate; + private List comments; - public PostDto(int id, String title, String content) { - this.id = id; - this.title = title; - this.content = content; - } + public PostDto() {} - public void setId(int id) { + public PostDto(int id, String title, String content, String username, int recommendedCount, + int viewCount, LocalDateTime createdDate, List comments) { this.id = id; - } - - public void setTitle(String title) { this.title = title; - } - - public void setContent(String content) { this.content = content; + this.username = username; + this.recommendedCount = recommendedCount; + this.viewCount = viewCount; + this.createdDate = createdDate; + this.comments = comments; } - // Getter (id는 조회만 가능) + // Getter/Setter public int getId() { return id; } + public void setId(int id) { this.id = id; } + public String getTitle() { return title; } + public void setTitle(String title) { this.title = title; } + public String getContent() { return content; } + public void setContent(String content) { this.content = content; } + + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + + public int getRecommendedCount() { return recommendedCount; } + public void setRecommendedCount(int recommendedCount) { this.recommendedCount = recommendedCount; } + + public int getViewCount() { return viewCount; } + public void setViewCount(int viewCount) { this.viewCount = viewCount; } + + public LocalDateTime getCreatedDate() { return createdDate; } + public void setCreatedDate(LocalDateTime createdDate) { this.createdDate = createdDate; } + + public List getComments() { return comments; } + public void setComments(List comments) { this.comments = comments; } } diff --git a/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java b/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java index 359c4eb..f09c384 100644 --- a/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java +++ b/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java @@ -2,20 +2,19 @@ import com.whalewatch.dto.PostDto; import com.whalewatch.domain.Post; +import org.mapstruct.Context; import org.mapstruct.Mapper; import org.mapstruct.Mapping; -@Mapper(componentModel = "spring") +@Mapper(componentModel = "spring", uses = {CommentMapper.class}) public interface PostMapper { + // Entity -> DTO - @Mapping(target = "id", source = "id") - @Mapping(target = "title", source = "title") - @Mapping(target = "content", source = "content") - PostDto toDto(Post entity); + @Mapping(target = "comments", expression = "java(entity.getComments().stream().map(commentMapper::toDto).collect(Collectors.toList()))") + PostDto toDto(Post entity, @Context CommentMapper commentMapper); // DTO -> Entity @Mapping(target = "id", ignore = true) - @Mapping(target = "title", source = "title") - @Mapping(target = "content", source = "content") + @Mapping(target = "comments", ignore = true) Post toEntity(PostDto dto); } diff --git a/post-service/src/main/java/com/whalewatch/repository/PostRepository.java b/post-service/src/main/java/com/whalewatch/repository/PostRepository.java index 0d1769b..99bbf44 100644 --- a/post-service/src/main/java/com/whalewatch/repository/PostRepository.java +++ b/post-service/src/main/java/com/whalewatch/repository/PostRepository.java @@ -3,5 +3,9 @@ import com.whalewatch.domain.Post; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface PostRepository extends JpaRepository { + + List findByUsername(String username); } diff --git a/post-service/src/main/java/com/whalewatch/service/PostService.java b/post-service/src/main/java/com/whalewatch/service/PostService.java index fb3f5ea..6f52397 100644 --- a/post-service/src/main/java/com/whalewatch/service/PostService.java +++ b/post-service/src/main/java/com/whalewatch/service/PostService.java @@ -19,14 +19,38 @@ public List getAllPosts() { return postRepository.findAll(); } + public Post getPostById(int id) { + return postRepository.findById(id) + .orElseThrow(() -> new RuntimeException("Post not found with id: " + id)); + } + + // 게시글 조회 시 viewCount 증가 + public Post incrementViewCount(int id) { + Post post = getPostById(id); + post.setViewCount(post.getViewCount() + 1); + return postRepository.save(post); + } + public Post createPost(Post post) { return postRepository.save(post); } public Post updatePost(int id, Post updatedPost) { - Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("Post not found")); + Post post = getPostById(id); post.setTitle(updatedPost.getTitle()); post.setContent(updatedPost.getContent()); + post.setRecommendedCount(updatedPost.getRecommendedCount()); + post.setViewCount(updatedPost.getViewCount()); + post.setCreatedDate(updatedPost.getCreatedDate()); return postRepository.save(post); } + + public void deletePost(int id) { + Post post = getPostById(id); + postRepository.delete(post); + } + + public List getPostsByUsername(String username) { + return postRepository.findByUsername(username); + } } From 91f6c13b0028bc3f6a0bccf1055e525bac85bde8 Mon Sep 17 00:00:00 2001 From: Junmo Date: Tue, 4 Mar 2025 09:23:05 +0900 Subject: [PATCH 2/2] feat: Comment API generate --- .../com/whalewatch/config/SecurityConfig.java | 25 ++++++++ .../controller/CommentController.java | 58 +++++++++++++++++++ .../whalewatch/controller/PostController.java | 1 + .../java/com/whalewatch/domain/Comment.java | 53 +++++++++++++++++ .../main/java/com/whalewatch/domain/Post.java | 1 + .../java/com/whalewatch/dto/CommentDto.java | 42 ++++++++++++++ .../main/java/com/whalewatch/dto/PostDto.java | 1 + .../com/whalewatch/mapper/CommentMapper.java | 26 +++++++++ .../com/whalewatch/mapper/PostMapper.java | 3 +- .../repository/CommentRepository.java | 10 ++++ .../whalewatch/service/CommentService.java | 47 +++++++++++++++ 11 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 post-service/src/main/java/com/whalewatch/config/SecurityConfig.java create mode 100644 post-service/src/main/java/com/whalewatch/controller/CommentController.java create mode 100644 post-service/src/main/java/com/whalewatch/domain/Comment.java create mode 100644 post-service/src/main/java/com/whalewatch/dto/CommentDto.java create mode 100644 post-service/src/main/java/com/whalewatch/mapper/CommentMapper.java create mode 100644 post-service/src/main/java/com/whalewatch/repository/CommentRepository.java create mode 100644 post-service/src/main/java/com/whalewatch/service/CommentService.java diff --git a/post-service/src/main/java/com/whalewatch/config/SecurityConfig.java b/post-service/src/main/java/com/whalewatch/config/SecurityConfig.java new file mode 100644 index 0000000..f242b03 --- /dev/null +++ b/post-service/src/main/java/com/whalewatch/config/SecurityConfig.java @@ -0,0 +1,25 @@ +package com.whalewatch.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.web.SecurityFilterChain; +import static org.springframework.security.config.Customizer.withDefaults; + +@Configuration +@EnableWebSecurity +public class SecurityConfig { + + @Bean + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { + http + .csrf(csrf -> csrf.disable()) // 필요에 따라 CSRF 설정 조정 + .authorizeHttpRequests(authorize -> authorize + .requestMatchers("/api/posts/**", "/api/comments/**").authenticated() + .anyRequest().permitAll() + ) + .httpBasic(withDefaults()); // 기본 httpBasic 설정 사용 + return http.build(); + } +} diff --git a/post-service/src/main/java/com/whalewatch/controller/CommentController.java b/post-service/src/main/java/com/whalewatch/controller/CommentController.java new file mode 100644 index 0000000..8394b76 --- /dev/null +++ b/post-service/src/main/java/com/whalewatch/controller/CommentController.java @@ -0,0 +1,58 @@ +package com.whalewatch.controller; + +import com.whalewatch.domain.Comment; +import com.whalewatch.dto.CommentDto; +import com.whalewatch.mapper.CommentMapper; +import com.whalewatch.service.CommentService; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/api/comments") +public class CommentController { + + private final CommentService commentService; + private final CommentMapper commentMapper; + + public CommentController(CommentService commentService, CommentMapper commentMapper) { + this.commentService = commentService; + this.commentMapper = commentMapper; + } + + // 특정 게시글의 댓글 조회 + @GetMapping("/post/{postId}") + public List getCommentsByPostId(@PathVariable int postId) { + List comments = commentService.getCommentsByPostId(postId); + return comments.stream().map(commentMapper::toDto).collect(Collectors.toList()); + } + + // 댓글 생성 + @PostMapping("/post/{postId}") + public CommentDto createComment(@PathVariable int postId, + @RequestBody CommentDto commentDto, + @AuthenticationPrincipal UserDetails userDetails) { + commentDto.setUsername(userDetails.getUsername()); + Comment entity = commentMapper.toEntity(commentDto); + Comment saved = commentService.createComment(entity, postId); + return commentMapper.toDto(saved); + } + + // 댓글 수정 + @PutMapping("/{commentId}") + public CommentDto updateComment(@PathVariable int commentId, @RequestBody CommentDto commentDto) { + Comment entity = commentMapper.toEntity(commentDto); + Comment updated = commentService.updateComment(commentId, entity); + return commentMapper.toDto(updated); + } + + // 댓글 삭제 + @DeleteMapping("/{commentId}") + public void deleteComment(@PathVariable int commentId) { + commentService.deleteComment(commentId); + } +} + diff --git a/post-service/src/main/java/com/whalewatch/controller/PostController.java b/post-service/src/main/java/com/whalewatch/controller/PostController.java index d4bd6a1..744e7c2 100644 --- a/post-service/src/main/java/com/whalewatch/controller/PostController.java +++ b/post-service/src/main/java/com/whalewatch/controller/PostController.java @@ -2,6 +2,7 @@ import com.whalewatch.domain.Post; import com.whalewatch.dto.PostDto; +import com.whalewatch.mapper.CommentMapper; import com.whalewatch.mapper.PostMapper; import com.whalewatch.service.PostService; import org.springframework.security.core.annotation.AuthenticationPrincipal; diff --git a/post-service/src/main/java/com/whalewatch/domain/Comment.java b/post-service/src/main/java/com/whalewatch/domain/Comment.java new file mode 100644 index 0000000..b8caa0f --- /dev/null +++ b/post-service/src/main/java/com/whalewatch/domain/Comment.java @@ -0,0 +1,53 @@ +package com.whalewatch.domain; + +import jakarta.persistence.*; + +import java.time.LocalDateTime; + +@Entity +@Table(name = "comment") +public class Comment { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + + private String content; + private String username; + private int recommendedCount; + private LocalDateTime createdDate; + + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "post_id") + private Post post; + + protected Comment() {} + + public Comment(String content, String username, Post post) { + this.content = content; + this.username = username; + this.post = post; + this.recommendedCount = 0; + this.createdDate = LocalDateTime.now(); + } + + // Getter/Setter + public int getId() { return id; } + + public String getContent() { return content; } + public void setContent(String content) { this.content = content; } + + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + + public int getRecommendedCount() { return recommendedCount; } + public void setRecommendedCount(int recommendedCount) { this.recommendedCount = recommendedCount; } + + public LocalDateTime getCreatedDate() { return createdDate; } + public void setCreatedDate(LocalDateTime createdDate) { this.createdDate = createdDate; } + + public Post getPost() { return post; } + public void setPost(Post post) { this.post = post; } +} diff --git a/post-service/src/main/java/com/whalewatch/domain/Post.java b/post-service/src/main/java/com/whalewatch/domain/Post.java index 4929e4c..070593e 100644 --- a/post-service/src/main/java/com/whalewatch/domain/Post.java +++ b/post-service/src/main/java/com/whalewatch/domain/Post.java @@ -4,6 +4,7 @@ import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.List; @Entity @Table(name = "post") diff --git a/post-service/src/main/java/com/whalewatch/dto/CommentDto.java b/post-service/src/main/java/com/whalewatch/dto/CommentDto.java new file mode 100644 index 0000000..0091134 --- /dev/null +++ b/post-service/src/main/java/com/whalewatch/dto/CommentDto.java @@ -0,0 +1,42 @@ +package com.whalewatch.dto; + +import java.time.LocalDateTime; + +public class CommentDto { + private int id; + private String content; + private String username; + private int recommendedCount; + private LocalDateTime createdDate; + private int postId; + + public CommentDto() {} + + public CommentDto(int id, String content, String username, int recommendedCount, LocalDateTime createdDate, int postId) { + this.id = id; + this.content = content; + this.username = username; + this.recommendedCount = recommendedCount; + this.createdDate = createdDate; + this.postId = postId; + } + + // Getter/Setter + public int getId() { return id; } + public void setId(int id) { this.id = id; } + + public String getContent() { return content; } + public void setContent(String content) { this.content = content; } + + public String getUsername() { return username; } + public void setUsername(String username) { this.username = username; } + + public int getRecommendedCount() { return recommendedCount; } + public void setRecommendedCount(int recommendedCount) { this.recommendedCount = recommendedCount; } + + public LocalDateTime getCreatedDate() { return createdDate; } + public void setCreatedDate(LocalDateTime createdDate) { this.createdDate = createdDate; } + + public int getPostId() { return postId; } + public void setPostId(int postId) { this.postId = postId; } +} \ No newline at end of file diff --git a/post-service/src/main/java/com/whalewatch/dto/PostDto.java b/post-service/src/main/java/com/whalewatch/dto/PostDto.java index 426c711..8abcdb0 100644 --- a/post-service/src/main/java/com/whalewatch/dto/PostDto.java +++ b/post-service/src/main/java/com/whalewatch/dto/PostDto.java @@ -1,6 +1,7 @@ package com.whalewatch.dto; import java.time.LocalDateTime; +import java.util.List; public class PostDto { private int id; diff --git a/post-service/src/main/java/com/whalewatch/mapper/CommentMapper.java b/post-service/src/main/java/com/whalewatch/mapper/CommentMapper.java new file mode 100644 index 0000000..6260d75 --- /dev/null +++ b/post-service/src/main/java/com/whalewatch/mapper/CommentMapper.java @@ -0,0 +1,26 @@ +package com.whalewatch.mapper; + +import com.whalewatch.domain.Comment; +import com.whalewatch.domain.Post; +import com.whalewatch.dto.CommentDto; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper(componentModel = "spring") +public interface CommentMapper { + + // Entity -> DTO + @Mapping(target = "postId", source = "post.id") + CommentDto toDto(Comment entity); + + // DTO -> Entity + @Mapping(target = "id", ignore = true) + @Mapping(target = "post", ignore = true) + Comment toEntity(CommentDto dto); + + default Comment toEntity(CommentDto dto, Post post) { + Comment comment = toEntity(dto); + comment.setPost(post); + return comment; + } +} diff --git a/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java b/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java index f09c384..3484302 100644 --- a/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java +++ b/post-service/src/main/java/com/whalewatch/mapper/PostMapper.java @@ -6,11 +6,12 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; + @Mapper(componentModel = "spring", uses = {CommentMapper.class}) public interface PostMapper { // Entity -> DTO - @Mapping(target = "comments", expression = "java(entity.getComments().stream().map(commentMapper::toDto).collect(Collectors.toList()))") + @Mapping(target = "comments", expression = "java(entity.getComments().stream().map(commentMapper::toDto).collect(java.util.stream.Collectors.toList()))") PostDto toDto(Post entity, @Context CommentMapper commentMapper); // DTO -> Entity diff --git a/post-service/src/main/java/com/whalewatch/repository/CommentRepository.java b/post-service/src/main/java/com/whalewatch/repository/CommentRepository.java new file mode 100644 index 0000000..011d9f0 --- /dev/null +++ b/post-service/src/main/java/com/whalewatch/repository/CommentRepository.java @@ -0,0 +1,10 @@ +package com.whalewatch.repository; + +import com.whalewatch.domain.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface CommentRepository extends JpaRepository { + List findByPostId(int postId); +} diff --git a/post-service/src/main/java/com/whalewatch/service/CommentService.java b/post-service/src/main/java/com/whalewatch/service/CommentService.java new file mode 100644 index 0000000..d8f5964 --- /dev/null +++ b/post-service/src/main/java/com/whalewatch/service/CommentService.java @@ -0,0 +1,47 @@ +package com.whalewatch.service; + +import com.whalewatch.domain.Comment; +import com.whalewatch.domain.Post; +import com.whalewatch.repository.CommentRepository; +import com.whalewatch.repository.PostRepository; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class CommentService { + private final CommentRepository commentRepository; + private final PostRepository postRepository; + + public CommentService(CommentRepository commentRepository, PostRepository postRepository) { + this.commentRepository = commentRepository; + this.postRepository = postRepository; + } + + public List getCommentsByPostId(int postId) { + return commentRepository.findByPostId(postId); + } + + public Comment createComment(Comment comment, int postId) { + Post post = postRepository.findById(postId) + .orElseThrow(() -> new RuntimeException("Post not found with id: " + postId)); + comment.setPost(post); + return commentRepository.save(comment); + } + + public Comment updateComment(int commentId, Comment updatedComment) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new RuntimeException("Comment not found with id: " + commentId)); + comment.setContent(updatedComment.getContent()); + comment.setUsername(updatedComment.getUsername()); + comment.setRecommendedCount(updatedComment.getRecommendedCount()); + comment.setCreatedDate(updatedComment.getCreatedDate()); + return commentRepository.save(comment); + } + + public void deleteComment(int commentId) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new RuntimeException("Comment not found with id: " + commentId)); + commentRepository.delete(comment); + } +}