Skip to content

Commit

Permalink
Merge pull request #37 from parkmyungkwan/parkmyungkwan
Browse files Browse the repository at this point in the history
3주차 과제
  • Loading branch information
bbhye1 authored Jan 2, 2024
2 parents 866208e + 35c260e commit 56cabc0
Show file tree
Hide file tree
Showing 6 changed files with 11,871 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/main/java/kr/megaptera/assignment/AssignmentApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class AssignmentApplication {
Expand All @@ -10,4 +13,16 @@ public static void main(String[] args) {
SpringApplication.run(AssignmentApplication.class, args);
}

@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS");
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,70 @@

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

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;

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

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

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

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public String create(@RequestParam String postId, @RequestBody CommentDto commentDto) {
commentDto.setId(generateId());
commentDto.setPostId(postId);

commentDtos.add(commentDto);

return "Complete!";
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(@PathVariable String id, @RequestParam String postId, @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);
}

private String generateId() {

newId += 1;
return newId.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,73 @@

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

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;

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

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

@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;
}

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

return "Complete!";
}

@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);
}

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

}
38 changes: 38 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,42 @@
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;
}
}
38 changes: 38 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,42 @@
package kr.megaptera.assignment.dtos;

public class PostDto {
private String id;

private String title;

private String content;

public PostDto() {
}

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

public String getId() {
return id;
}

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

public String getTitle() {
return title;
}

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

public String getContent() {
return content;
}

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

0 comments on commit 56cabc0

Please sign in to comment.