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

Post crud task #41

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
38b2bac
feat: PostCRUD
chanmin97 Apr 4, 2024
e764e2e
refactor: fix testcode
chanmin97 Apr 7, 2024
ef5c233
merge
chanmin97 Apr 7, 2024
a020322
feat: PostController updatePost
chanmin97 Apr 7, 2024
ead0646
feat: PostService updatePost
chanmin97 Apr 7, 2024
8d2f19d
Test: updatePost
chanmin97 Apr 7, 2024
754fe55
:sparkles: [Add] GlobalExceptionHandler 추가
chanmin97 Apr 10, 2024
f14c0d3
:white_check_mark: [Test] PostServiceTest 추가
chanmin97 Apr 10, 2024
2e75a22
:white_check_mark: [Test] PostControllerTest 수정
chanmin97 Apr 10, 2024
f5ef79b
:recycle: Refactor: Post
chanmin97 Apr 15, 2024
585be82
:recycle: Refactor: PostReply
chanmin97 Apr 15, 2024
f90269d
:sparkles: Add: PostResponse
chanmin97 Apr 15, 2024
d6efc0f
:sparkles: Add: AddPostRequest
chanmin97 Apr 15, 2024
fce47ee
:sparkles: Add: UpdatePostRequest
chanmin97 Apr 15, 2024
2253c2a
:sparkles: Add: PostService
chanmin97 Apr 15, 2024
681cee9
:sparkles: Add: PostController
chanmin97 Apr 15, 2024
5a97f47
:sparkles: Add: Post
chanmin97 Apr 15, 2024
f5f5f80
:sparkles: Add: PostApiControllerTest
chanmin97 Apr 15, 2024
9e1da2e
:sparkles: Add: PostApiController
chanmin97 Apr 15, 2024
056b3b7
:sparkles: Add: PostApiControllerTest
chanmin97 Apr 15, 2024
9dac3a8
:sparkles: Add: PostReply
chanmin97 Apr 15, 2024
47d0df2
:sparkles: Add: PostRepository
chanmin97 Apr 15, 2024
aaf1eaf
:sparkles: Add: PostServiceTest.java
chanmin97 Apr 15, 2024
1cfe8cf
:fire: Delete : PostServiceTest
chanmin97 Apr 16, 2024
35f76a8
:fire: Delete: PostControllerTest
chanmin97 Apr 16, 2024
7d30459
:fire: Delete: User
chanmin97 Apr 16, 2024
c159778
:twisted_rightwards_arrows: Merge Main
chanmin97 Apr 16, 2024
a296d96
:twisted_rightwards_arrows: Merge Main
chanmin97 Apr 16, 2024
f8f28c5
:white_check_mark: Test: PostApiControllerTest
chanmin97 Apr 16, 2024
c416a97
:white_check_mark: Test: PostApiControllerTest생성예외
chanmin97 Apr 22, 2024
56843fb
:sparkles: Feat : 로깅 및 Post컨트롤러 태그 추가
chanmin97 May 4, 2024
90bf54b
:sparkles: Feat : 페이징 기능 구현
chanmin97 May 4, 2024
cba91dd
:white_check_mark: Test : 페이징 기능 테스트코드 추가
chanmin97 May 4, 2024
9def7a0
Comment: 서비스코드 주석추가
chanmin97 May 8, 2024
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
@@ -0,0 +1,53 @@
package com.ssafy.springbootapi.domain.post.api;

import com.ssafy.springbootapi.domain.post.application.PostService;
import com.ssafy.springbootapi.domain.post.domain.Post;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Tag(name = "Post", description = "Post API 입니다.")
@RestController
@RequestMapping("/api/v1/posts")
public class PostController {
private final PostService postService;

@Autowired
public PostController(PostService postService) {
this.postService = postService;
}

@GetMapping
public ResponseEntity<List<Post>> getAllPosts() {
List<Post> posts = postService.getAllPosts();
return new ResponseEntity<>(posts, HttpStatus.OK);
}

@GetMapping("/{id}")
public ResponseEntity<Post> getPostById(@PathVariable Long id) {
return postService.getPostById(id)
.map(post -> new ResponseEntity<>(post, HttpStatus.OK))
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

@PostMapping
public ResponseEntity<Post> createPost(@RequestBody Post post) {
Post createdPost = postService.savePost(post);
return new ResponseEntity<>(createdPost, HttpStatus.CREATED);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deletePost(@PathVariable Long id) {
postService.deletePost(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@PutMapping("/{id}")
public ResponseEntity<Post> updatePost(@PathVariable Long id, @RequestBody Post postDetails) {
Post updatedPost = postService.updatePost(id, postDetails);
return new ResponseEntity<>(updatedPost, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ssafy.springbootapi.domain.post.application;

import com.ssafy.springbootapi.domain.post.dao.PostRepository;
import com.ssafy.springbootapi.domain.post.domain.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class PostService {
private final PostRepository postRepository;

@Autowired
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}

public List<Post> getAllPosts() {
return postRepository.findAll();
}

public Optional<Post> getPostById(Long id) {
return postRepository.findById(id);
}

public Post savePost(Post post) {
return postRepository.save(post);
}

public void deletePost(Long id) {
postRepository.deleteById(id);
}

public Post updatePost(Long id, Post postDetails) {
Post post = postRepository.findById(id)
.orElseThrow(() -> new RuntimeException());

post.setContents(postDetails.getContents());

return postRepository.save(post);
}
Copy link

Choose a reason for hiding this comment

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

팀원의 업데이트 방식과 상이합니다. 경호씨는 dirty check 사용하셨어요!


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ssafy.springbootapi.domain.post.dao;

import com.ssafy.springbootapi.domain.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.ssafy.springbootapi.domain.post.domain;


import com.ssafy.springbootapi.domain.user.domain.User;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Entity
@Getter @Setter
public class Post {
@Id
@GeneratedValue
Copy link

Choose a reason for hiding this comment

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

ID 생성 전략이 기본 값으로 돼 있어서 MySQL을 사용하신다면 sequence 전략일 겁니다.
경호씨는 다른 전략 사용하셨던데, 테이블마다 전략이 다른게 아니라면 맞춰주세요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

시정하겠습니다 !! 대가리 박겠습니다!!

private Long id;

private String contents;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

private Long likes;

private Long dislikes;

private Long views;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

public Post(long l, String firstPost, LocalDateTime now, LocalDateTime now1, long l1, long l2, long l3, User user) {
}


public Post() {

}
Copy link
Contributor

Choose a reason for hiding this comment

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

생성자가 왜 비워져있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

수정완료

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ssafy.springbootapi.domain.post.domain;

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

import java.time.LocalDateTime;

@Entity
@Getter @Setter
public class PostReply {
@Id
@GeneratedValue
private Long id;

private String contents;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

private Long likes;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

dto가 있는데 왜 entity로 postReply가 있는건가요?

Copy link
Contributor

Choose a reason for hiding this comment

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

경호가 잘못봤대요

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ssafy.springbootapi.domain.post.dto;

import lombok.Getter;
import lombok.Setter;

import java.sql.Date;

@Getter @Setter
public class Post {
private Long id;
private String contents;
private Date createdAt;
private Date updatedAt;
private Long likes;
private Long dislikes;
private Long views;
private Long userId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ssafy.springbootapi.domain.post.dto;

import lombok.Getter;
import lombok.Setter;

import java.sql.Date;


@Getter @Setter
public class PostReply {
private Long id;
private String contents;
private Date createdAt;
private Date updatedAt;
private Long likes;
private Long postId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ssafy.springbootapi.domain.user.domain;

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

@Entity
@Getter
@Setter
public class User {
@Id
@GeneratedValue
private Long id;

private String username;


// 생성자, getter, setter 등은 생략
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ssafy.springbootapi.global.error;

import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleRuntimeException(RuntimeException ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("서버 내부 오류가 발생했습니다.");
}

@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<String> handleEntityNotFoundException(EntityNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("존재하지 않습니다.");
}



}
Loading