diff --git a/src/main/java/com/kasina/makeitshort/controller/MakeItShortController.java b/src/main/java/com/kasina/makeitshort/controller/MakeItShortController.java index c865caf..ad2c1b7 100644 --- a/src/main/java/com/kasina/makeitshort/controller/MakeItShortController.java +++ b/src/main/java/com/kasina/makeitshort/controller/MakeItShortController.java @@ -27,6 +27,7 @@ public ResponseEntity generateShortLink(@RequestBody MakeItShortDto makeItSho MakeItShort makeItShort = makeItShortService.generateShortLink(makeItShortDto); if (makeItShort != null) { MakeItShortResponse makeItShortResponse = new MakeItShortResponse(); + makeItShortResponse.setOriginalUrl(makeItShort.getOriginalUrl()); makeItShortResponse.setExpirationDate(makeItShort.getExpirationDate()); makeItShortResponse.setShortLink(makeItShort.getShortLink()); @@ -59,7 +60,7 @@ public ResponseEntity redirectOriginalUrl(@PathVariable String shortLink, Htt } if(shortUrl.getExpirationDate().isBefore(LocalDateTime.now())){ - makeItShortService.deleteShortLink(shortUrl); + makeItShortService.deleteShortLink(shortUrl.getUrlId()); UrlErrorResponseDto urlErrorResponseDto = new UrlErrorResponseDto(); urlErrorResponseDto.setStatus("Url Expired. Please try generating a fresh one"); urlErrorResponseDto.setError("200"); @@ -68,4 +69,13 @@ public ResponseEntity redirectOriginalUrl(@PathVariable String shortLink, Htt response.sendRedirect(shortUrl.getOriginalUrl()); return null; } + + @DeleteMapping("/{urlId}") + public ResponseEntity deleteShortLink(@PathVariable String urlId) { + + makeItShortService.deleteShortLink(urlId); + return ResponseEntity.ok("Url deleted successfully"); + } + + } diff --git a/src/main/java/com/kasina/makeitshort/model/MakeItShort.java b/src/main/java/com/kasina/makeitshort/model/MakeItShort.java index fe27072..632b221 100644 --- a/src/main/java/com/kasina/makeitshort/model/MakeItShort.java +++ b/src/main/java/com/kasina/makeitshort/model/MakeItShort.java @@ -5,23 +5,34 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; +import java.util.UUID; @Entity @Data @Builder @AllArgsConstructor -@NoArgsConstructor @Document(collection = "urls") public class MakeItShort { @Id - private long urlId; + private String urlId; + @Lob private String originalUrl; private String shortLink; + @DateTimeFormat(pattern = "yyyy-MM-dd") private LocalDateTime creationDate; + @DateTimeFormat(pattern = "yyyy-MM-dd") private LocalDateTime expirationDate; + + + public MakeItShort() { + // Generate a UUID as the URL ID + this.urlId = UUID.randomUUID().toString(); + } } diff --git a/src/main/java/com/kasina/makeitshort/model/MakeItShortDto.java b/src/main/java/com/kasina/makeitshort/model/MakeItShortDto.java index 765097a..dcc029e 100644 --- a/src/main/java/com/kasina/makeitshort/model/MakeItShortDto.java +++ b/src/main/java/com/kasina/makeitshort/model/MakeItShortDto.java @@ -6,6 +6,7 @@ @Data @RequiredArgsConstructor public class MakeItShortDto { + private String url; private String expirationDate; //optional } diff --git a/src/main/java/com/kasina/makeitshort/model/MakeItShortResponse.java b/src/main/java/com/kasina/makeitshort/model/MakeItShortResponse.java index e9e4033..a6ad2b8 100644 --- a/src/main/java/com/kasina/makeitshort/model/MakeItShortResponse.java +++ b/src/main/java/com/kasina/makeitshort/model/MakeItShortResponse.java @@ -8,6 +8,7 @@ @Data @RequiredArgsConstructor public class MakeItShortResponse { + private String originalUrl; private String shortLink; private LocalDateTime expirationDate; diff --git a/src/main/java/com/kasina/makeitshort/repository/MakeItShortRepo.java b/src/main/java/com/kasina/makeitshort/repository/MakeItShortRepo.java index 30274c5..e7683dd 100644 --- a/src/main/java/com/kasina/makeitshort/repository/MakeItShortRepo.java +++ b/src/main/java/com/kasina/makeitshort/repository/MakeItShortRepo.java @@ -4,7 +4,7 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Query; -public interface MakeItShortRepo extends MongoRepository { +public interface MakeItShortRepo extends MongoRepository { @Query("{ 'shortLink' : ?0 }") MakeItShort findByShortLink(String shortLink); diff --git a/src/main/java/com/kasina/makeitshort/service/MakeItShortService.java b/src/main/java/com/kasina/makeitshort/service/MakeItShortService.java index 8fdf564..431fa41 100644 --- a/src/main/java/com/kasina/makeitshort/service/MakeItShortService.java +++ b/src/main/java/com/kasina/makeitshort/service/MakeItShortService.java @@ -7,6 +7,6 @@ public interface MakeItShortService { MakeItShort generateShortLink(MakeItShortDto makeItShortDto); MakeItShort persistShortLink(MakeItShort makeItShort); MakeItShort getEncoderUrl(String url); - void deleteShortLink(MakeItShort makeItShort); + void deleteShortLink(String urlId); } diff --git a/src/main/java/com/kasina/makeitshort/service/MakeItShortServiceImpl.java b/src/main/java/com/kasina/makeitshort/service/MakeItShortServiceImpl.java index f0e3daf..7d717d4 100644 --- a/src/main/java/com/kasina/makeitshort/service/MakeItShortServiceImpl.java +++ b/src/main/java/com/kasina/makeitshort/service/MakeItShortServiceImpl.java @@ -3,13 +3,21 @@ import com.google.common.hash.Hashing; import com.kasina.makeitshort.model.MakeItShort; import com.kasina.makeitshort.model.MakeItShortDto; +import com.kasina.makeitshort.model.UrlErrorResponseDto; import com.kasina.makeitshort.repository.MakeItShortRepo; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import javax.swing.text.DateFormatter; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; + +import static org.springframework.util.ClassUtils.isPresent; @Component @RequiredArgsConstructor @@ -25,6 +33,7 @@ public MakeItShort generateShortLink(MakeItShortDto makeItShortDto) { String encodedUrl = encodeUrl(makeItShortDto.getUrl()); MakeItShort urlToPersist = new MakeItShort(); + System.out.println(" Current Url ID is: ******" +urlToPersist.getUrlId()); urlToPersist.setCreationDate(LocalDateTime.now()); urlToPersist.setOriginalUrl(makeItShortDto.getUrl()); urlToPersist.setShortLink(encodedUrl); @@ -36,10 +45,15 @@ public MakeItShort generateShortLink(MakeItShortDto makeItShortDto) { } private LocalDateTime getExpirationDate(String expirationDate, LocalDateTime creationDate) { + // TODO: check if date is in the correct format if(StringUtils.isBlank(expirationDate)){ return creationDate.plusSeconds(120); } - return LocalDateTime.parse(expirationDate); + + return LocalDateTime.parse( + expirationDate + "T00:00:00", // Append a default time of 00:00:00 + DateTimeFormatter.ISO_LOCAL_DATE_TIME + ); } private String encodeUrl(String url) { @@ -60,7 +74,15 @@ public MakeItShort getEncoderUrl(String url) { } @Override - public void deleteShortLink(MakeItShort makeItShort) { - makeItShortRepository.delete(makeItShort); + public void deleteShortLink(String urlId) { + if (StringUtils.isEmpty(urlId) && makeItShortRepository.findById(urlId).isEmpty()){ + + throw new IllegalStateException("Url does not exist or it might have expired"); + } + makeItShortRepository.deleteById(urlId); } + +// public void deleteShortLink(String urlId) { +// makeItShortRepository.deleteById(urlId); +// } }