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,27 @@
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.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/posts")
public class CommentController {
private final CommentService commentService;

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

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

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

import java.util.List;

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

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

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

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

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

public class UserController {
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;

@RestController
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
//회원가입
@PostMapping("/users")
public UserDto signup(@ModelAttribute UserDto userDto) {
return userService.signup(userDto);
}
}
8 changes: 7 additions & 1 deletion 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;

public class CommentDto {
import com.example.springhw32.entity.Post;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CommentDto {
private String text;
}
13 changes: 12 additions & 1 deletion src/main/java/com/example/springhw32/dto/PostDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package com.example.springhw32.dto;

public class PostDto {
import com.example.springhw32.entity.User;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
public class PostDto {
private String title;
private String content;
private LocalDateTime createdAt;
private int commentNumber;
}
8 changes: 7 additions & 1 deletion src/main/java/com/example/springhw32/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.example.springhw32.dto;

public class UserDto {
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UserDto {
private String username;
private String password;
}
16 changes: 16 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,21 @@
package com.example.springhw32.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.apache.catalina.users.GenericRole;

@Entity
@Getter
@Setter
public class Comment {

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

@ManyToOne
private Post post;

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

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Entity
@Getter
@Setter
public class Post {

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

@ManyToOne
private User user;

@Column(nullable = false)
private String title;

@Column(nullable = false)
private String content;

private int commentNumber;

private LocalDateTime createdAt;
}
16 changes: 16 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,21 @@
package com.example.springhw32.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class User {

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

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

@Column(nullable = false)
private String password;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.example.springhw32.repository;

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

import java.util.List;

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

public interface PostRepository {
import com.example.springhw32.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface PostRepository extends JpaRepository<Post, Long> {
public List<Post> findAllByOrderByCreatedAtDesc();
public List<Post> findAllByUserId(Long userId);
public List<Post> findAllByOrderByCommentNumberDesc();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.example.springhw32.repository;

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

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
}
42 changes: 42 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,47 @@
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 java.util.List;
import java.util.stream.Collectors;

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

//댓글 작성
public CommentDto addComment(CommentDto commentDto, Long postId) {
Post post = postRepository.findById(postId).orElseThrow();

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

post.setCommentNumber(post.getCommentNumber() + 1);
postRepository.save(post);

return commentDto;
}

//특정 개시물에 달린 모든 댓글 조회
public List<CommentDto> getCommentByPostId(Long postId) {
List<CommentDto> commentDtos = commentRepository.findAllByPostId(postId).stream()
.map(this::EntityToDto).collect(Collectors.toList());
return commentDtos;
}

private CommentDto EntityToDto(Comment comment) {
CommentDto commentDto = new CommentDto();
commentDto.setText(comment.getText());
return commentDto;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/example/springhw32/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
package com.example.springhw32.service;

import com.example.springhw32.dto.PostDto;
import com.example.springhw32.entity.Post;
import com.example.springhw32.repository.PostRepository;
import com.example.springhw32.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

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

@Service
@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
private final UserRepository userRepository;

//글 작성
public PostDto save(PostDto postDto, Long userId) {
Post post = new Post();
post.setTitle(postDto.getTitle());
post.setContent(postDto.getContent());
post.setUser(userRepository.findById(userId).orElse(null));
post.setCommentNumber(0);
post.setCreatedAt(LocalDateTime.now());
postRepository.save(post);
return postDto;
}

//최신순으로 글 조회
public List<PostDto> findAllByOrderByCreatedAtDesc() {
List<PostDto> postDtos = postRepository.findAllByOrderByCreatedAtDesc().stream()
.map(this::EntityToDto).collect(Collectors.toList());
return postDtos;
}

//특정 회원이 작성한 글 조회
public List<PostDto> findAllByWriter(Long userId) {
List<PostDto> postDtos = postRepository.findAllByUserId(userId).stream()
.map(this::EntityToDto).collect(Collectors.toList());
return postDtos;
}

//댓글이 많은 순으로 정렬 조회
public List<PostDto> findAllByOrderByCommentNumberDesc() {
List<PostDto> postDtos = postRepository.findAllByOrderByCommentNumberDesc().stream()
.map(this::EntityToDto).collect(Collectors.toList());
return postDtos;
}

private PostDto EntityToDto(Post post) {
PostDto postDto = new PostDto();
postDto.setTitle(post.getTitle());
postDto.setContent(post.getContent());
postDto.setCreatedAt(post.getCreatedAt());
postDto.setCommentNumber(post.getCommentNumber());
return postDto;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/springhw32/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package com.example.springhw32.service;

import com.example.springhw32.dto.UserDto;
import com.example.springhw32.entity.User;
import com.example.springhw32.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
//회원가입
public UserDto signup(UserDto userDto) {
User user = new User();
user.setUsername(userDto.getUsername());
user.setPassword(userDto.getPassword());
userRepository.save(user);
return userDto;
}

}
6 changes: 3 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
username: your_username
password: your_password
url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: na58745874@
driver-class-name: com.mysql.cj.jdbc.Driver

jpa:
Expand Down