대외활동/UMC 4th

Ch 7 API : 당근마켓 CRUD

oxdjww 2023. 5. 18. 19:59
728x90
반응형

UMC 4th - SpringBoot

 

ch 6에서 설계했던 당근마켓 schema를 기반으로 실습을 하는 챕터이다

 

사용 기술 : SpringBoot, Postman, Datagrip

 

export 해두었던 data들이 잘 있는 모습이다

 

0. TestController

test를 위한 TestController를 작성해주었다

package tete.carrot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {

    @GetMapping("/test")
    public String test() {
        return "hello world";
    }
}

test 성공

 

1. User Entity

User class

 

User Entity를 구현해주었다.

lombok이 참 개발자를 편하게 해주는 기능인 것 같다

Entity특성상 setter를 빼고 getter 어노테이션을 붙여주었고,

기본 생성자와 필드 생성자 모두를 구현해주는 NoArgsConstructor, AllArgsConstructor 어노테이션을 붙여주었다.

package tete.carrot.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.*;
import org.springframework.boot.autoconfigure.domain.EntityScan;

import javax.naming.StringRefAddr;

@Entity(name="User")
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String nickname;
    private String email;
    private String phone;
    private String password;
    public void update(String name, String nickname, String email, String phone, String password) {
        this.name = name;
        this.nickname = nickname;
        this.email = email;
        this.phone = phone;
        this.password = password;
    }

}

 

2. JpaRepository class

package tete.carrot.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import tete.carrot.entity.User;

public interface UserJpaRepository extends JpaRepository<User,Long> {
}

Spring data JPA로 JPA에서 주는 메서드들을 이용하였다

 

3. UserController class

package tete.carrot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tete.carrot.entity.User;
import tete.carrot.service.UserService;
import lombok.*;

@RequiredArgsConstructor
@RestController
public class UserController {

    private final UserService userService;

    @PostMapping("/user")
    public Long create(@RequestBody User user) {
        return userService.save(user);
    }

    @GetMapping("/user/{id}")
    public User read(@PathVariable Long id) {
        return userService.findById(id);
    }

    @PostMapping("/user/{id}/update")
    public Long update(@PathVariable Long id, @RequestBody User user) {
        return userService.update(id, user);
    }

    @PostMapping("/user/{id}/delete")
    public Long delete(@PathVariable Long id) {
        userService.delete(id);
        return id;
    }
}

기본적인 crud가 가능하게끔 controller를 구현해주었다.

 

4. UserService class 

package tete.carrot.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tete.carrot.entity.User;
import tete.carrot.repository.UserJpaRepository;
import jakarta.transaction.Transactional;

@Service
public class UserService {
    @Autowired
    public UserService(UserJpaRepository userJpaRepository) {
        this.userJpaRepository = userJpaRepository;
    }

    private final UserJpaRepository userJpaRepository;

    public User findById(Long id) {
        return userJpaRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 유저입니다."));
    }

    @Transactional
    public Long save(User user) {
        return userJpaRepository.save(user).getId();
    }

    @Transactional
    public Long update(Long id, User user) {
        User currentUser = findById(id);
        currentUser.update(user.getName(), user.getNickname(), user.getPhone(), user.getEmail(),user.getPassword());
        return id;
    }

    @Transactional
    public void delete(Long id) {
        User user = findById(id);
        userJpaRepository.delete(user);
    }

}

service class를 구현해주고, postman을 이용한 api test를 해주었다

 

1. user create test

2. read user test

3. change user test

 

4. delete user test

 

CRUD 모두 성공적으로 된다 !

728x90
반응형

'대외활동 > UMC 4th' 카테고리의 다른 글

CI/CD with Github Action & AWS S3  (2) 2023.09.06
[FlagApp] SpringBoot - Server 배포  (0) 2023.08.07
Ch4 SQL : RDS실습  (0) 2023.05.17
Ch3 데이터베이스 : AWS RDS 구축  (0) 2023.05.17
Ch2 클라우드 구축 : AWS EC2 실습  (0) 2023.05.06