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

[5기 최인준] Shorten-URL 과제 제출합니다 #60

Open
wants to merge 20 commits into
base: Injun
Choose a base branch
from

Conversation

dlswns2480
Copy link

📌 과제 설명

원본 URL을 입력받아 Short Url을 생성해주고 Short Url을 통해 원본 Url에 접근할 수 있는 서비스입니다.

👩‍💻 요구 사항과 구현 내용

요구사항

  • URL 입력폼 제공 및 결과 출력
  • URL Shortening Key는 8 Character 이내로 생성
  • 단축된 URL 요청시 원래 URL로 리다이렉트
  • 단축된 URL에 대한 요청 수 정보저장 (optional)
  • Shortening Key를 생성하는 알고리즘 2개 이상 제공하며 애플리케이션 실행중 동적으로 변경 가능 (optional)

구현 내용

  • Base62 인코딩 알고리즘을 통해 Url 을 인코딩 했습니다.
  • url 단축은 해당 url의 primary key를 인코딩한 결과로 하였습니다.
  • 이미 단축된 결과가 있는 원본 URL은 새로 인코딩하여 생성하지 않고 기존에 생성된 short url을 리턴하도록 하였습니다.
  • 단축 결과 페이지에 단축된 url과 해당 단축 url의 리다이렉트 요청 횟수를 렌더링하도록 하였습니다.
  • 존재하지 않는 단축 url을 통한 리다이렉트 요청 시 에러 페이지로 리다이렉트 시키도록 했습니다.
  • 이번 과제는 시간이 부족해 구현 자체에만 집중하였습니다.

✅ PR 포인트 & 궁금한 점

  • pk값, 즉 id가 작은 수일 때, 인코딩 알고리즘 결과인 단축 url이 너무 단축된 url인데 인코딩 알고리즘을 어떻게 구현하면 좋을까요??

Copy link

@EunChanNam EunChanNam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

과제 고생하셨어요~!👏 간단하게 리뷰 남겨봤습니다 !


public abstract class CommonException extends RuntimeException {

public abstract String getMessage();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeException 안에 getMessage() 메소드가 있는거로아는데 getMessage() 메소드를 따로 선언하신 이유가 있으실까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getMessage()를 해당 클래스의 추상 메소드로 두고 싶어서 이런식으로 했는데 굳이 필요하지 않은 부분일까요?

Comment on lines +13 to +21
@Override
public String encode(Long id) {
StringBuilder sb = new StringBuilder();
while (id > 0) {
log.info("id % radix = {}", (int) (id % RADIX));
sb.append(BASE62_CHARACTERS.charAt((int) (id % RADIX)));
id /= RADIX;
}
return sb.toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 구현되면 글자수가 1개부터 시작할 것 같은데, 의도하신 걸까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흔히 있는 인코딩 알고리즘을 써서 그렇게 됐습니다.. 인코딩 알고리즘에 대해 더 자세히 알아봐야 할 것 같습니다!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그냥 다들 7자나 8자로 구현하길래 물어봤습니당..ㅎㅎ

findUrl.get().getHits()); // 단축 URL, 누적 요청횟수 리턴
}

Url url = new Url(longUrl, "", 0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

너무 부자연스러운 생성자 호출같은데, 이 과정에서 필요한 생성 메소드나 생성자를 만드는건 어떨까요?

Comment on lines 32 to 36
public Url(String longUrl, String shortUrl, int hits) {
this.longUrl = longUrl;
this.shortUrl = shortUrl;
this.hits = hits;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

처음에 생성되는 Url 은 hits 가 0 으로 초기화되는거로 알고 있는데, 파라미터로 받는거보단 내부적으로 0 으로 초기화하는건 어떨까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

무조건 0으로 생성되니까 그렇게 하는게 맞는 것 같습니다. 이 리뷰 주신 부분 위 리뷰랑 엮어서 반영하겠습니다!

Comment on lines +46 to +62
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Url url = (Url) o;
return hits == url.hits && Objects.equals(id, url.id) && Objects.equals(longUrl,
url.longUrl) && Objects.equals(shortUrl, url.shortUrl);
}

@Override
public int hashCode() {
return Objects.hash(id, longUrl, shortUrl, hits);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 클래스에 equalsAndHashCode 를 구현하신 이유가 궁금합니다 :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 할 때 편했을 때가 있어 구현했는데 테스트를 짜지않아.. 없어도 될 것 같습니다

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UrlEncoder 네이밍이고 Url 도메인 안에서만 사용되는 것 같은데 common 으로 분리하신 이유가 있으실까요?

Copy link

@ordilov ordilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

과제 고생하셨습니다~!!
질문주신 부분 달아보자면 쓰이는 부분에 따라 다르긴한데 간단한 용도라면 오픈소스 쓰시는 것도 괜찮습니다. 이런데서 어떻게 만들었는지 참고해보시면 괜찮을 것 같습니다!


public Url(String longUrl) {
this.longUrl = longUrl;
this.shortUrl = "";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성할 때 빈 값으로 생성 후에 제대로 된 값으로 변경하지말고 생성 시에 제대로 된 값으로 넣어주면 좋을 것 같습니다.
생성된 이후엔 따로 정상적인 값이 들어있는지 확인할 필요가 없으면 좋은 것 같아요!

@Column(name = "short_url")
private String shortUrl;

@Column(name = "request_hits")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

크게 중요한건 아닌데 양수만 가능한 필드면 @Column(columnDefinition = "UNSIGNED INT(11)")로 지정하는 것도 괜찮습니다.

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

Successfully merging this pull request may close these issues.

None yet

3 participants