Feat: 단축 URL로 원본 URL 조회 기능 구현
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package be.gyu.urlShortener.exception;
|
||||
|
||||
public class ShortUrlNotFoundException extends RuntimeException{
|
||||
public ShortUrlNotFoundException(){ super("존재하지 않는 단축 URL 입니다."); }
|
||||
public ShortUrlNotFoundException(String msg){ super(msg); }
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package be.gyu.urlShortener.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import be.gyu.urlShortener.entity.UrlMap;
|
||||
|
||||
public interface UrlMapRepository extends JpaRepository<UrlMap,Integer> {
|
||||
|
||||
}
|
||||
public Optional<UrlMap> findByUrlMapShort(String urlMapShort);
|
||||
}
|
||||
@@ -4,8 +4,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import be.gyu.urlShortener.entity.UrlMap;
|
||||
import be.gyu.urlShortener.exception.ShortUrlNotFoundException;
|
||||
import be.gyu.urlShortener.repository.UrlMapRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.time.LocalDateTime;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@@ -79,4 +81,15 @@ public class MainService {
|
||||
// 단축된 URL 반환
|
||||
return shortResult;
|
||||
}
|
||||
|
||||
// 단축 URL로 원본 URL 조회 및 반환 메소드
|
||||
public String getOriginalUrl(String shortUrl){
|
||||
Optional<UrlMap> optional=urlMapRepository.findByUrlMapShort(shortUrl);
|
||||
|
||||
if(!optional.isPresent()){
|
||||
throw new ShortUrlNotFoundException();
|
||||
}
|
||||
|
||||
return optional.get().getUrlMapOriginal();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user