-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Post crud task #41
Changes from 9 commits
38b2bac
e764e2e
ef5c233
a020322
ead0646
8d2f19d
754fe55
f14c0d3
2e75a22
f5ef79b
585be82
f90269d
d6efc0f
fce47ee
2253c2a
681cee9
5a97f47
f5f5f80
9e1da2e
056b3b7
9dac3a8
47d0df2
aaf1eaf
1cfe8cf
35f76a8
7d30459
c159778
a296d96
f8f28c5
c416a97
56843fb
90bf54b
cba91dd
9def7a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
|
||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ID 생성 전략이 기본 값으로 돼 있어서 MySQL을 사용하신다면 sequence 전략일 겁니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생성자가 왜 비워져있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dto가 있는데 왜 entity로 postReply가 있는건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("존재하지 않습니다."); | ||
} | ||
|
||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
팀원의 업데이트 방식과 상이합니다. 경호씨는 dirty check 사용하셨어요!