Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
95ccbd4
issue-auth new approach for JsonResponse
smart--petea Dec 12, 2023
65d6f43
issue-auth json. unauthorized response
smart--petea Dec 12, 2023
6630ab7
issue-auth src/middleware/trydirect.rs
smart--petea Dec 12, 2023
899b8f8
issue-auth src/routes/client/add.rs
smart--petea Dec 12, 2023
892ff9d
issue-auth enable client
smart--petea Dec 12, 2023
9989b11
issue-auth client logic refactor
smart--petea Dec 13, 2023
ee200c4
issue-auth src/routes/client/update.rs
smart--petea Dec 13, 2023
f1cba7e
issue-auth
smart--petea Dec 13, 2023
3ba9b90
issue-auth renamed client/mod.rs into client.rs
smart--petea Dec 13, 2023
33d9cd8
issue-auth working on src/routes/rating/add.rs
smart--petea Dec 13, 2023
ccd1340
issue-auth message product
smart--petea Dec 14, 2023
c8fca0b
issue-auth src/routes/rating/add.rs
smart--petea Dec 14, 2023
3453c3e
issue-auth models into files
smart--petea Dec 16, 2023
dd202db
issue-auth postgres enum rate_category
smart--petea Dec 16, 2023
412d0da
issue-auth RateCategory enum saved in db
smart--petea Dec 16, 2023
ecf28fe
issue-auth stack get refactor
smart--petea Dec 17, 2023
e660098
issue-auth new command. updated
smart--petea Dec 18, 2023
9e4042a
issue-auth routes/stack/get.rs
smart--petea Dec 18, 2023
8fbf9d7
issue-auth routes/stack/add.rs
smart--petea Dec 18, 2023
985a1a5
issue-auth src/routes/stack/add.rs
smart--petea Dec 18, 2023
fa8f86e
issue-auth src/routes/stack/add.rs
smart--petea Dec 18, 2023
46aa43d
issue-auth src/routes/stack/add.rs
smart--petea Dec 18, 2023
295f36c
issue-auth client db. api
smart--petea Dec 20, 2023
7a3ca1a
issue-auth product db api
smart--petea Dec 20, 2023
da380cf
issue-auth rating db api
smart--petea Dec 20, 2023
0262171
issue-auth db::rating::fetch_by_obj_and_user_and_category
smart--petea Dec 21, 2023
a4998fc
issue-auth db::stack::fetch
smart--petea Dec 21, 2023
2e6f5e3
issue-auth db::stack::insert
smart--petea Dec 23, 2023
4f13c4c
issue-auth removed tracings
smart--petea Dec 23, 2023
a10eca7
issue-auth src/routes/stack.add.rs internal server error
smart--petea Dec 23, 2023
e569835
issue-auth check_if_stack_exists
smart--petea Dec 23, 2023
32b571d
issue-auth convert body to form
smart--petea Dec 23, 2023
0626be0
issue-auth body_to_form
smart--petea Dec 23, 2023
63fa86b
issue-auth json error path
smart--petea Dec 23, 2023
623e5dd
issue-auth removed comments
smart--petea Dec 23, 2023
a197ba5
issue-auth stack/compose.rs add
smart--petea Dec 23, 2023
335b62a
issue-auth compose.rs admin route
smart--petea Dec 23, 2023
6d6bc2e
issue-auth compose logic. optimized
smart--petea Dec 23, 2023
a77681c
issue-auth compose logic
smart--petea Dec 23, 2023
d584a68
issue-auth optimisation
smart--petea Dec 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ serde_yaml = "0.9"
lapin = { version = "2.3.1", features = ["serde_json"] }
futures-lite = "1.13.0"
clap = { version = "4.4.8", features = ["derive"] }
serde_path_to_error = "0.1.14"

[dependencies.sqlx]
version = "0.6.3"
Expand Down
2 changes: 2 additions & 0 deletions migrations/20230903063840_creating_rating_tables.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ DROP INDEX idx_obj_id_rating_id;

DROP table rating;
DROP table product;

DROP TYPE rate_category;
14 changes: 13 additions & 1 deletion migrations/20230903063840_creating_rating_tables.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
-- Add up migration script here

CREATE TYPE rate_category AS ENUM (
'application',
'cloud',
'stack',
'deploymentSpeed',
'documentation',
'design',
'techSupport',
'price',
'memoryUsage'
);

CREATE TABLE product (
id integer NOT NULL, PRIMARY KEY(id),
obj_id integer NOT NULL,
Expand All @@ -12,7 +24,7 @@ CREATE TABLE rating (
id serial,
user_id VARCHAR(50) NOT NULL,
obj_id integer NOT NULL,
category VARCHAR(255) NOT NULL,
category rate_category NOT NULL,
comment TEXT DEFAULT NULL,
hidden BOOLEAN DEFAULT FALSE,
rate INTEGER,
Expand Down
2 changes: 1 addition & 1 deletion src/console/commands/appclient/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl crate::console::commands::CallableTrait for NewCommand {

//todo get user from trydirect
let user = crate::models::user::User {
id: "first_name".to_string(),
id: format!("{}", self.user_id),
first_name: "first_name".to_string(),
last_name: "last_name".to_string(),
email: "email".to_string(),
Expand Down
105 changes: 105 additions & 0 deletions src/db/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use sqlx::PgPool;
use crate::models;
use tracing::Instrument;

pub async fn update(pool: &PgPool, client: models::Client) -> Result<models::Client, String> {
let query_span = tracing::info_span!("Updating client into the database");
sqlx::query!(
r#"
UPDATE client
SET
secret=$1,
updated_at=NOW() at time zone 'utc'
WHERE id = $2
"#,
client.secret,
client.id
)
.execute(pool)
.instrument(query_span)
.await
.map(|_|{
tracing::info!("Client {} have been saved to database", client.id);
client
})
.map_err(|err| {
tracing::error!("Failed to execute query: {:?}", err);
"".to_string()
})
}

pub async fn fetch(pool: &PgPool, id: i32) -> Result<Option<models::Client>, String> {
let query_span = tracing::info_span!("Fetching the client by ID");
sqlx::query_as!(
models::Client,
r#"
SELECT
id,
user_id,
secret
FROM client c
WHERE c.id = $1
LIMIT 1
"#,
id,
)
.fetch_one(pool)
.instrument(query_span)
.await
.map(|client| Some(client))
.or_else(|e| {
match e {
sqlx::Error::RowNotFound => Ok(None),
s => {
tracing::error!("Failed to execute fetch query: {:?}", s);
Err("".to_string())
}
}
})
}

pub async fn count_by_user(pool: &PgPool , user_id: &String) -> Result<i64, String> {
let query_span = tracing::info_span!("Counting the user's clients");

sqlx::query!(
r#"
SELECT
count(*) as client_count
FROM client c
WHERE c.user_id = $1
"#,
user_id.clone(),
)
.fetch_one(pool)
.instrument(query_span)
.await
.map(|result| {result.client_count.unwrap()})
.map_err(|err| {
tracing::error!("Failed to execute query: {:?}", err);
"Internal Server Error".to_string()
})
}

pub async fn insert(pool: &PgPool, mut client: models::Client) -> Result<models::Client, String> {
let query_span = tracing::info_span!("Saving new client into the database");
sqlx::query!(
r#"
INSERT INTO client (user_id, secret, created_at, updated_at)
VALUES ($1, $2, NOW() at time zone 'utc', NOW() at time zone 'utc')
RETURNING id
"#,
client.user_id.clone(),
client.secret,
)
.fetch_one(pool)
.instrument(query_span)
.await
.map(move |result| {
client.id = result.id;
client
})
.map_err(|e| {
tracing::error!("Failed to execute query: {:?}", e);
"Failed to insert".to_string()
})
}
4 changes: 4 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod client;
pub mod product;
pub mod rating;
pub mod stack;
30 changes: 30 additions & 0 deletions src/db/product.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use sqlx::PgPool;
use crate::models;
use tracing::Instrument;

pub async fn fetch_by_obj(pg_pool: &PgPool, obj_id: i32) -> Result<Option<models::Product>, String> {
let query_span = tracing::info_span!("Check product existence by id.");
sqlx::query_as!(
models::Product,
r#"SELECT
*
FROM product
WHERE obj_id = $1
LIMIT 1
"#,
obj_id
)
.fetch_one(pg_pool)
.instrument(query_span)
.await
.map(|product| Some(product))
.or_else(|e| {
match e {
sqlx::Error::RowNotFound => Ok(None),
s => {
tracing::error!("Failed to execute fetch query: {:?}", s);
Err("".to_string())
}
}
})
}
133 changes: 133 additions & 0 deletions src/db/rating.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use sqlx::PgPool;
use crate::models;
use tracing::Instrument;

pub async fn fetch_all(pool: &PgPool) -> Result<Vec<models::Rating>, String> {
let query_span = tracing::info_span!("Fetch all ratings.");
sqlx::query_as!(
models::Rating,
r#"SELECT
id,
user_id,
obj_id,
category as "category: _",
comment,
hidden,
rate,
created_at,
updated_at
FROM rating"#
)
.fetch_all(pool)
.instrument(query_span)
.await
.map_err(|e| {
tracing::error!("Failed to execute fetch query: {:?}", e);
"".to_string()
})
}

pub async fn fetch(pool: &PgPool, id: i32) -> Result<Option<models::Rating>, String> {
let query_span = tracing::info_span!("Fetch rating by id");
sqlx::query_as!(
models::Rating,
r#"SELECT
id,
user_id,
obj_id,
category as "category: _",
comment,
hidden,
rate,
created_at,
updated_at
FROM rating
WHERE id=$1
LIMIT 1"#,
id
)
.fetch_one(pool)
.instrument(query_span)
.await
.map(|rating| Some(rating))
.or_else(|e| {
match e {
sqlx::Error::RowNotFound => Ok(None),
s => {
tracing::error!("Failed to execute fetch query: {:?}", s);
Err("".to_string())
}
}
})
}

pub async fn fetch_by_obj_and_user_and_category(
pool: &PgPool,
obj_id: i32,
user_id: String,
category: models::RateCategory,
) -> Result<Option<models::Rating>, String> {
let query_span = tracing::info_span!("Fetch rating by obj, user and category.");
sqlx::query_as!(
models::Rating,
r#"SELECT
id,
user_id,
obj_id,
category as "category: _",
comment,
hidden,
rate,
created_at,
updated_at
FROM rating
WHERE user_id=$1
AND obj_id=$2
AND category=$3
LIMIT 1"#,
user_id,
obj_id,
category as _
)
.fetch_one(pool)
.instrument(query_span)
.await
.map(|rating| Some(rating))
.or_else(|e| {
match e {
sqlx::Error::RowNotFound => Ok(None),
s => {
tracing::error!("Failed to execute fetch query: {:?}", s);
Err("".to_string())
}
}
})
}

pub async fn insert(pool: &PgPool, mut rating: models::Rating) -> Result<models::Rating, String> {
let query_span = tracing::info_span!("Saving new rating details into the database");
sqlx::query!(
r#"
INSERT INTO rating (user_id, obj_id, category, comment, hidden, rate, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, NOW() at time zone 'utc', NOW() at time zone 'utc')
RETURNING id
"#,
rating.user_id,
rating.obj_id,
rating.category as _,
rating.comment,
rating.hidden,
rating.rate
)
.fetch_one(pool)
.instrument(query_span)
.await
.map(move |result| {
rating.id = result.id;
rating
})
.map_err(|e| {
tracing::error!("Failed to execute query: {:?}", e);
"Failed to insert".to_string()
})
}
Loading