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
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package com.example.springhw32.controller;

import com.example.springhw32.dto.CommentDto;
import com.example.springhw32.dto.UserDto;
import com.example.springhw32.entity.User;
import com.example.springhw32.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/posts/comments")
public class CommentController {
private final CommentService commentService;
@PostMapping("")
public CommentDto createComments(@ModelAttribute CommentDto commentDto,Long userId, Long postId){

return commentService.createComments(commentDto,userId,postId);
}

@GetMapping("/{postId}")
public List<CommentDto> getCommentsByPostId(@PathVariable Long postId){
return commentService.getCommentsByPostId(postId);

}



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

import com.example.springhw32.dto.CommentDto;
import com.example.springhw32.dto.PostDto;
import com.example.springhw32.dto.UserDto;
import com.example.springhw32.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

@PostMapping("")
public PostDto create(Long userId,
@ModelAttribute PostDto postDto){
return postService.create(userId,postDto);
}

@GetMapping("")
public List<PostDto> getRecentPosts(){

return postService.getRecentPosts();

}

@GetMapping("/{writer}")
public List<PostDto> getPostsByWriter(@PathVariable("writer")String nickName){
return postService.getPostsByWriter(nickName);


}

@GetMapping("/comments")
public List<PostDto> getPopularPosts(){

return postService.getPopularPosts();
}




}
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
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.RestController;
@RequiredArgsConstructor
@RestController
public class UserController {
private final UserService userService;
@PostMapping("/users")
public UserDto join(@ModelAttribute UserDto userDto){
return userService.join(userDto);
}



}
24 changes: 24 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,29 @@
package com.example.springhw32.dto;

import com.example.springhw32.entity.Comment;
import com.example.springhw32.entity.Post;
import com.example.springhw32.entity.User;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CommentDto {
public CommentDto(Comment comment) {
this.post = comment.getPost();
this.user = comment.getUser();
this.text = comment.getText();


}

@ManyToOne
private Post post;

@OneToOne
private User user;

private String text;

}
27 changes: 27 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,32 @@
package com.example.springhw32.dto;

import com.example.springhw32.entity.Post;
import com.example.springhw32.entity.User;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
public class PostDto {
public PostDto(Post post) {
this.user = post.getUser();
this.title = post.getTitle();
this.content = post.getContent();
this.commentCnt = post.getCommentCnt();
this.createdAt = post.getCreatedAt();

}
@ManyToOne
private User user;

private String title;
private String content;
private Long commentCnt;
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;
private String nickname;

}
19 changes: 19 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,24 @@
package com.example.springhw32.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.IdGeneratorType;
@Getter
@Setter
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne
private Post post;

@OneToOne
private User user;

private String text;


}
20 changes: 20 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,25 @@
package com.example.springhw32.entity;

import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne
private User user;

private String title;
private String content;

private Long commentCnt;
private LocalDateTime createdAt;

}
17 changes: 17 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,22 @@
package com.example.springhw32.entity;

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

@Getter
@Setter
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String username;
private String password;
private String nickname;

}
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 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> findAllByPostId(Long postId);

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

public interface PostRepository {
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 PostRepository extends JpaRepository<Post,Long> {
List<Post> findAllByOrderByCreatedAtDesc();
List<Post> findAllByUserId(Long userId);
List<Post> findAllByOrderByCommentCntDesc();
}
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> {
User findByNickname(String nickname);

}
45 changes: 45 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,50 @@
package com.example.springhw32.service;

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

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

@RequiredArgsConstructor
@Service
@Transactional
public class CommentService {
private final CommentRepository commentRepository;
private final UserRepository userRepository;
private final PostRepository postRepository;

//댓글 작성
public CommentDto createComments(CommentDto commentDto, Long userId, Long postId){
User user = userRepository.findById(userId).orElseThrow();
Post post = postRepository.findById(postId).orElseThrow();

Comment comment = new Comment();
comment.setText(commentDto.getText());
comment.setPost(post);
comment.setUser(user);
commentRepository.save(comment);

return commentDto;

}
//특정게시물의 모든댓글 조회
public List<CommentDto> getCommentsByPostId(Long postId){
return commentRepository.findAllByPostId(postId).stream()
.map(CommentDto::new)
.collect(Collectors.toList());


}

}
Loading