Skip to content

Commit

Permalink
Restructured code organization so that there are router and service
Browse files Browse the repository at this point in the history
layers.
Also added create expense to the data api.
  • Loading branch information
nicolasauler committed Dec 24, 2023
1 parent 4bf6afc commit a427b45
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 88 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.

2 changes: 2 additions & 0 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod router;
pub mod service;
24 changes: 24 additions & 0 deletions src/data/router.rs
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
}
48 changes: 48 additions & 0 deletions src/data/service.rs
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())),
}
}
23 changes: 0 additions & 23 deletions src/data_router.rs

This file was deleted.

File renamed without changes.
2 changes: 2 additions & 0 deletions src/hypermedia/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod router;
pub mod service;
55 changes: 55 additions & 0 deletions src/hypermedia/router.rs
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
}
Loading

0 comments on commit a427b45

Please sign in to comment.