Skip to content

Commit

Permalink
Merge pull request #41 from LIG-JY/LIG-JY
Browse files Browse the repository at this point in the history
[백엔드 9기 이재연] 3주차 과제 제출
  • Loading branch information
bbhye1 authored Jan 2, 2024
2 parents 866208e + 2e65c75 commit 07bf4ad
Show file tree
Hide file tree
Showing 6 changed files with 6,248 additions and 2,935 deletions.
20 changes: 17 additions & 3 deletions src/main/java/kr/megaptera/assignment/AssignmentApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@

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 {

public static void main(String[] args) {
SpringApplication.run(AssignmentApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(AssignmentApplication.class, args);
}

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:8000")
.allowedMethods("GET", "POST", "PUT", "DELETE");
}
};
}
}
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));
}
}
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 src/main/java/kr/megaptera/assignment/dtos/CommentDto.java
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;
}
}
35 changes: 35 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,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;
}
}
Loading

0 comments on commit 07bf4ad

Please sign in to comment.