From 673ba0176a31c9052810f227be6731e087ebd3cf Mon Sep 17 00:00:00 2001 From: Gyubin-Han Date: Tue, 17 Jun 2025 16:04:09 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EB=8B=A8=EC=B6=95=20URL=EB=A1=9C=20?= =?UTF-8?q?=EC=9B=90=EB=B3=B8=20URL=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/ShortUrlNotFoundException.java | 6 ++++++ .../urlShortener/repository/UrlMapRepository.java | 6 ++++-- .../be/gyu/urlShortener/service/MainService.java | 13 +++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/main/java/be/gyu/urlShortener/exception/ShortUrlNotFoundException.java diff --git a/src/main/java/be/gyu/urlShortener/exception/ShortUrlNotFoundException.java b/src/main/java/be/gyu/urlShortener/exception/ShortUrlNotFoundException.java new file mode 100644 index 0000000..b5bd276 --- /dev/null +++ b/src/main/java/be/gyu/urlShortener/exception/ShortUrlNotFoundException.java @@ -0,0 +1,6 @@ +package be.gyu.urlShortener.exception; + +public class ShortUrlNotFoundException extends RuntimeException{ + public ShortUrlNotFoundException(){ super("존재하지 않는 단축 URL 입니다."); } + public ShortUrlNotFoundException(String msg){ super(msg); } +} diff --git a/src/main/java/be/gyu/urlShortener/repository/UrlMapRepository.java b/src/main/java/be/gyu/urlShortener/repository/UrlMapRepository.java index 3a9f2d4..c741119 100644 --- a/src/main/java/be/gyu/urlShortener/repository/UrlMapRepository.java +++ b/src/main/java/be/gyu/urlShortener/repository/UrlMapRepository.java @@ -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 { - -} + public Optional findByUrlMapShort(String urlMapShort); +} \ No newline at end of file diff --git a/src/main/java/be/gyu/urlShortener/service/MainService.java b/src/main/java/be/gyu/urlShortener/service/MainService.java index b574d81..72b9b40 100644 --- a/src/main/java/be/gyu/urlShortener/service/MainService.java +++ b/src/main/java/be/gyu/urlShortener/service/MainService.java @@ -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 optional=urlMapRepository.findByUrlMapShort(shortUrl); + + if(!optional.isPresent()){ + throw new ShortUrlNotFoundException(); + } + + return optional.get().getUrlMapOriginal(); + } }