Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bin/
*.iws
*.iml
*.ipr
*.yml
out/
!**/src/main/**/out/
!**/src/test/**/out/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package com.example.springhw32.controller;

import com.example.springhw32.dto.CommentDto;
import com.example.springhw32.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/posts/comments")
@RestController
@RequiredArgsConstructor
public class CommentController {

private final CommentService commentService;

// 댓글 작성
@PostMapping
public CommentDto createComment(@ModelAttribute CommentDto commentDto, @RequestParam Long postId) {
return commentService.createComment(commentDto, postId);
}

// 특정 게시물에 달린 모든 댓글 조회
@GetMapping("/{postId}")
public List<CommentDto> findAllByPostId(@PathVariable Long postId) {
return commentService.findAllByPostId(postId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
package com.example.springhw32.controller;

import com.example.springhw32.dto.PostDto;
import com.example.springhw32.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/posts")
@RestController
@RequiredArgsConstructor
public class PostController {
private final PostService postService;

// 글 작성
@PostMapping
public PostDto createPost(@ModelAttribute PostDto postDto, Long userId) {
return postService.createPost(postDto, userId);
}

// 최신순 글 조회
@GetMapping
public List<PostDto> finaAllByCreatedAtDesc() {
return postService.finaAllByCreatedAtDesc();
}

// 작성자 글 조회
@GetMapping("/{writer}")
public List<PostDto> findAllByUsername(@PathVariable("writer") String username) {
return postService.findAllByUsername(username);
}

// 댓글 많은 순 글 조회
@GetMapping("/comments")
public List<PostDto> findAllByOrderByCommentCountDesc() {
return postService.findAllByOrderByCommentCountDesc();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package com.example.springhw32.controller;

import com.example.springhw32.dto.UserDto;
import com.example.springhw32.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/users")
@RestController
@RequiredArgsConstructor
public class UserController {

private final UserService userService;

// 회원가입
@PostMapping
public UserDto join(@ModelAttribute UserDto userDto) {
return userService.join(userDto);
}
}

6 changes: 6 additions & 0 deletions src/main/java/com/example/springhw32/dto/CommentDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.example.springhw32.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CommentDto {

private String content;
}
15 changes: 15 additions & 0 deletions src/main/java/com/example/springhw32/dto/PostDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package com.example.springhw32.dto;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
public class PostDto {

private String title;

private String content;

private Long commentCount;

private LocalDateTime createdAt;

}
9 changes: 9 additions & 0 deletions src/main/java/com/example/springhw32/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.example.springhw32.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UserDto {

private String username;

private String password;

}
23 changes: 23 additions & 0 deletions src/main/java/com/example/springhw32/entity/Comment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package com.example.springhw32.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

@Getter
@Setter
@Entity
public class Comment {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long CommentId;

@ManyToOne
private Post post;

@NonNull

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nonnull과 nullable=false의 차이점이 궁금합니다..!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마 @nonnnull은 프로그램 코드 내에서 null이 들어가지 않게 하는 거고
nullable=false 이건 데이터베이스 column에 null이 들어가는 걸 막는 걸로 알고 잇어

private String content;


}
33 changes: 33 additions & 0 deletions src/main/java/com/example/springhw32/entity/Post.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package com.example.springhw32.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
@Entity
public class Post {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long postId;

@ManyToOne
private User user;

@Column(nullable = false)
private String title;

@Column(nullable = false)
private String content;

@Column(nullable = false)
private Long commentCount = 0L;

@Column(nullable = false)
private LocalDateTime createdAt;

}
22 changes: 22 additions & 0 deletions src/main/java/com/example/springhw32/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.example.springhw32.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

@Getter
@Setter
@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(unique = true, nullable = false)
private String username;

@Column(nullable = false)
private String password;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.example.springhw32.repository;

public interface CommentRepository {
import com.example.springhw32.entity.Comment;
import com.example.springhw32.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CommentRepository extends JpaRepository<Comment,Long> {
List<Comment> findAllByPost_PostId(Long postId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package com.example.springhw32.repository;

public interface PostRepository {
import com.example.springhw32.dto.PostDto;
import com.example.springhw32.entity.Post;
import org.hibernate.boot.archive.internal.JarProtocolArchiveDescriptor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PostRepository extends JpaRepository<Post,Long> {

List<Post> findAllByOrderByCreatedAtDesc();
List<Post> findAllByUser_Username(String username);
List<Post> findAllByOrderByCommentCountDesc();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.example.springhw32.repository;

public interface UserRepository {
import com.example.springhw32.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;


@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}
47 changes: 47 additions & 0 deletions src/main/java/com/example/springhw32/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
package com.example.springhw32.service;

import com.example.springhw32.dto.CommentDto;
import com.example.springhw32.entity.Comment;
import com.example.springhw32.entity.Post;
import com.example.springhw32.repository.CommentRepository;
import com.example.springhw32.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class CommentService {

private final PostRepository postRepository;
private final CommentRepository commentRepository;

public CommentDto createComment(CommentDto commentDto, Long postId){
Post post = postRepository.findById(postId).orElseThrow();

Comment comment = new Comment();
comment.setContent(commentDto.getContent());
comment.setPost(post);
commentRepository.save(comment);

// 댓글 수 증가
post.setCommentCount(post.getCommentCount() + 1L);
postRepository.save(post);

return commentDto;
}

public List<CommentDto> findAllByPostId(Long postId){
return commentRepository.findAllByPost_PostId(postId).stream()
.map(this::convertToCommentDto)
.collect(Collectors.toList());
}


public CommentDto convertToCommentDto(Comment comment) {
CommentDto commentDto = new CommentDto();

commentDto.setContent(comment.getContent());

return commentDto;
}
}
Loading