-
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.
- Loading branch information
1 parent
866208e
commit 5f2c536
Showing
5 changed files
with
6,249 additions
and
2,933 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
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,75 @@ | ||
package kr.megaptera.assignment.controllers; | ||
|
||
import kr.megaptera.assignment.dtos.CommentDto; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
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> getComments( | ||
@RequestParam Long postId | ||
) { | ||
System.out.println("getComments for postId:" + postId); | ||
return commentDtos.stream() | ||
.filter(commentDto -> commentDto.getPostId().equals(postId)) | ||
.toList(); | ||
} | ||
|
||
@PostMapping("/") | ||
public CommentDto createComment( | ||
@RequestParam Long postId, | ||
CommentDto commentDto | ||
) { | ||
System.out.println("createComment for postId:" + postId); | ||
commentDto.setComment( | ||
newId++, | ||
postId, | ||
commentDto.getContent() | ||
); | ||
commentDtos.add(commentDto); | ||
return commentDto; | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public CommentDto updateComment( | ||
@PathVariable Long id, | ||
CommentDto commentDto | ||
) { | ||
System.out.println("updateComment for id:" + id); | ||
|
||
for (CommentDto comment : commentDtos) { | ||
if (comment.getId().equals(id)) { | ||
comment.setComment( | ||
commentDto.getId(), | ||
commentDto.getPostId(), | ||
commentDto.getContent() | ||
); | ||
return comment; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void deleteComment( | ||
@PathVariable Long id | ||
) { | ||
System.out.println("deleteComment for id:" + id); | ||
|
||
for (CommentDto comment : commentDtos) { | ||
if (comment.getId().equals(id)) { | ||
commentDtos.remove(comment); | ||
return; | ||
} | ||
} | ||
} | ||
} |
60 changes: 59 additions & 1 deletion
60
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,70 @@ | ||
package kr.megaptera.assignment.controllers; | ||
|
||
import kr.megaptera.assignment.dtos.PostDto; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
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> getPosts() { | ||
System.out.println("getPosts"); | ||
return postDtos; | ||
} | ||
|
||
@PostMapping() | ||
public PostDto createPost(PostDto postDto) { | ||
System.out.println("createPost"); | ||
postDto.setPost( | ||
newId++, | ||
postDto.getTitle(), | ||
postDto.getContent() | ||
); | ||
postDtos.add(postDto); | ||
return postDto; | ||
} | ||
|
||
@PutMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public PostDto updatePost( | ||
@PathVariable Long id, | ||
PostDto postDto | ||
) { | ||
System.out.println("updatePost"); | ||
|
||
for (PostDto post : postDtos) { | ||
if (post.getId().equals(id)) { | ||
post.setPost( | ||
postDto.getId(), | ||
postDto.getTitle(), | ||
postDto.getContent() | ||
); | ||
return post; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void deletePost( | ||
@PathVariable Long id | ||
) { | ||
System.out.println("deletePost"); | ||
|
||
for (PostDto post : postDtos) { | ||
if (post.getId().equals(id)) { | ||
postDtos.remove(post); | ||
return; | ||
} | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
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,40 @@ | ||
package kr.megaptera.assignment.dtos; | ||
|
||
public class CommentDto { | ||
private Long id; | ||
private Long postId; | ||
private String content; | ||
|
||
public CommentDto( | ||
Long id, | ||
Long postId, | ||
String content | ||
) { | ||
this.id = id; | ||
this.postId = postId; | ||
this.content = content; | ||
} | ||
|
||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public Long getPostId() { | ||
return this.postId; | ||
} | ||
|
||
public String getContent() { | ||
return this.content; | ||
} | ||
|
||
// setter | ||
public void setComment( | ||
Long id, | ||
Long postId, | ||
String content | ||
) { | ||
this.id = id; | ||
this.postId = postId; | ||
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,40 @@ | ||
package kr.megaptera.assignment.dtos; | ||
|
||
public class PostDto { | ||
private Long id; | ||
private String title; | ||
private String content; | ||
|
||
public PostDto( | ||
Long id, | ||
String title, | ||
String content | ||
) { | ||
this.id = id; | ||
this.title = title; | ||
this.content = content; | ||
} | ||
|
||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public String getTitle() { | ||
return this.title; | ||
} | ||
|
||
public String getContent() { | ||
return this.content; | ||
} | ||
|
||
// setter | ||
public void setPost( | ||
Long id, | ||
String title, | ||
String content | ||
) { | ||
this.id = id; | ||
this.title = title; | ||
this.content = content; | ||
} | ||
} |
Oops, something went wrong.