mirror of
https://github.com/SinTan1729/chhoto-url
synced 2024-12-26 23:58:35 -06:00
Avoid adding duplicate shortUrl
This commit is contained in:
parent
0740bc79aa
commit
fab2924250
2 changed files with 20 additions and 6 deletions
|
@ -30,12 +30,15 @@ public class Routes {
|
||||||
}
|
}
|
||||||
String shortUrl = split[1];
|
String shortUrl = split[1];
|
||||||
shortUrl = shortUrl.toLowerCase();
|
shortUrl = shortUrl.toLowerCase();
|
||||||
|
|
||||||
|
var shortUrlPresent = urlRepository
|
||||||
|
.findForShortUrl(shortUrl);
|
||||||
|
|
||||||
if (Utils.validate(shortUrl)) {
|
if (shortUrlPresent.isEmpty() && Utils.validate(shortUrl)) {
|
||||||
return urlRepository.addUrl(longUrl, shortUrl);
|
return urlRepository.addUrl(longUrl, shortUrl);
|
||||||
} else {
|
} else {
|
||||||
res.status(HttpStatus.BAD_REQUEST_400);
|
res.status(HttpStatus.BAD_REQUEST_400);
|
||||||
return "shortUrl not valid ([a-z0-9-_]+)";
|
return "shortUrl not valid or already in use";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,11 +72,22 @@ const submitForm = () => {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: `${longUrl.value};${shortUrl.value}`
|
body: `${longUrl.value};${shortUrl.value}`
|
||||||
})
|
})
|
||||||
.then(_ => {
|
.then((res) => {
|
||||||
longUrl.value = "";
|
if (!res.ok) {
|
||||||
shortUrl.value = "";
|
controls = document.querySelector(".pure-controls");
|
||||||
|
errBox = document.createElement("p");
|
||||||
|
errBox.setAttribute("id", "errBox");
|
||||||
|
errBox.setAttribute("style", "color:red");
|
||||||
|
errBox.innerHTML = "Short URL not valid or already in use";
|
||||||
|
controls.appendChild(errBox);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
document.getElementById("errBox")?.remove();
|
||||||
|
longUrl.value = "";
|
||||||
|
shortUrl.value = "";
|
||||||
|
|
||||||
refreshData();
|
refreshData();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue