[TIL] 230709 κ²Œμ‹œκΈ€ CRUD API

CountryGirlΒ·2023λ…„ 7μ›” 10일
0

TIL

λͺ©λ‘ 보기
17/80

사싀 λ‚΄κ°€ 과제λ₯Ό ν•  λ•ŒλŠ” κ°•μ˜λ₯Ό 보고 λ³΅λΆ™ν•˜κ±°λ‚˜ λ‹€λ₯Έ μ‚¬λžŒν•œν…Œ 많이 도움을 λ°›μ•„μ„œ 해결을 ν–ˆλ‹€.

κ·Έλž˜λ„ CRUDλŠ” 기본적으둜 μ•Œμ•„μ•Όν•˜μ§€ μ•ŠλŠ”κ°€....?

이런 κ°„λ‹¨ν•œ CRUD μ •λ„λŠ” κ΅¬ν˜„ν•  수 있게 곡뢀λ₯Ό 살짝 ν–ˆλ‹€!
γ…Ž

πŸ“Œ 3 Layer Architecture

πŸ› οΈ Controller

: ν΄λΌμ΄μ–ΈνŠΈμ˜ μš”μ²­μ„ λ°›λŠ”λ‹€.
: μš”μ²­μ— λŒ€ν•œ 둜직 μ²˜λ¦¬λŠ” Service μ—κ²Œ μ „λ‹¬ν•œλ‹€. (Request κ°€ μžˆλ‹€λ©΄ 같이 Service μ—κ²Œ 보낸닀)
: Service μ—μ„œ 처리 μ™„λ£Œλœ κ²°κ³Όλ₯Ό ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ μ‘λ‹΅ν•œλ‹€.

// Controller -> κ°„λ‹¨ν•˜κ²Œ 

import com.example.post.dto.DeleteResponseDto;
import com.example.post.dto.PostRequestDto;
import com.example.post.dto.PostResponseDto;
import com.example.post.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class PostController {
    private final PostService postService;
    @GetMapping("/post")
    public List<PostResponseDto> getPosts () {
        return postService.getPosts();
    }

    @PostMapping("/post")
    public PostResponseDto createPost(@RequestBody PostRequestDto postRequestDto) {
        return postService.createPost(postRequestDto);
    }

    @PutMapping("/post/{id}")
    public PostResponseDto updatePost(@PathVariable Long id, @RequestBody PostRequestDto postRequestDto) {
        return postService.updatePost(id, postRequestDto);
    }

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

πŸ‘©πŸ»β€πŸ”§ Service

: μ‚¬μš©μž μš”κ΅¬μ‚¬ν•­μ„ μ²˜λ¦¬ν•˜λŠ” μ‹€μ„Έ!
: DB μ €μž₯ 및 μ‘°νšŒκ°€ ν•„μš”ν•  λ•ŒλŠ” Repositoryμ—κ²Œ μš”μ²­ν•œλ‹€.

@Service
@RequiredArgsConstructor
public class PostService {
    private final PostRepository postRepository;
    public List<PostResponseDto> getPosts () {
        return postRepository.findAllByOrderByCreatedAtDesc()
                .stream().map(PostResponseDto::new).toList();
    }

    public PostResponseDto createPost (PostRequestDto postRequestDto) {
        Post post = new Post(postRequestDto);
        postRepository.save(post);
        return new PostResponseDto(post);
    }

    @Transactional
    public PostResponseDto updatePost(Long id, PostRequestDto postRequestDto) {
        Post post = findPost(id);
        post.update(postRequestDto);
        return new PostResponseDto(post);
    }

    public DeleteResponseDto deletePost(Long id) {
        Post post = findPost(id);
        postRepository.delete(post);
        return new DeleteResponseDto();
    }

    public Post findPost (Long id) {
        return postRepository.findById(id).orElseThrow(()
                -> new IllegalArgumentException("μ„ νƒν•œ κ²Œμ‹œκΈ€μ€ μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."));
    }
}

πŸ“€ Repository

: DB 관리 (μ—°κ²°, ν•΄μ œ, μžμ› 관리)
: DB CRUD μž‘μ—…μ„ 처리

public interface PostRepository extends JpaRepository<Post, Long> {
    List<Post> findAllByOrderByCreatedAtDesc();
}

profile
πŸ’»πŸŒΎμ‹œκ³¨μ†Œλ…€μ˜ 엉망징창 개발 μ„±μž₯μΌμ§€πŸŒΎπŸ’» (2023.05.23 ~)

0개의 λŒ“κΈ€