1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2025-03-18 06:46:48 -05:00
chhoto-url/src/main/java/tk/draganczuk/url/Routes.java

55 lines
1,017 B
Java
Raw Normal View History

2020-02-13 16:52:33 -06:00
package tk.draganczuk.url;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import org.eclipse.jetty.util.log.Log;
import spark.Request;
import spark.Response;
public class Routes {
private static UrlFile urlFile;
static{
try {
urlFile = new UrlFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<String> getAll(Request req, Response res) throws IOException{
return urlFile.getAll();
}
public static String addUrl(Request req, Response res) {
String longUrl = req.queryParams("long");
String shortUrl = req.queryParams("short");
if (shortUrl == null) {
shortUrl = Utils.randomString();
}
return urlFile.addUrl(longUrl, shortUrl);
}
public static String goToLongUrl(Request req, Response res){
String shortUrl = req.params("shortUrl");
var longUrlOpt = urlFile
.findForShortUrl(shortUrl);
if(longUrlOpt.isEmpty()){
res.status(404);
return "";
}
res.redirect(longUrlOpt.get());
return "";
}
}