260612_04_ENH: uuid 생성 전에 이미 있는 요청인지 확인
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
package com.kri.shorturl.common.consts;
|
package com.kri.shorturl.common.consts;
|
||||||
|
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
public final class ShortUrlConstat {
|
public final class ShortUrlConstat {
|
||||||
|
|
||||||
public static final Integer UUID_LENGTH = 8;
|
public static final Integer UUID_LENGTH = 8;
|
||||||
|
public static final Integer MAX_UUID_GENERATION_ATTEMPTS = 10;
|
||||||
|
|
||||||
|
public static final String BASE_URL = "https://s.kric.com";
|
||||||
|
|
||||||
private ShortUrlConstat() {}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ public interface ShortUrlRepository extends JpaRepository<TShortUrlEntity, Strin
|
|||||||
|
|
||||||
Optional<TShortUrlEntity> findByUuid(String uuid);
|
Optional<TShortUrlEntity> findByUuid(String uuid);
|
||||||
|
|
||||||
|
Optional<TShortUrlEntity> findByOrgUrlAndUseParam(String orgUrl, Boolean useParam);
|
||||||
|
|
||||||
Boolean existsByUuid(String uuid);
|
Boolean existsByUuid(String uuid);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.kri.shorturl.service;
|
package com.kri.shorturl.service;
|
||||||
|
|
||||||
|
import static com.kri.shorturl.common.consts.ShortUrlConstat.BASE_URL;
|
||||||
|
import static com.kri.shorturl.common.consts.ShortUrlConstat.MAX_UUID_GENERATION_ATTEMPTS;
|
||||||
import static com.kri.shorturl.common.consts.ShortUrlConstat.UUID_LENGTH;
|
import static com.kri.shorturl.common.consts.ShortUrlConstat.UUID_LENGTH;
|
||||||
|
|
||||||
import com.kri.common.feature.util.CommonUtil;
|
import com.kri.common.feature.util.CommonUtil;
|
||||||
@@ -23,19 +25,22 @@ public class ShortUrlService {
|
|||||||
|
|
||||||
@Transactional(transactionManager = "transactionManager")
|
@Transactional(transactionManager = "transactionManager")
|
||||||
public String createShortUrl(ShortUrlCreateRequestDto req, String regUser, LocalDateTime regDt) {
|
public String createShortUrl(ShortUrlCreateRequestDto req, String regUser, LocalDateTime regDt) {
|
||||||
String uuid = CommonUtil.uuid(UUID_LENGTH);
|
|
||||||
while (shortUrlRepository.existsByUuid(uuid)) {
|
|
||||||
uuid = CommonUtil.uuid(UUID_LENGTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!req.getOrgUrl().startsWith("http://") && !req.getOrgUrl().startsWith("https://")) {
|
if (!req.getOrgUrl().startsWith("http://") && !req.getOrgUrl().startsWith("https://")) {
|
||||||
req.setOrgUrl("https://" + req.getOrgUrl());
|
req.setOrgUrl("https://" + req.getOrgUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return shortUrlRepository.findByOrgUrlAndUseParam(req.getOrgUrl(), req.isUseParam())
|
||||||
|
.map(entity -> makeShortUrl(entity.getUuid()))
|
||||||
|
.orElseGet(() -> createNewShortUrl(req, regUser, regDt));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createNewShortUrl(ShortUrlCreateRequestDto req, String regUser, LocalDateTime regDt) {
|
||||||
|
String uuid = generateUniqueUuid();
|
||||||
|
|
||||||
TShortUrlEntity entity = TShortUrlEntity.create(uuid, req, regUser, regDt);
|
TShortUrlEntity entity = TShortUrlEntity.create(uuid, req, regUser, regDt);
|
||||||
shortUrlRepository.save(entity);
|
shortUrlRepository.save(entity);
|
||||||
|
|
||||||
return uuid;
|
return makeShortUrl(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI getRedirectUri(String uuid, MultiValueMap<String, String> params){
|
public URI getRedirectUri(String uuid, MultiValueMap<String, String> params){
|
||||||
@@ -64,4 +69,23 @@ public class ShortUrlService {
|
|||||||
|
|
||||||
return builder.build(true).toUri();
|
return builder.build(true).toUri();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String makeShortUrl(String uuid){
|
||||||
|
return BASE_URL + "/" + uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateUniqueUuid() {
|
||||||
|
for (int attempt = 0; attempt < MAX_UUID_GENERATION_ATTEMPTS; attempt++) {
|
||||||
|
String uuid = CommonUtil.uuid(UUID_LENGTH);
|
||||||
|
|
||||||
|
if (!Boolean.TRUE.equals(shortUrlRepository.existsByUuid(uuid))) {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw BusinessException.internalError(
|
||||||
|
"SHORT_URL_KEY_GENERATION_FAILED",
|
||||||
|
"Failed to generate short URL key."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user