Skip to content
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

[10기 백철현] 3주차 과제 - DTO를 이용한 게시판 REST API 만들기 #45

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comments")
public class CommentController {
private Long newId = 0L;

private List<CommentDto> commentDtos = new ArrayList<>();

private String generateId() {
newId += 1;
return newId.toString();
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public String create(@RequestBody CommentDto commentDto,
@RequestParam String postId) {
commentDto.setId(generateId());
commentDto.setPostId(postId);

commentDtos.add(commentDto);

return "Complete!";
}

@GetMapping
public List<CommentDto> list(@RequestParam String postId) {
List<CommentDto> commentDtoList = commentDtos.stream()
.filter(i -> i.getId().equals(postId))
.toList();
return commentDtoList;
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(@RequestParam String postId,
@PathVariable String id,
@RequestBody CommentDto commentDto) {

commentDtos = commentDtos.stream()
.map(i -> i.getPostId().equals(postId) && i.getId().equals(id) ? commentDto : i)
.collect(Collectors.toList());
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@RequestParam String postId,
@PathVariable String id) {

CommentDto commentDto = commentDtos.stream()
.filter(i -> i.getPostId().equals(postId) && i.getId().equals(id))
.findFirst()
.get();

commentDtos.remove(commentDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
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;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/posts")
public class PostController {
private Long newId = 0L;

private List<PostDto> postDtos = new ArrayList<>();

private String generateId() {
newId += 1;
return newId.toString();
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public String create(@RequestBody PostDto postDto) {
postDto.setId(generateId());
postDtos.add(postDto);

return "Complete!";
}

@GetMapping
public List<PostDto> list() {
return postDtos;
}

@GetMapping("/{id}")
public PostDto detail(@PathVariable String id) {
PostDto postDto = postDtos.stream()
.filter(i -> i.getId().equals(id))
.findFirst()
.get();

return postDto;
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(@PathVariable String id, @RequestBody PostDto postDto) {
postDtos = postDtos.stream()
.map(i -> i.getId().equals(id) ? postDto : i)
.collect(Collectors.toList());
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable String id) {
PostDto postDto = postDtos.stream()
.filter(i -> i.getId().equals(id))
.findFirst()
.get();

postDtos.remove(postDto);
}
}
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 String id;
private String postId;
private String content;

public CommentDto() {
}

public CommentDto(String id, String postId, String content) {
this.id = id;
this.postId = postId;
this.content = content;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getPostId() {
return postId;
}

public void setPostId(String postId) {
this.postId = postId;
}

public String getContent() {
return content;
}

public void setContent(String content) {
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 String title;
private String content;
private String id;

public PostDto() {
}

public PostDto(String title, String content, String id) {
this.title = title;
this.content = content;
this.id = id;
}

public String getTitle() {
return title;
}

public String getContent() {
return content;
}

public String getId() {
return id;
}

public void setTitle(String title) {
this.title = title;
}

public void setContent(String content) {
this.content = content;
}

public void setId(String id) {
this.id = id;
}
}
Loading
Loading