-
Notifications
You must be signed in to change notification settings - Fork 19
Feature/currency preference #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ pub struct AuthRequest { | |
| pub struct AuthResponse { | ||
| pub id: i64, | ||
| pub username: String, | ||
| pub currency: String, | ||
| } | ||
|
|
||
| #[utoipa::path( | ||
|
|
@@ -65,6 +66,7 @@ pub async fn register( | |
| Ok(Json(AuthResponse { | ||
| id: result, | ||
| username: payload.username, | ||
| currency: "USD".to_string(), | ||
| })) | ||
| } | ||
|
|
||
|
|
@@ -87,8 +89,8 @@ pub async fn login( | |
| Json(payload): Json<AuthRequest>, | ||
| ) -> Result<impl IntoResponse, PaymeError> { | ||
| payload.validate()?; | ||
| let user: (i64, String, String) = | ||
| sqlx::query_as("SELECT id, username, password_hash FROM users WHERE username = ?") | ||
| let user: (i64, String, String, String) = | ||
| sqlx::query_as("SELECT id, username, password_hash, currency FROM users WHERE username = ?") | ||
| .bind(&payload.username) | ||
| .fetch_optional(&pool) | ||
| .await? | ||
|
|
@@ -128,6 +130,7 @@ pub async fn login( | |
| Json(AuthResponse { | ||
| id: user.0, | ||
| username: user.1, | ||
| currency: user.3, | ||
| }), | ||
| )) | ||
| } | ||
|
|
@@ -169,7 +172,7 @@ pub async fn me( | |
| State(pool): State<SqlitePool>, | ||
| axum::Extension(claims): axum::Extension<Claims>, | ||
| ) -> Result<Json<AuthResponse>, PaymeError> { | ||
| let user: (i64, String) = sqlx::query_as("SELECT id, username FROM users WHERE id = ?") | ||
| let user: (i64, String, String) = sqlx::query_as("SELECT id, username, currency FROM users WHERE id = ?") | ||
| .bind(claims.sub) | ||
| .fetch_optional(&pool) | ||
| .await? | ||
|
|
@@ -178,6 +181,7 @@ pub async fn me( | |
| Ok(Json(AuthResponse { | ||
| id: user.0, | ||
| username: user.1, | ||
| currency: user.2, // ADDED | ||
| })) | ||
| } | ||
|
|
||
|
|
@@ -240,9 +244,16 @@ pub async fn change_username( | |
| .execute(&pool) | ||
| .await?; | ||
|
|
||
| // ADDED: Fetch currency | ||
| let currency: (String,) = sqlx::query_as("SELECT currency FROM users WHERE id = ?") | ||
| .bind(claims.sub) | ||
| .fetch_one(&pool) | ||
| .await?; | ||
|
|
||
| Ok(Json(AuthResponse { | ||
| id: claims.sub, | ||
| username: payload.new_username, | ||
| currency: currency.0, // ADDED | ||
| })) | ||
| } | ||
|
|
||
|
|
@@ -359,3 +370,45 @@ pub async fn clear_all_data( | |
| Json(serde_json::json!({"message": "All data cleared"})), | ||
| )) | ||
| } | ||
| #[derive(Deserialize, ToSchema, Validate)] | ||
| pub struct ChangeCurrencyRequest { | ||
| #[validate(length(min = 3, max = 3))] | ||
| pub currency: String, | ||
| } | ||
|
|
||
|
Comment on lines
379
to
384
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows any three-character string (e.g. "FOO", "BAR", "$$$"). The backend should not accept values outside the supported currency set enforced by your frontend changes |
||
| #[utoipa::path( | ||
| put, | ||
| path = "/api/auth/change-currency", | ||
| request_body = ChangeCurrencyRequest, | ||
| responses( | ||
| (status = 200, description = "Currency changed successfully", body = AuthResponse), | ||
| (status = 500, description = "Internal server error") | ||
| ), | ||
| tag = "Auth", | ||
| summary = "Change currency preference", | ||
| description = "Updates the authenticated user's preferred display currency." | ||
| )] | ||
| pub async fn change_currency( | ||
| State(pool): State<SqlitePool>, | ||
| axum::Extension(claims): axum::Extension<Claims>, | ||
| Json(payload): Json<ChangeCurrencyRequest>, | ||
| ) -> Result<Json<AuthResponse>, PaymeError> { | ||
| payload.validate()?; | ||
|
|
||
| sqlx::query("UPDATE users SET currency = ? WHERE id = ?") | ||
| .bind(&payload.currency) | ||
| .bind(claims.sub) | ||
| .execute(&pool) | ||
| .await?; | ||
|
|
||
| let user: (String,) = sqlx::query_as("SELECT username FROM users WHERE id = ?") | ||
|
||
| .bind(claims.sub) | ||
| .fetch_one(&pool) | ||
| .await?; | ||
|
|
||
| Ok(Json(AuthResponse { | ||
| id: claims.sub, | ||
| username: user.0, | ||
| currency: payload.currency, | ||
| })) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
// ADDEDcomments don't seem to be needed? Same with line 184 above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed on latest PR