Spring Coupang REST API 어플리케이션 만들기

Danny·2023년 10월 27일
0

ASAC

목록 보기
5/8

스프링부트 핵심 가이드의 내용을 참고 했습니다.


Spec

  • OS : MacOs
  • IDE : IntelliJ
  • JAVA : Version_17
  • MySql : Version_8
  • Spring Boot : 2.7.5

ERD

API 명세서

기본 유저 테스트

업로드중..


목표

설계한 API 중 다음 4개의 API 를 직접 개발 (JDBC Template 만을 활용하여)

  • 상품 디테일 CRUD
    • 상품 디테일 정보 조회 API 1개
    • (어드민) 상품 디테일 추가 API 1개
    • (어드민) 상품 디테일 삭제 API 1개
    • (어드민) 상품 디테일 수정 API 1개

초기 프로젝트 세팅

  • Docker 활용하여 내 컴퓨터에서 데이터베이스 실행 및 Spring 연결하기
    • (Docker Compose 미사용)
server:
  port: 9000

spring:
  application:
    name: demo

  datasource:
    url: jdbc:mysql://localhost:3309/asac?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&serverTimezone=Asia/Seoul
    username: root
    password: inhatc # password
    driver-class-name: com.mysql.cj.jdbc.Driver # mysql 8버전
    # driver-class-name: com.mysql.jdbc.Driver # mysql 5버전
  sql:
    init:
      platform: mysql


상품 디테일 정보 조회 API 💾

  • 상품의 상세설명을 위해 상품 밑 계층의 상품 상세설명 테이블과 조인후
    원하는 상품 Id를 검색 후 그 하나의 상품에 대한 상품설명을 API로 호출했다.

쿼리문 테스트

쿼리문

SELECT products.*, product_Detail.*
FROM products
JOIN product_Detail 
ON products.productid = product_Detail.products_productid
WHERE products.productid = ?;

코드

ProductDao.java

public GetProductRes getProductDeatilById(int productId){
        String getProductQuery = "SELECT products.*, product_Detail.*\n" +
                "FROM products\n" +
                "JOIN product_Detail ON products.productid = product_Detail.products_productid\n" +
                "WHERE products.productid = ?;";
        int getProductParams = productId;
        return this.jdbcTemplate.queryForObject(getProductQuery,
                (rs, rowNum) -> new GetProductRes(
                        rs.getInt("ProductId"),
                        rs.getString("ProductName"),
                        rs.getString("ProductPrice"),
                        rs.getInt("seller_sellerid"),
                        rs.getString("ProductComment"),
                        rs.getString("ProductPhone")),
                getProductParams);
    }

ProductController.java

@ResponseBody
    @GetMapping("/{productId}") // (GET) 127.0.0.1:9000/api/products/:productId
    public BaseResponse<GetProductRes> getProductDeatilById(@PathVariable("productId") int productId) {
        // Get Users
        try{
            GetProductRes getProductRes = productService.getProductDeatilById(productId);
            return new BaseResponse<>(getProductRes);
        } catch(BaseException exception){
            return new BaseResponse<>((exception.getStatus()));
        }

    }

실행화면


(어드민) 상품 디테일 추가 API 💾

상품 디테일을 추가할 수 있는 어드민에 대한 권한을 줘야 한다고 생각을 함.
각 유저들에게 권한을 부여하여 관리자 권한 이라면 POST

  • Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.

  • MySQL의 "Safe Update Mode" 때문에 발생,

  • Safe Update Mode에서는 WHERE 절에서 KEY(column)을 사용하지 않고 전체 테이블을 업데이트 하는 것을 방지

쿼리문 테스트

/api/products/:productId/admin/productdetail

(어드민) 상품 디테일 추가 API 💾

필자의 기분이 안좋음에 따라 무기한 연기

0개의 댓글