[何か作ろうか…]Postに関連するCRUD APIを作成します.3)


コントローラとサービスの作成


パッケージの管理



PostServiceの作成

package com.momenting.todosomething.service.post;

import com.momenting.todosomething.domain.post.Post;
import com.momenting.todosomething.domain.post.PostRepository;
import com.momenting.todosomething.dto.PostListResponseDto;
import com.momenting.todosomething.dto.PostResponseDto;
import com.momenting.todosomething.dto.PostSaveRequestDto;
import com.momenting.todosomething.dto.PostUpdateRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@RequiredArgsConstructor
@Service
public class PostService {
    private final PostRepository postRepository;

    public PostResponseDto findById (Long id) {
        Post post = postRepository.findById(id).orElseThrow(() ->
                new IllegalArgumentException("해당게시글이 없습니다. id=" + id));
        return  new PostResponseDto(post);
    }

    @Transactional(readOnly = true)
    public List<PostListResponseDto> findAllDesc() {
        return postRepository.findAllDesc().stream().map(PostListResponseDto::new).collect(Collectors.toList());
    }

    @Transactional
    public Long save(PostSaveRequestDto requestDto) {
        return postRepository.save(requestDto.toEntity()).getId();
    }

    @Transactional
    public Long update(Long id, PostUpdateRequestDto requestDto) {
        Post post = postRepository.findById(id).orElseThrow( () ->
                new IllegalArgumentException("해당게시글이 없습니다. id=" + id));

        post.update(requestDto.getPcontent(), requestDto.getSecret());

        return id;
    }

    @Transactional
    public void delete(Long id) {
        Post post = postRepository.findById(id).orElseThrow( () ->
                new IllegalArgumentException("해당게시글이 없습니다, id=" +id));
        postRepository.delete(post);
    }

}

PostApiコントローラの作成

package com.momenting.todosomething.controller;

import com.momenting.todosomething.domain.post.Post;
import com.momenting.todosomething.dto.PostListResponseDto;
import com.momenting.todosomething.dto.PostResponseDto;
import com.momenting.todosomething.dto.PostSaveRequestDto;
import com.momenting.todosomething.dto.PostUpdateRequestDto;
import com.momenting.todosomething.service.post.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@RestController
public class PostApiController {

    private  final PostService postService;

    @GetMapping("post/{id}")
    public PostResponseDto findById(@PathVariable Long id) {
        return postService.findById(id);
    }
    

    @GetMapping("post/all")
    public List<PostListResponseDto> getFindAll() {
        return  postService.findAllDesc();
    }

    @PostMapping("post")
    public Long save(@RequestBody PostSaveRequestDto requestDto) {
        return  postService.save(requestDto);
    }

    @PutMapping("post/{id}")
    public Long update(@PathVariable Long id, @RequestBody PostUpdateRequestDto requestDto) {
        return  postService.update(id, requestDto);
    }

    @DeleteMapping("post/{id}")
    public Long delete(@PathVariable Long id) {
        postService.delete(id);
        return id;
    }
}