Skip to content

Commit

Permalink
af
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamv108 committed Feb 6, 2024
1 parent aab6640 commit 98e6489
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface ShortUrlRepository extends JpaRepository<ShortURL, Long> {

@Query(nativeQuery = true,
value = "SELECT url FROM short_urls WHERE key_name = ? AND (account_id IS NULL OR account_id = ?)")
value = "SELECT url FROM short_urls WHERE key_name = ? AND (account_id IS NULL OR account_id = ?) AND expiry_at > now()")
Optional<String> findURL(String shortUrl, Long accountId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Optional;

@Slf4j
@RestController
Expand All @@ -30,12 +32,22 @@ public class TinyURLController {
private final TinyURLService service;

@GetMapping
public ResponseEntity<?> get(@RequestParam("url") final String url) {
public ResponseEntity<?> get(@RequestParam("url") final String url,
@RequestParam(value = "alias", required = false) final String alias,
@RequestParam(value = "ttlInSeconds", required = false) final Long ttlInSeconds) {

if (StringUtils.isEmpty(url))
throw new InvalidRequestException("url", "url must not be null or empty");

final ShortURL shortURL = ShortURL.builder().url(url).accountId(AccountIDContextHolder.get()).build();
final ShortURL shortURL = ShortURL.builder()
.url(url)
.accountId(AccountIDContextHolder.get())
.keyName(alias)
.expiryAt(Optional.ofNullable(ttlInSeconds)
.map(ttl -> new Date(System.currentTimeMillis() + (ttl * 1000)))
.orElse(null))
.build();

final String tinyURL = this.service.create(shortURL);
return ResponseUtils.getDataResponseEntity(HttpStatus.CREATED.value(), new HashMap<>() {
{
Expand Down

0 comments on commit 98e6489

Please sign in to comment.