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

feat : 2023-10-09 메가테라 3주차 백엔드 DTO 및 CORS 보안 문제 해결 #32

Merged
merged 2 commits into from
Oct 10, 2023
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
17 changes: 17 additions & 0 deletions src/main/java/kr/megaptera/assignment/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kr.megaptera.assignment;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 모든 요청 경로에 대해서
.allowedOrigins("http://localhost:8000") // 허용할 오리진 설정
.allowedMethods("*") // 허용할 HTTP 메서드 설정. 예: GET, POST, PUT, DELETE 등
.maxAge(3600); // preflight 요청의 결과를 1시간 동안 캐싱
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
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<>();

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

return commentDtoList;
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public String create(@RequestParam String postId, @RequestBody CommentDto commentDto
) {
CommentDto AddedIdCommentDto = new CommentDto(createId(),postId, commentDto.content());
commentDtos.add(AddedIdCommentDto);

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.postId().equals(postId) && i.id().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.postId().equals(postId) && i.id().equals(id))
.findFirst()
.get();

commentDtos.remove(commentDto);
}

//정적 팩토리 메서드 구현
private String createId() {
newId++;
return newId.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
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<>();
}

@GetMapping
public List<PostDto> listPost() {
return postDtos.stream().toList();
}

@GetMapping("/{id}")
public PostDto getPost(@PathVariable String id) {
return postDtos.stream().filter(item -> item.id().equals(id)).findFirst().orElse(null);

}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public String createPost(@RequestBody PostDto postRequestDto) {
PostDto AddedIdPostDto = new PostDto(createId(),postRequestDto.title(),postRequestDto.content());
postDtos.add(AddedIdPostDto);
return "Complete!";
}

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

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deletePost(@PathVariable String id) {
PostDto postDto = postDtos.stream().filter(item -> item.id().equals(id)).findFirst().orElse(null);
if (postDto != null) {
postDtos.remove(postDto);
}
}

//정적 팩토리 메서드 구현
private String createId() {
newId++;
return newId.toString();
}
}
2 changes: 1 addition & 1 deletion src/main/java/kr/megaptera/assignment/dtos/CommentDto.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.megaptera.assignment.dtos;

public class CommentDto {
public record CommentDto (String id, String postId, String content){
}
5 changes: 4 additions & 1 deletion src/main/java/kr/megaptera/assignment/dtos/PostDto.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package kr.megaptera.assignment.dtos;

public class PostDto {
public record PostDto (String id,
String title,
String content
){
}
Loading
Loading