Skip to content

Commit

Permalink
Merge pull request #35 from onetime-with-members/feature/#34/url-vali…
Browse files Browse the repository at this point in the history
…dation

[feat] : URL 변환 시 검증 로직을 추가한다
  • Loading branch information
bbbang105 authored Sep 26, 2024
2 parents 67fd96a + 0f62bf5 commit e636be0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/main/java/side/onetime/repository/EventRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

public interface EventRepository extends JpaRepository<Event,Long> {
Optional<Event> findByEventId(UUID eventId);
boolean existsByEventId(UUID eventId);
}
29 changes: 27 additions & 2 deletions src/main/java/side/onetime/service/UrlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,46 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import side.onetime.dto.UrlDto;
import side.onetime.exception.EventErrorResult;
import side.onetime.exception.EventException;
import side.onetime.repository.EventRepository;
import side.onetime.util.Base62Util;

import java.util.UUID;

@Service
@RequiredArgsConstructor
public class UrlService {
private final EventRepository eventRepository;

// URL 단축 메서드
public UrlDto.ConvertToShortenUrlResponse convertToShortenUrl(UrlDto.ConvertToShortenUrlRequest convertToShortenUrlRequest) {
String originalUrl = convertToShortenUrlRequest.getOriginalUrl();

UUID eventId = extractEventIdFromUrl(originalUrl);
if (!eventRepository.existsByEventId(eventId)) {
throw new EventException(EventErrorResult._NOT_FOUND_EVENT);
}

return UrlDto.ConvertToShortenUrlResponse.of(Base62Util.convertToShortenUrl(originalUrl));
}

// URL 단축 메서드
// URL 복원 메서드
public UrlDto.ConvertToOriginalUrlResponse convertToOriginalUrl(UrlDto.ConvertToOriginalUrlRequest convertToOriginalUrlRequest) {
String shortenUrl = convertToOriginalUrlRequest.getShortenUrl();
return UrlDto.ConvertToOriginalUrlResponse.of(Base62Util.convertToOriginalUrl(shortenUrl));
String originalUrl = Base62Util.convertToOriginalUrl(shortenUrl);

UUID eventId = extractEventIdFromUrl(originalUrl);
if (!eventRepository.existsByEventId(eventId)) {
throw new EventException(EventErrorResult._NOT_FOUND_EVENT);
}

return UrlDto.ConvertToOriginalUrlResponse.of(originalUrl);
}

// URL에서 Event ID 추출
private UUID extractEventIdFromUrl(String url) {
String[] parts = url.split("/");
return UUID.fromString(parts[parts.length - 1]);
}
}
3 changes: 0 additions & 3 deletions src/main/java/side/onetime/util/Base62Util.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package side.onetime.util;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigInteger;

@Component
@RequiredArgsConstructor
public class Base62Util {
private static final String BASE62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final String SHORTEN_DOMAIN = "https://1-ti.me/";
Expand Down

0 comments on commit e636be0

Please sign in to comment.