Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Support custom extension for url (#25)
Browse files Browse the repository at this point in the history
* Add shuttle crate

* Update sqlx table schema

* Add shuttle deployment

* Add extension to db if provided

* Add tests for adding records with extension
  • Loading branch information
adrianEffe authored Sep 6, 2023
1 parent 8671998 commit 9abb4b0
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions Tests/shorten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,59 @@ async fn shorten_returns_200_for_valid_form_data() {
assert_eq!(saved.url, "www.google.com");
}

#[tokio::test]
async fn shorten_with_extension_returns_200_for_valid_form_data() {
let app = TestApp::new().await;

let mut connection = PgConnection::connect(&app.db_connection)
.await
.expect("Failed to connect to Postgres");

let url = "https://rust-lang.org";

let custom_extension = "rustlang";

let body = format!(
"{{\"url\":\"{}\",\"extension\":\"{}\"}}",
url, custom_extension
);

let _ = sqlx::query!("DELETE FROM briefly WHERE id = $1", custom_extension)
.execute(&mut connection)
.await;

let response = app.post("shorten", body).send().await.unwrap();

assert!(response.status().is_success());

let saved = sqlx::query!("SELECT url FROM briefly WHERE id = $1", custom_extension)
.fetch_one(&mut connection)
.await
.expect("Failed to fetch saved shortened url");

assert_eq!(saved.url, url);
}

#[tokio::test]
async fn shorten_with_extension_returns_500_for_existing_record() {
let app = TestApp::new().await;

let url = "https://github.com";

let custom_extension = "github";

let body = format!(
"{{\"url\":\"{}\",\"extension\":\"{}\"}}",
url, custom_extension
);

let _ = app.post("shorten", body.clone()).send().await.unwrap();

let response = app.post("shorten", body).send().await.unwrap();

assert!(response.status().is_server_error());
}

#[tokio::test]
async fn shorten_returns_422_for_missing_data() {
let app = TestApp::new().await;
Expand Down
12 changes: 8 additions & 4 deletions src/routes/shorten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub async fn shorten(
let mut retry_count = 3;

while retry_count > 0 {
let query_result = insert_in_db(&payload.url, State(&data)).await;
let query_result =
insert_in_db(&payload.url, payload.extension.as_deref(), State(&data)).await;

match query_result {
Ok(request) => {
Expand All @@ -34,17 +35,20 @@ pub async fn shorten(

async fn insert_in_db(
url: &str,
extension: Option<&str>,
State(data): State<&Arc<AppState>>,
) -> Result<UrlRequestModel, Error> {
let date = Utc::now();
let modifier = date.to_string() + url;
let id = generate(&modifier);
let id = extension.map(|ext| ext.to_string()).or_else(|| {
let modifier = format!("{}{}", date, url);
Some(generate(&modifier))
});

sqlx::query_as!(
UrlRequestModel,
"INSERT INTO briefly (id, url, created_at) VALUES ($1, $2, $3) RETURNING *",
id,
url.to_string(),
url,
date
)
.fetch_one(&data.db)
Expand Down
1 change: 1 addition & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct CreateShortUrlSchema {
pub url: String,
pub extension: Option<String>,
}

0 comments on commit 9abb4b0

Please sign in to comment.