Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

전남대 BE_나제법_1주차 과제(3단계) #173

Open
wants to merge 119 commits into
base: nove1080
Choose a base branch
from

Conversation

nove1080
Copy link

코드 작성하면서 어려웠던 점

CREATE TABLE Product (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    price INT NOT NULL,
    image_url VARCHAR(255) NOT NULL
);
public class Product extends Item {

    private Integer price;
    private URL imageUrl;
    //...
}

URL을 다루는 부분에서 객체와 테이블 간의 타입 차이가 있어서 둘을 서로 변환하는 과정이 좀 번거로웠습니다. 객체에도 DB와 동일하게 String 타입으로 URL을 다루었어야 하는지 잘 모르겠습니다. 제가 생각했을 때에는 자바에는 URL을 다루기에 적합한 데이터 타입이 이미 있지만 String을 사용해서 해결하여야 하는지 아니면 아래와 같은 컨버터를 두어서라도 URL로 다루는 것이 적절한 선택이였을지 잘 모르겠습니다.

@Component
public class StringToUrlConverter implements Converter<String, URL> {

    @Override
    public URL convert(String source) {
        try {
            return new URI(source).toURL();
        } catch (MalformedURLException | URISyntaxException e) {
            throw new IllegalArgumentException("유효하지 않은 URL 형식입니다.");
        }
    }
}

코드 리뷰 시, 멘토님이 중점적으로 리뷰해주셨으면 하는 부분

    public Long save(Product product) {
        String sql = "insert into product (name, price, image_url) values (?, ?, ?)";
        jdbcTemplate.update(sql,
            product.getName(), product.getPrice(), product.getImageUrl().toString());
        return product.getId(); //todo 반환값 수정(Product를 식별할 수 있는 값으로)
    }

Repository의 상품 저장 메서드에서 반환값으로 상품을 식별할 수 있는 ID값을 반환하려 했지만 현재 메서드에서 상품의 ID가 설정되기 전이라 null값이 반환되는 문제가 있었습니다.

CREATE TABLE Product (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    price INT NOT NULL,
    image_url VARCHAR(255) NOT NULL
);

Product 테이블을 위와 같이 ID를 AUTO_INCREMENT로 선언하여 실제 DB에 Insert가 되기 전까지는 해당 레코드의 아이디를 알 수 없기 때문에 일단은 save()의 반환값을 사용하는 부분은 없어서 그대로 두었습니다 ID를 가져오기 위해 다시 DB에 조회 쿼리를 보내서라도 해결하는 것이 나았을지는 잘 모르겠습니다 ㅜㅜ

nove1080 added 30 commits June 25, 2024 01:19
동일한 역할의 다른 이름을 가진 클래스 구현 예정
아이디가 같으면 같은 객체로 인식
web 패키지의 하위 패키지로 이동
findAllProducts -> readAllProducts
메서드 네이밍 통일을 위함
nove1080 added 26 commits July 1, 2024 13:49
CreateRequestToProductConverter 사용 대신 dto가 직접 Entity로의 변환 책임을 가진다
dto에서 변환 작업을 수행하도록 변경되어 사용하지 않는 클래스
dto에서 변환 작업을 수행하도록 변경되어 사용하지 않는 클래스
domain과 view의 분리를 위함
UpdateProductFromDto -> ReadProductResponse 사용
@nove1080 nove1080 closed this by deleting the head repository Jul 1, 2024
@nove1080 nove1080 reopened this Jul 2, 2024
@Hongdonggeon
Copy link

  • save 반환값
    KeyHolder 로 해결하셨네요 ! 👍

  • Product url 필드 타입
    선택은 자유지만, 저는 String으로 선언했을 것 같아요
    url 형식인지에 대한 검증을 하기 위해서 생성자에서 검증을 해줬을 것 같긴 하네요
    아래와 같은 형식으로요! 그러면 url 형식인지에 대한 검증도 완료되고 db -> 엔티티 반환할 때 따로 변환해주지 않아도 되지 않으니 더 간편해질 것 같아요

Product(String name, Integer price, String imageUrl) {
    validateName(name);
    validatePrice(price);
    validateImageUrl(imageUrl);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants