Skip to content

Commit

Permalink
mongodb integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
casyazmon committed Jul 14, 2023
1 parent bd1874a commit 9237672
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 22 deletions.
15 changes: 10 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@
<artifactId>guava</artifactId>
<version>28.2-android</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Optional;

@RestController
@RequestMapping("/makeitshort/")
@RequiredArgsConstructor
public class MakeItShortController {
@Autowired

private final MakeItShortServiceImpl makeItShortService;

@PostMapping("/generate")
Expand All @@ -30,12 +32,12 @@ public ResponseEntity<?> generateShortLink(@RequestBody MakeItShortDto makeItSho
makeItShortResponse.setOriginalUrl(makeItShort.getOriginalUrl());
makeItShortResponse.setExpirationDate(makeItShort.getExpirationDate());
makeItShortResponse.setShortLink(makeItShort.getShortLink());
return new ResponseEntity<MakeItShortResponse>(makeItShortResponse, HttpStatus.OK);
return new ResponseEntity<>(makeItShortResponse, HttpStatus.OK);
}
UrlErrorResponseDto urlErrorResponseDto = new UrlErrorResponseDto();
urlErrorResponseDto.setStatus("404");
urlErrorResponseDto.setError("There was an error processing your request. please try again");
return new ResponseEntity<UrlErrorResponseDto>(urlErrorResponseDto, HttpStatus.OK);
return new ResponseEntity<>(urlErrorResponseDto, HttpStatus.OK);
}

@GetMapping("/{shortLink}")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/kasina/makeitshort/mode/MakeItShort.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;

Expand All @@ -16,6 +17,7 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "urls")
public class MakeItShort {
@Id
@GeneratedValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kasina.makeitshort.repository;

import com.kasina.makeitshort.mode.MakeItShort;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.util.Optional;

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

/*@Query("{ 'shortLink' : ?0 }")
Optional<MakeItShort> findByShortLink(String shortLink);*/
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.hash.Hashing;
import com.kasina.makeitshort.mode.MakeItShort;
import com.kasina.makeitshort.mode.MakeItShortDto;
import com.kasina.makeitshort.repository.MakeItShortRepository;
import com.kasina.makeitshort.repository.MakeItShortRepo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -15,11 +15,13 @@
@Component
@RequiredArgsConstructor
public class MakeItShortServiceImpl implements MakeItShortService {
@Autowired
private final MakeItShortRepository makeItShortRepository;

private final MakeItShortRepo makeItShortRepository;

@Override
public MakeItShort generateShortLink(MakeItShortDto makeItShortDto) {
// TODO: check if url already exist in the database then return it rather than saving the same url multiple time

if (StringUtils.isNotEmpty(makeItShortDto.getUrl())){
String encodedUrl = encodeUrl(makeItShortDto.getUrl());

Expand All @@ -36,7 +38,7 @@ public MakeItShort generateShortLink(MakeItShortDto makeItShortDto) {

private LocalDateTime getExpirationDate(String expirationDate, LocalDateTime creationDate) {
if(StringUtils.isBlank(expirationDate)){
return creationDate.plusSeconds(60);
return creationDate.plusSeconds(120);
}
return LocalDateTime.parse(expirationDate);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MONGO_DATABASE=makeitshort
MONGO_USER=casy
MONGO_PASSWORD=OerXjCNw703xMjzF
MONGO_CLUSTER=cluster0
7 changes: 7 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
#spring.data.mongodb.database=${env.MONGO_DATABASE}
#spring.data.mongodb.uri=mongodb+srv://${env.MONGO_USER}:${env.MONGO_PASSWORD}@${env.MONGO_CLUSTER}


spring.data.mongodb.uri=mongodb+srv://casy:OerXjCNw703xMjzF@cluster0.jfge0em.mongodb.net/
spring.data.mongodb.database=makeitshort

spring.main.allow-bean-definition-overriding=true

0 comments on commit 9237672

Please sign in to comment.