[뭐라도 해야지...] Post관련 CRUD api 작성하기. 3)
22024 단어 ReactSpringboot뭐라도해야지mysqlReact
컨트롤러 및 서비스 작성
패키지 관리
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);
}
}
PostApiController 작성
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;
}
}
Author And Source
이 문제에 관하여([뭐라도 해야지...] Post관련 CRUD api 작성하기. 3)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@dudgns1086/뭐라도-해야지...-Post관련-CRUD-api-작성하기.-3저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)