Skip to content

Commit

Permalink
과제 3
Browse files Browse the repository at this point in the history
  • Loading branch information
smilejakdu committed Nov 18, 2023
1 parent 866208e commit 5f2c536
Show file tree
Hide file tree
Showing 5 changed files with 6,249 additions and 2,933 deletions.
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;
}
}
}
}
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 src/main/java/kr/megaptera/assignment/dtos/CommentDto.java
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;
}
}
36 changes: 36 additions & 0 deletions src/main/java/kr/megaptera/assignment/dtos/PostDto.java
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;
}
}
Loading

0 comments on commit 5f2c536

Please sign in to comment.