ShortUrl shortUrl = new ShortUrl();
shortUrl.setOriginalUrl(request.getUrl());
shortUrl.setShortCode(shortCode);
// 设置过期时间
if (request.getExpireDays() != null) {
shortUrl.setExpiresAt(LocalDateTime.now().plusDays(request.getExpireDays()));
}
repository.save(shortUrl);
return new ShortUrlResponse(shortCode, request.getUrl());
}
public String getOriginalUrl(String shortCode) {
ShortUrl shortUrl = repository.findByShortCode(shortCode)
.orElseThrow(() new ResourceNotFoundException(Short URL not found));
// 检查是否过期
if (shortUrl.getExpiresAt() != null && shortUrl.getExpiresAt().isBefore(LocalDateTime.now())) {
throw new ResourceExpiredException(Short URL has expired);
}