-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from LIG-JY/LIG-JY
[백엔드 9기 이재연] 3주차 과제 제출
- Loading branch information
Showing
6 changed files
with
6,248 additions
and
2,935 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/kr/megaptera/assignment/controllers/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,64 @@ | ||
package kr.megaptera.assignment.controllers; | ||
|
||
import kr.megaptera.assignment.dtos.CommentDto; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/comments") | ||
public class CommentController { | ||
private Long newId = 0L; | ||
|
||
private List<CommentDto> commentDtos = new ArrayList<>(); | ||
|
||
@GetMapping() | ||
public List<CommentDto> getCommentList(@RequestParam String postId) { | ||
return this.commentDtos.stream() | ||
.filter(comment -> comment.getPostId().equals(postId)) | ||
.toList(); | ||
} | ||
|
||
@PostMapping() | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public String createComment(@RequestParam String postId, @RequestBody CommentDto commentDto) { | ||
commentDto.setId(String.valueOf(newId++)); | ||
commentDto.setPostId(postId); | ||
this.commentDtos.add(commentDto); | ||
return "Complete!"; | ||
} | ||
|
||
@PutMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void updateComment(@PathVariable String id, | ||
@RequestParam String postId, | ||
@RequestBody CommentDto commentDto) { | ||
this.commentDtos.stream() | ||
.filter(comment -> comment.getId().equals(id)) | ||
.findFirst() | ||
.ifPresent(comment -> { | ||
comment.setId(id); | ||
comment.setContent(commentDto.getContent()); | ||
comment.setPostId(postId); | ||
}); | ||
|
||
} | ||
|
||
|
||
@DeleteMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void deleteComment(@PathVariable String id) { | ||
this.commentDtos.removeIf(comment -> comment.getId().equals(id)); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/kr/megaptera/assignment/controllers/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,63 @@ | ||
package kr.megaptera.assignment.controllers; | ||
|
||
import kr.megaptera.assignment.dtos.PostDto; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/posts") | ||
public class PostController { | ||
private Long newId = 0L; | ||
|
||
private List<PostDto> postDtos = new ArrayList<>(); | ||
|
||
@GetMapping | ||
public List<PostDto> getPostList() { | ||
return this.postDtos; | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public PostDto getPost(@PathVariable String id) { | ||
return this.postDtos.stream() | ||
.filter(postDto -> postDto.getId().equals(id)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public String createPost(@RequestBody PostDto postDto) { | ||
postDto.setId(String.valueOf(newId++)); | ||
this.postDtos.add(postDto); | ||
return "Complete!"; | ||
} | ||
|
||
@PutMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void updatePost(@PathVariable String id, @RequestBody PostDto postDto) { | ||
this.postDtos.stream().filter(post -> post.getId().equals(id)) | ||
.findFirst() | ||
.ifPresent(post -> { | ||
post.setId(postDto.getId()); | ||
post.setTitle(postDto.getTitle()); | ||
post.setContent(postDto.getContent()); | ||
}); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void deletePost(@PathVariable String id) { | ||
this.postDtos.removeIf(postDto -> postDto.getId().equals(id)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/kr/megaptera/assignment/dtos/CommentDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,39 @@ | ||
package kr.megaptera.assignment.dtos; | ||
|
||
public class CommentDto { | ||
|
||
// id | ||
private String id; | ||
// postId | ||
private String postId; | ||
// content | ||
private String content; | ||
|
||
// JavaBeans convention : 기본 생성자 | ||
public CommentDto() { | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getPostId() { | ||
return postId; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setPostId(String postId) { | ||
this.postId = postId; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,39 @@ | ||
package kr.megaptera.assignment.dtos; | ||
|
||
public class PostDto { | ||
|
||
// id | ||
private String id; | ||
// title | ||
private String title; | ||
// content | ||
private String content; | ||
|
||
// JavaBeans convention : 기본 생성자 | ||
public PostDto() { | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
} |
Oops, something went wrong.