-
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 1 commit
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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); | ||
} | ||
} |
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 | ||
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 PostDto { | ||
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 PostReplyDto { | ||
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,96 @@ | ||
package com.ssafy.springbootapi.domain.post.api; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.ssafy.springbootapi.domain.post.application.PostService; | ||
import com.ssafy.springbootapi.domain.post.domain.Post; | ||
|
||
import com.ssafy.springbootapi.domain.user.domain.User; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.times; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class PostControllerTest { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Mock | ||
private PostService postService; | ||
|
||
@InjectMocks | ||
private PostController postController; | ||
|
||
@Test | ||
void getAllPosts() throws Exception { | ||
when(postService.getAllPosts()).thenReturn(Arrays.asList( | ||
new Post(1L, "First post", LocalDateTime.now(), LocalDateTime.now(), 0L, 0L, 0L, new User()), | ||
new Post(2L, "Second post", LocalDateTime.now(), LocalDateTime.now(), 0L, 0L, 0L, new User()) | ||
)); | ||
|
||
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(postController).build(); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/posts")) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString(Arrays.asList( | ||
new Post(1L, "First post", LocalDateTime.now(), LocalDateTime.now(), 0L, 0L, 0L, new User()), | ||
new Post(2L, "Second post", LocalDateTime.now(), LocalDateTime.now(), 0L, 0L, 0L, new User()) | ||
)))); | ||
} | ||
|
||
@Test | ||
void getPostById() throws Exception { | ||
Long postId = 1L; | ||
Optional<Post> post = Optional.of(new Post(postId, "First post", LocalDateTime.now(), LocalDateTime.now(), 0L, 0L, 0L, new User())); | ||
when(postService.getPostById(postId)).thenReturn(post); | ||
|
||
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(postController).build(); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/posts/{id}", postId)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString(post.get()))); | ||
} | ||
|
||
@Test | ||
void createPost() throws Exception { | ||
Post post = new Post(1L, "New post", LocalDateTime.now(), LocalDateTime.now(), 0L, 0L, 0L, new User()); | ||
|
||
when(postService.savePost(ArgumentMatchers.any(Post.class))).thenReturn(post); | ||
|
||
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(postController) | ||
.build(); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.post("/posts") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(new ObjectMapper().writeValueAsString(post))) // ObjectMapper를 사용하여 객체를 JSON 형식으로 변환 | ||
.andExpect(MockMvcResultMatchers.status().isCreated()) | ||
.andExpect(MockMvcResultMatchers.content().json(new ObjectMapper().writeValueAsString(post))); // ObjectMapper를 사용하여 객체를 JSON 형식으로 변환하여 비교 | ||
} | ||
|
||
|
||
@Test | ||
void deletePost() throws Exception { | ||
Long postId = 1L; | ||
|
||
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(postController).build(); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.delete("/posts/{id}", postId)) | ||
.andExpect(MockMvcResultMatchers.status().isNoContent()); | ||
|
||
verify(postService, times(1)).deletePost(postId); | ||
} | ||
} |
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.
ID 생성 전략이 기본 값으로 돼 있어서 MySQL을 사용하신다면 sequence 전략일 겁니다.
경호씨는 다른 전략 사용하셨던데, 테이블마다 전략이 다른게 아니라면 맞춰주세요!
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.
시정하겠습니다 !! 대가리 박겠습니다!!