Skip to content

Commit

Permalink
Delete short link implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
casyazmon committed Jul 15, 2023
1 parent 606c7b6 commit 0b86a75
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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");
Expand All @@ -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");
}


}
15 changes: 13 additions & 2 deletions src/main/java/com/kasina/makeitshort/model/MakeItShort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@Data
@RequiredArgsConstructor
public class MakeItShortDto {

private String url;
private String expirationDate; //optional
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Data
@RequiredArgsConstructor
public class MakeItShortResponse {

private String originalUrl;
private String shortLink;
private LocalDateTime expirationDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface MakeItShortRepo extends MongoRepository<MakeItShort, Long> {
public interface MakeItShortRepo extends MongoRepository<MakeItShort, String> {
@Query("{ 'shortLink' : ?0 }")
MakeItShort findByShortLink(String shortLink);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
// }
}

0 comments on commit 0b86a75

Please sign in to comment.