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

[#46] 커뮤니티 Post, comment 생성 #47

Open
wants to merge 2 commits into
base: feature/27
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions post-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<CommentDto> getCommentsByPostId(@PathVariable int postId) {
List<Comment> 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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -14,30 +17,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<PostDto> getPosts(){
public List<PostDto> 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<PostDto> 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);
}
}
53 changes: 53 additions & 0 deletions post-service/src/main/java/com/whalewatch/domain/Comment.java
Original file line number Diff line number Diff line change
@@ -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; }
}
56 changes: 38 additions & 18 deletions post-service/src/main/java/com/whalewatch/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import jakarta.persistence.*;

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

@Entity
@Table(name = "post")
public class Post {
Expand All @@ -12,31 +16,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<Comment> 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<Comment> getComments() { return comments; }
public void setComments(List<Comment> comments) { this.comments = comments; }
}
42 changes: 42 additions & 0 deletions post-service/src/main/java/com/whalewatch/dto/CommentDto.java
Original file line number Diff line number Diff line change
@@ -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; }
}
Loading