-
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.
feat: add initial wisdoms and way to retrieve it
- Loading branch information
1 parent
a9cb02a
commit 4e8720e
Showing
14 changed files
with
124 additions
and
31 deletions.
There are no files selected for viewing
Empty file.
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 @@ | ||
BEGIN TRANSACTION; | ||
|
||
DELETE FROM wisdoms WHERE id IN (1, 2, 3); | ||
|
||
COMMIT; |
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,7 @@ | ||
BEGIN TRANSACTION; | ||
|
||
INSERT INTO wisdoms (id, description) VALUES (1, 'Patience is a virtue.'); | ||
INSERT INTO wisdoms (id, description) VALUES (2, 'Knowledge is power.'); | ||
INSERT INTO wisdoms (id, description) VALUES (3, 'Time is money.'); | ||
|
||
COMMIT; |
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,31 @@ | ||
pub type Result<T> = core::result::Result<T, Error>; | ||
|
||
use sqlx::postgres::PgPoolOptions; | ||
use tracing_fast_dev::tfd; | ||
|
||
pub type Database = sqlx::Pool<sqlx::Postgres>; | ||
|
||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
FailedConnectingToDatabase { error: String }, | ||
} | ||
|
||
impl From<sqlx::Error> for Error { | ||
fn from(value: sqlx::Error) -> Self { | ||
Self::FailedConnectingToDatabase { | ||
error: value.to_string(), | ||
} | ||
} | ||
} | ||
|
||
pub async fn connect(db_url: &str) -> Result<Database> { | ||
let pool: Database = PgPoolOptions::new() | ||
// TODO: Max connections! Dynamic? | ||
.max_connections(5) | ||
.connect(db_url) | ||
.await?; | ||
tfd().info("CONNECTED", "DATABASE"); | ||
|
||
Ok(pool) | ||
} |
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,31 +1,5 @@ | ||
pub type Result<T> = core::result::Result<T, Error>; | ||
mod queries; | ||
mod connect; | ||
|
||
|
||
use sqlx::postgres::PgPoolOptions; | ||
use tracing_fast_dev::tfd; | ||
|
||
pub type Database = sqlx::Pool<sqlx::Postgres>; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
FailedConnectingToDatabase { error: String }, | ||
} | ||
|
||
impl From<sqlx::Error> for Error { | ||
fn from(value: sqlx::Error) -> Self { | ||
Self::FailedConnectingToDatabase { | ||
error: value.to_string(), | ||
} | ||
} | ||
} | ||
|
||
pub async fn connect(db_url: &str) -> Result<Database> { | ||
let pool: Database = PgPoolOptions::new() | ||
// TODO: Max connections! Dynamic? | ||
.max_connections(5) | ||
.connect(db_url) | ||
.await?; | ||
tfd().info("CONNECTED", "DATABASE"); | ||
|
||
Ok(pool) | ||
} | ||
pub use queries::*; | ||
pub use connect::*; |
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,10 @@ | ||
use crate::models::Wisdom; | ||
use super::connect::Database; | ||
|
||
pub async fn _get_wisdoms(db: &Database) -> Result<Vec<Wisdom>, sqlx::Error> { | ||
tracing_fast_dev::tfd().info("GET_WISDOMS_INTERNAL", "QUERY"); | ||
let wisdoms: Vec<Wisdom> = sqlx::query_as("SELECT * from wisdoms") | ||
.fetch_all(db) | ||
.await?; | ||
Ok(wisdoms) | ||
} |
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,10 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{Response, StatusCode}, | ||
response::IntoResponse, | ||
}; | ||
pub fn default_handle_error<E: std::fmt::Display>(e: E) -> Response<Body> { | ||
let mut response = (StatusCode::INTERNAL_SERVER_ERROR).into_response(); | ||
response.extensions_mut().insert(e.to_string()); | ||
response | ||
} |
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,12 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct CreateWisdomDTO { | ||
pub description: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct UpdateWisdomDTO { | ||
pub id: i32, | ||
pub description: String, | ||
} |
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 @@ | ||
mod dto; | ||
mod wisdom; | ||
|
||
pub use dto::*; | ||
pub use wisdom::*; |
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,8 @@ | ||
use serde::Serialize; | ||
use sqlx::prelude::FromRow; | ||
|
||
#[derive(Debug, FromRow, Serialize)] | ||
pub struct Wisdom { | ||
pub id: i32, | ||
pub description: String, | ||
} |
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,25 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::{http::StatusCode, response::IntoResponse, routing::get, Extension, Json, Router}; | ||
|
||
use crate::{db::_get_wisdoms, helpers::default_handle_error, state::AppState}; | ||
|
||
use serde_json::json; | ||
|
||
pub fn routes() -> Router { | ||
Router::new().nest("/wisdoms", _routes()) | ||
} | ||
|
||
fn _routes() -> Router { | ||
Router::new() | ||
.route("/", get(get_wisdoms)) | ||
} | ||
|
||
|
||
async fn get_wisdoms(Extension(state): Extension<Arc<AppState>>) -> impl IntoResponse { | ||
tracing_fast_dev::tfd().info("GET_WISDOM", "FUNCTION"); | ||
match _get_wisdoms(&state.db).await { | ||
Ok(wisdoms) => (StatusCode::OK, Json(json!({ "wisdoms": wisdoms }))).into_response(), | ||
Err(e) => default_handle_error(e), | ||
} | ||
} |
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 @@ | ||
use crate::db::Database; | ||
|
||
|
||
#[derive(Clone)] | ||
pub struct AppState { | ||
pub db: Database, | ||
|