From 769db79fded0c52e97338b5b2d65c8ecba99140b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Wed, 2 Nov 2022 08:10:27 +0000 Subject: [PATCH] Fix error when shorturl is empty --- src/main/java/tk/draganczuk/url/Routes.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/tk/draganczuk/url/Routes.java b/src/main/java/tk/draganczuk/url/Routes.java index ae070a3..8a6dd99 100644 --- a/src/main/java/tk/draganczuk/url/Routes.java +++ b/src/main/java/tk/draganczuk/url/Routes.java @@ -18,13 +18,17 @@ public class Routes { public static String addUrl(Request req, Response res) { var body = req.body(); - var split = body.split(";"); - String longUrl = split[0]; - String shortUrl = split[1]; - - if (shortUrl == null || shortUrl.isBlank()) { - shortUrl = Utils.randomString(); + if (body.endsWith(";")) { + body = body + "$"; } + var split = body.split(";"); + + String longUrl = split[0]; + + if (split[1].equals("$")) { + split[1] = Utils.randomString(); + } + String shortUrl = split[1]; if (Utils.validate(shortUrl)) { return urlRepository.addUrl(longUrl, shortUrl);