-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructured code organization so that there are router and service
layers. Also added create expense to the data api.
- Loading branch information
1 parent
4bf6afc
commit a427b45
Showing
13 changed files
with
297 additions
and
88 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
.sqlx/query-2ecafd9d41c252f48b0ada5b327fd851cda1011770d4e70c29e65c4b36bab74a.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
.sqlx/query-fbb682265b897346c802c489d79e2ddd5d7fcb8e4888d46d7c54e5684b2baaee.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod router; | ||
pub mod service; |
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,24 @@ | ||
use axum::{routing::get, Json}; | ||
use std::sync::Arc; | ||
|
||
use askama_axum::IntoResponse; | ||
use axum::{extract::State, Router}; | ||
|
||
use crate::{schema::UpdateExpense, AppState}; | ||
|
||
pub fn data_router() -> Router<Arc<AppState>> { | ||
Router::new().route("/api/expenses", get(get_expenses).post(create_expense)) | ||
} | ||
|
||
pub async fn get_expenses( | ||
State(shared_state): State<Arc<AppState>>, | ||
) -> Result<impl IntoResponse, impl IntoResponse> { | ||
super::service::get_expenses(&shared_state.pool).await | ||
} | ||
|
||
pub async fn create_expense( | ||
State(shared_state): State<Arc<AppState>>, | ||
Json(create_expense): Json<UpdateExpense>, | ||
) -> Result<impl IntoResponse, impl IntoResponse> { | ||
super::service::create_expense(&shared_state.pool, Json(create_expense)).await | ||
} |
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,48 @@ | ||
use askama_axum::IntoResponse; | ||
use axum::{http::StatusCode, Json}; | ||
use sqlx::{Pool, Postgres}; | ||
|
||
use crate::schema::{Expense, ExpenseType, UpdateExpense}; | ||
|
||
pub async fn get_expenses( | ||
db_pool: &Pool<Postgres>, | ||
) -> Result<impl IntoResponse, impl IntoResponse> { | ||
match sqlx::query_as!( | ||
Expense, | ||
r#"SELECT id, description, price, expense_type as "expense_type: ExpenseType", is_essencial, date | ||
FROM expenses | ||
ORDER BY date ASC"#, | ||
) | ||
.fetch_all(db_pool) | ||
.await | ||
{ | ||
Ok(expenses) => Ok((StatusCode::CREATED, Json(expenses))), | ||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), | ||
} | ||
} | ||
|
||
pub async fn create_expense( | ||
db_pool: &Pool<Postgres>, | ||
Json(create_expense): Json<UpdateExpense>, | ||
) -> Result<impl IntoResponse, impl IntoResponse> { | ||
match sqlx::query_as!( | ||
Expense, | ||
r#" | ||
INSERT INTO expenses | ||
(description, price, expense_type, is_essencial, date) | ||
VALUES ($1, $2, $3 :: expense_type, $4, $5) | ||
RETURNING id, description, price, expense_type as "expense_type: ExpenseType", is_essencial, date | ||
"#, | ||
create_expense.description, | ||
create_expense.price, | ||
create_expense.expense_type as Option<ExpenseType>, | ||
create_expense.is_essencial, | ||
create_expense.date | ||
) | ||
.fetch_one(db_pool) | ||
.await | ||
{ | ||
Ok(expense) => Ok((StatusCode::CREATED, Json(expense))), | ||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,2 @@ | ||
pub mod router; | ||
pub mod service; |
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,55 @@ | ||
use crate::{ | ||
schema::{GetExpense, UpdateExpense}, | ||
AppState, ExpensesTemplate, | ||
}; | ||
use std::sync::Arc; | ||
|
||
use askama_axum::IntoResponse; | ||
use axum::{ | ||
extract::{Path, Query, State}, | ||
routing::get, | ||
Json, Router, | ||
}; | ||
|
||
pub fn hypermedia_router() -> Router<Arc<AppState>> { | ||
Router::new() | ||
.route("/", get(expenses_index)) | ||
.route("/expenses", get(get_expenses)) | ||
.route("/expenses/:id/edit", get(edit_expense)) | ||
.route("/expenses/:id", get(get_expense).put(update_expense)) | ||
} | ||
|
||
pub async fn expenses_index() -> impl IntoResponse { | ||
ExpensesTemplate { | ||
..Default::default() | ||
} | ||
} | ||
|
||
pub async fn get_expenses( | ||
State(shared_state): State<Arc<AppState>>, | ||
Query(get_expense_input): Query<GetExpense>, | ||
) -> impl IntoResponse { | ||
super::service::get_expenses(&shared_state.pool, Query(get_expense_input)).await | ||
} | ||
|
||
pub async fn edit_expense( | ||
Path(id): Path<i32>, | ||
State(shared_state): State<Arc<AppState>>, | ||
) -> impl IntoResponse { | ||
super::service::edit_expense(&shared_state.pool, Path(id)).await | ||
} | ||
|
||
pub async fn get_expense( | ||
Path(id): Path<i32>, | ||
State(shared_state): State<Arc<AppState>>, | ||
) -> impl IntoResponse { | ||
super::service::get_expense(&shared_state.pool, Path(id)).await | ||
} | ||
|
||
pub async fn update_expense( | ||
Path(id): Path<i32>, | ||
State(shared_state): State<Arc<AppState>>, | ||
Json(update_expense): Json<UpdateExpense>, | ||
) -> impl IntoResponse { | ||
super::service::update_expense(&shared_state.pool, Path(id), Json(update_expense)).await | ||
} |
Oops, something went wrong.