Skip to content

Commit

Permalink
Merge pull request #7 from smart--petea/issue_enum
Browse files Browse the repository at this point in the history
Issue enum
  • Loading branch information
vsilent authored Sep 24, 2023
2 parents eea7c55 + a8b3814 commit d6b3b81
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 52 deletions.
3 changes: 3 additions & 0 deletions src/forms/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod rating;

pub use rating::*;
14 changes: 14 additions & 0 deletions src/forms/rating.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::models;
use serde::{Deserialize, Serialize};
use serde_valid::Validate;

#[derive(Serialize, Deserialize, Debug, Validate)]
pub struct Rating {
pub obj_id: i32, // product external id
pub category: models::RateCategory, // rating of product | rating of service etc
#[validate(max_length = 1000)]
pub comment: Option<String>, // always linked to a product
#[validate(minimum = 0)]
#[validate(maximum = 10)]
pub rate: i32, //
}
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub mod routes;
pub mod startup;
pub mod configuration;
pub mod telemetry;
pub mod forms;
mod middleware;
mod models;
pub mod models;
pub mod routes;
pub mod services;
pub mod startup;
pub mod telemetry;
4 changes: 3 additions & 1 deletion src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod rating;
pub mod stack;
pub mod user;
pub mod stack;

pub use rating::*;
34 changes: 15 additions & 19 deletions src/models/rating.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use uuid::Uuid;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

pub struct Product {
// Product - is an external object that we want to store in the database,
Expand All @@ -10,40 +10,37 @@ pub struct Product {
// rating - is a rating of the product
// product type stack & app,
// id is generated based on the product type and external obj_id
pub id: i32, //primary key, for better data management
pub obj_id: u32, // external product ID db, no autoincrement, example: 100
pub obj_type: String, // stack | app, unique index
pub rating: Rating, // 0-10
// pub rules: Rules,
pub id: i32, //primary key, for better data management
pub obj_id: i32, // external product ID db, no autoincrement, example: 100
pub obj_type: String, // stack | app, unique index
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}

pub struct Rating {
pub id: i32,
pub user_id: Uuid, // external user_id, 100, taken using token (middleware?)
pub category: String, // rating of product | rating of service etc
pub comment: String, // always linked to a product
pub hidden: bool, // rating can be hidden for non-adequate user behaviour
pub user_id: Uuid, // external user_id, 100, taken using token (middleware?)
pub product_id: i32, //primary key, for better data management
pub category: String, // rating of product | rating of service etc
pub comment: String, // always linked to a product
pub hidden: bool, // rating can be hidden for non-adequate user behaviour
pub rate: u32,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}


#[derive(sqlx::Type)]
#[sqlx(rename_all = "lowercase", type_name = "category")]
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
#[derive(sqlx::Type, Serialize, Deserialize, Debug, Clone, Copy)]
#[sqlx(rename_all = "lowercase", type_name = "varchar")]
pub enum RateCategory {
Application, // app, feature, extension
Cloud, // is user satisfied working with this cloud
Stack, // app stack
Application, // app, feature, extension
Cloud, // is user satisfied working with this cloud
Stack, // app stack
DeploymentSpeed,
Documentation,
Design,
TechSupport,
Price,
MemoryUsage
MemoryUsage,
}

impl Into<String> for RateCategory {
Expand All @@ -57,4 +54,3 @@ pub struct Rules {
// example: allow to add only a single comment
comments_per_user: i32, // default = 1
}

65 changes: 37 additions & 28 deletions src/routes/rating.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,51 @@
use crate::forms;
use crate::models;
use crate::startup::AppState;
use actix_web::{web, HttpResponse};
use serde::{Deserialize, Serialize};
use crate::models::rating::RateCategory;
use serde_valid::Validate;
use sqlx::PgPool;
use tracing::instrument;
use uuid::Uuid;
use crate::startup::AppState;
use tracing::Instrument;
use uuid::Uuid;

// workflow
// add, update, list, get(user_id), ACL,
// ACL - access to func for a user
// ACL - access to objects for a user

#[derive(Serialize, Deserialize, Debug, Validate)]
pub struct RatingForm {
pub obj_id: i32, // product external id
pub category: RateCategory, // rating of product | rating of service etc
#[validate(max_length = 1000)]
pub comment: Option<String>, // always linked to a product
#[validate(minimum = 0)]
#[validate(maximum = 10)]
pub rate: i32, //
}

pub async fn rating(app_state: web::Data<AppState>, form: web::Json<RatingForm>, pool:
web::Data<PgPool>) -> HttpResponse {
pub async fn rating(
app_state: web::Data<AppState>,
form: web::Json<forms::Rating>,
pool: web::Data<PgPool>,
) -> HttpResponse {
//TODO. check if there already exists a rating for this product committed by this user
let request_id = Uuid::new_v4();
let user_id = app_state.user_id; // uuid Let's assume we have a user id already taken from auth

match sqlx::query_as!(
models::Product,
r"SELECT * FROM product WHERE obj_id = $1",
form.obj_id
)
.fetch_one(pool.get_ref())
.await
{
Ok(product) => {
tracing::info!("req_id: {} Found product: {:?}", request_id, product.obj_id);
}
Err(e) => {
tracing::error!(
"req_id: {} Failed to fetch product: {:?}, error: {:?}",
request_id,
form.obj_id,
e
);
return HttpResponse::InternalServerError().finish();
}
};

let query_span = tracing::info_span!(
"Saving new rating details in the database"
);
let user_id = app_state.user_id; // uuid Let's assume we have a user id already taken from auth
let query_span = tracing::info_span!("Saving new rating details in the database");
// Get product by id
// Insert rating
let category = Into::<String>::into(form.category.clone());
//let category = Into::<String>::into(form.category.clone());
match sqlx::query!(
r#"
INSERT INTO rating (user_id, product_id, category, comment, hidden,rate,
Expand All @@ -45,7 +55,7 @@ web::Data<PgPool>) -> HttpResponse {
"#,
user_id,
form.obj_id,
category.as_str(),
form.category as models::RateCategory,
form.comment,
false,
form.rate
Expand All @@ -55,6 +65,7 @@ web::Data<PgPool>) -> HttpResponse {
.await
{
Ok(_) => {
//TODO return json containing the id of the new rating
tracing::info!(
"req_id: {} New subscriber details have been saved to database",
request_id
Expand All @@ -65,7 +76,5 @@ web::Data<PgPool>) -> HttpResponse {
tracing::error!("req_id: {} Failed to execute query: {:?}", request_id, e);
HttpResponse::InternalServerError().finish()
}
};
println!("{:?}", form);
HttpResponse::Ok().finish()
}
}

0 comments on commit d6b3b81

Please sign in to comment.