mirror of
https://github.com/SinTan1729/chhoto-url
synced 2025-04-28 11:56:52 -05:00
Merge pull request #53 from magnusja/main
Create UNIQUE INDEX on short URL
This commit is contained in:
commit
a170954232
2 changed files with 21 additions and 14 deletions
|
@ -99,5 +99,12 @@ pub fn open_db(path: String) -> Connection {
|
||||||
)
|
)
|
||||||
.expect("Unable to initialize empty database.");
|
.expect("Unable to initialize empty database.");
|
||||||
|
|
||||||
|
// Create index on short_url for faster lookups
|
||||||
|
db.execute(
|
||||||
|
"CREATE UNIQUE INDEX IF NOT EXISTS idx_short_url ON urls (short_url)",
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
.expect("Unable to create index on short_url.");
|
||||||
|
|
||||||
db
|
db
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,11 @@ pub fn is_api_ok(http: HttpRequest) -> Response {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request the DB for searching an URL
|
// Request the DB for searching an URL
|
||||||
pub fn get_longurl(shortlink: String, db: &Connection, needhits: bool) -> (Option<String>, Option<i64>) {
|
pub fn get_longurl(
|
||||||
|
shortlink: String,
|
||||||
|
db: &Connection,
|
||||||
|
needhits: bool,
|
||||||
|
) -> (Option<String>, Option<i64>) {
|
||||||
if validate_link(&shortlink) {
|
if validate_link(&shortlink) {
|
||||||
database::find_url(shortlink.as_str(), db, needhits)
|
database::find_url(shortlink.as_str(), db, needhits)
|
||||||
} else {
|
} else {
|
||||||
|
@ -123,18 +127,14 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
|
||||||
chunks.shortlink = gen_link(style, len);
|
chunks.shortlink = gen_link(style, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
if validate_link(chunks.shortlink.as_str())
|
if validate_link(chunks.shortlink.as_str()) {
|
||||||
&& get_longurl(chunks.shortlink.clone(), db, false).0.is_none()
|
if database::add_link(chunks.shortlink.clone(), chunks.longlink, db) {
|
||||||
{
|
(true, chunks.shortlink)
|
||||||
(
|
} else {
|
||||||
database::add_link(chunks.shortlink.clone(), chunks.longlink, db),
|
(false, String::from("Short URL is already in use!"))
|
||||||
chunks.shortlink,
|
}
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
(
|
(false, String::from("Short URL is not valid!"))
|
||||||
false,
|
|
||||||
String::from("Short URL not valid or already in use!"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue