Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team 9 #49

Open
wants to merge 1 commit into
base: team-9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,27 @@ public UrlShortenerController(UrlShortenerService urlShortenerService) {
this.urlShortenerService = urlShortenerService;
}

@GetMapping("health")
public Mono<String> healthCheck() {
@GetMapping("health/{n}") //{} 변수로 처리하겠다.
public Mono<String> healthCheck(@PathVariable int n) {
// For basic tutorial
return Mono.just("Hello World");
return fib(n).map(element -> {
return String.valueOf(element);
});
}

private Mono<Integer> fib(int n) {
if(n==0) {
return Mono.just(0);
}
if(n==1 || n==2) {
return Mono.just(1);
}

Mono<Integer> f0 = fib(n-1);
Mono<Integer> f1 = fib(n-2);
return Mono.zip(f0, f1, (n0, n1) -> {
return n0+n1;
});
}

@GetMapping("{encoded}")
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/ac/cnu/realcoding/encoding/Base62Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ public class Base62Processor {

public static String encode(long number) {
// TODO: Implement Base62 Encoding Algorithm.
return "";

StringBuilder sb = new StringBuilder();
do {
sb.append(CODEC.charAt((int)(number%BASE)));
number /= BASE;
} while (number > 0);
return sb.reverse().toString();
}

public static long decode(String encoded) {
// TODO: Implement Base62 Decoding Algorithm.
return 0;
long sum = 0;
for(Character ch : encoded.toCharArray()) {
sum *= BASE;
sum += CODEC.indexOf(ch);
}
return sum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class URLInformation {
@Id
private Long id;

private String url;

public URLInformation(String url) {
this.url=url;
}
}
20 changes: 18 additions & 2 deletions src/main/java/ac/cnu/realcoding/service/UrlShortenerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.net.URI;

import ac.cnu.realcoding.encoding.Base62Processor;
import ac.cnu.realcoding.repository.URLInformation;
import org.springframework.stereotype.Service;

import ac.cnu.realcoding.configurations.ApplicationConfiguration;
import ac.cnu.realcoding.models.UrlShortenerRequest;
import ac.cnu.realcoding.models.UrlShortenerResponse;
import ac.cnu.realcoding.repository.URLRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

@Service
Expand All @@ -22,14 +25,27 @@ public Mono<URI> unshortenUrl(String encoded) {
// Problem A: Decode encoded String
// Problem B: Get full url by querying decoded PK.
// Problem C: if encoded string is invalid or not found return Bad Request
return Mono.error(new UnsupportedOperationException("not implemented"));
return Mono.just(Base62Processor.decode(encoded))
.flatMap(decoded -> urlRepository.findById(decoded))
.map(URLInformation::getUrl)
.map(URI::create);
}

public Mono<UrlShortenerResponse> shortenUrl(UrlShortenerRequest urlShortenerRequest) {
// Problem A: Return Bad request for non http:// or https:// scheme
// Problem B: Insert to database and get PK from database, PK should be auto-generated integer.
// Problem C: Encode PK by using Base 63.
// Problem D: Build UrlShortenerResponse with server host and port.
return Mono.error(new UnsupportedOperationException("not implemented"));
String url = urlShortenerRequest.getUrl();
return urlRepository.save(new URLInformation(url))
.map(URLInformation::getId)
.map(Base62Processor::encode)
.map(encoded -> UriComponentsBuilder.newInstance()
.scheme("http")
.host(applicationConfig.getHost())
.port(applicationConfig.getPort())
.path(encoded)
.toUriString())
.map(UrlShortenerResponse::of);
}
}