-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bbf022
commit e8a7a55
Showing
14 changed files
with
442 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
application: | ||
host: 127.0.0.1 | ||
base_url: "http://127.0.0.1" | ||
|
||
database: | ||
require_ssl: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Add migration script here | ||
ALTER TABLE | ||
subscriptions | ||
ADD | ||
COLUMN status TEXT NULL; |
18 changes: 18 additions & 0 deletions
18
migrations/20241102065900_make_status_not_null_in_subscriptions.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- Add migration script here | ||
BEGIN; | ||
|
||
UPDATE | ||
subscriptions | ||
SET | ||
status = 'confirmed' | ||
WHERE | ||
status IS NULL; | ||
|
||
ALTER TABLE | ||
subscriptions | ||
ALTER COLUMN | ||
status | ||
SET | ||
NOT NULL; | ||
|
||
COMMIT; |
6 changes: 6 additions & 0 deletions
6
migrations/20241102070203_create_subscription_tokens_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- Add migration script here | ||
CREATE TABLE subscription_tokens( | ||
subscription_token TEXT NOT NULL, | ||
subscriber_id uuid NOT NULL REFERENCES subscriptions (id), | ||
PRIMARY KEY (subscription_token) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod health_check; | ||
mod subscriptions; | ||
mod subscriptions_confirm; | ||
|
||
pub use health_check::*; | ||
pub use subscriptions::*; | ||
pub use subscriptions_confirm::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use axum::{ | ||
debug_handler, | ||
extract::{Query, State}, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use reqwest::StatusCode; | ||
use serde::Deserialize; | ||
use sqlx::PgPool; | ||
use uuid::Uuid; | ||
|
||
use crate::startup::AppState; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Parameters { | ||
subscription_token: String, | ||
} | ||
|
||
#[debug_handler] | ||
pub async fn confirm( | ||
State(state): State<AppState>, | ||
Query(params): Query<Parameters>, | ||
) -> Result<Response, StatusCode> { | ||
let id = match get_subscriber_id_from_token(&state.db_pool, ¶ms.subscription_token).await { | ||
Ok(id) => id, | ||
Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR), | ||
}; | ||
dbg!(id); | ||
match id { | ||
Some(subscriber_id) => { | ||
if confirm_subscriber(&state.db_pool, subscriber_id) | ||
.await | ||
.is_err() | ||
{ | ||
return Err(StatusCode::INTERNAL_SERVER_ERROR); | ||
} | ||
Ok((StatusCode::OK).into_response()) | ||
} | ||
None => Err(StatusCode::UNAUTHORIZED), | ||
} | ||
} | ||
|
||
pub async fn confirm_subscriber(pool: &PgPool, subscriber_id: Uuid) -> Result<(), sqlx::Error> { | ||
sqlx::query( | ||
r#" | ||
UPDATE subscriptions SET status = 'confirmed' WHERE id = $1 | ||
"#, | ||
) | ||
.bind(subscriber_id) | ||
.execute(pool) | ||
.await | ||
.map_err(|e| { | ||
tracing::error!("Failed to execute query: {:?}", e); | ||
e | ||
})?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(sqlx::FromRow, Debug, PartialEq, Eq)] | ||
pub struct SubscriberId(Uuid); | ||
|
||
async fn get_subscriber_id_from_token( | ||
pool: &PgPool, | ||
subscription_token: &str, | ||
) -> Result<Option<Uuid>, sqlx::Error> { | ||
let result: Option<SubscriberId> = sqlx::query_as( | ||
r#" | ||
SELECT subscriber_id FROM subscription_tokens WHERE subscription_token=$1 | ||
"#, | ||
) | ||
.bind(subscription_token) | ||
.fetch_optional(pool) | ||
.await | ||
.map_err(|e| { | ||
tracing::error!("Failed to execute query: {:?}", e); | ||
e | ||
})?; | ||
Ok(result.map(|r| r.0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.