content: package com.ahastudio.api.rest.demo.controllers;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.springframework.http.HttpStatus;
  import org.springframework.web.bind.annotation.DeleteMapping;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PatchMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.PostMapping;
  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 com.ahastudio.api.rest.demo.dtos.PostDto;
  import com.ahastudio.api.rest.demo.exceptions.PostNotFound;
  
  @RestController
  @RequestMapping("/posts")
  public class PostController {
      private List<PostDto> postDtos = new ArrayList(List.of(
              new PostDto("1", "제목", "테스트입니다"),
              new PostDto("2", "2등", "2등이다!")
      ));
  
      @GetMapping
      public List<PostDto> list() {
          return postDtos;
      }
  
      @GetMapping("/{id}")
      public PostDto detail(@PathVariable String id) {
          PostDto postDto = new PostDto(id, "제목", "테스트입니다");
  
          return postDto;
      }
  
      @PostMapping
      @ResponseStatus(HttpStatus.CREATED)
      public PostDto create(@RequestBody PostDto postDto) {
          postDto.setId("1004");
  
          postDtos.add(postDto);
  
          return postDto;
      }
  
      @PatchMapping("/{id}")
      public PostDto update(@PathVariable String id,
                            @RequestBody PostDto postDto) {
          postDto.setId(id);
  
          return postDto;
      }
  
      @DeleteMapping("/{id}")
      public PostDto delete(@PathVariable String id) {
          PostDto postDto = postDtos.stream()
                  .filter(i -> i.getId().equals(id))
                  .findFirst()
                  .get();
  
          postDtos.remove(postDto);
  
          return postDto;
      }
  
      @ExceptionHandler(PostNotFound.class)
      @ResponseStatus(HttpStatus.NOT_FOUND)
      public String postNotFound() {
          return "게시물을 찾을 수 없습니다\n";
      }
  }
  