mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 01:38:44 +00:00
report rating count on rating
This commit is contained in:
1 parent
291b4a2ff9
commit
cd583e3b91
4 files changed
+84
No files matched your search
+9
@@ -39,4 +39,13 @@ impl UserRatingEntity {
|
||||
UserRatingEntity::W2lTemplate(token) => token.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_composite_keys(&self) -> (UserRatingEntityType, &str) {
|
||||
match self {
|
||||
UserRatingEntity::MediaFile(token) => (UserRatingEntityType::MediaFile, token.as_str()),
|
||||
UserRatingEntity::ModelWeight(token) => (UserRatingEntityType::ModelWeight, token.as_str()),
|
||||
UserRatingEntity::TtsModel(token) => (UserRatingEntityType::TtsModel, token.as_str()),
|
||||
UserRatingEntity::W2lTemplate(token) => (UserRatingEntityType::W2lTemplate, token.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
use anyhow::anyhow;
|
||||
use log::warn;
|
||||
use sqlx::{Executor, MySql};
|
||||
use sqlx::pool::PoolConnection;
|
||||
|
||||
use errors::AnyhowResult;
|
||||
|
||||
use crate::composite_keys::by_table::user_ratings::user_rating_entity::UserRatingEntity;
|
||||
|
||||
pub struct PositiveRatingCount {
|
||||
pub positive_count : usize,
|
||||
}
|
||||
|
||||
pub async fn get_total_user_rating_count_for_entity(
|
||||
user_rating_entity: &UserRatingEntity,
|
||||
mysql_connection: &mut PoolConnection<MySql>,
|
||||
) -> AnyhowResult<PositiveRatingCount> {
|
||||
|
||||
let (entity_type, entity_token) = user_rating_entity.get_composite_keys();
|
||||
|
||||
let maybe_result = sqlx::query_as!(
|
||||
RawRecord,
|
||||
r#"
|
||||
SELECT
|
||||
COUNT(*) as positive_count
|
||||
FROM
|
||||
user_ratings
|
||||
WHERE
|
||||
entity_type = ?
|
||||
AND entity_token = ?
|
||||
AND rating_value = 'positive'
|
||||
"#,
|
||||
entity_type,
|
||||
entity_token
|
||||
)
|
||||
.fetch_one(&mut **mysql_connection)
|
||||
.await;
|
||||
|
||||
match maybe_result {
|
||||
Err(err) => match err {
|
||||
sqlx::Error::RowNotFound => Ok(PositiveRatingCount { positive_count: 0 }),
|
||||
_ => {
|
||||
warn!("get_total_user_rating_count db error: {:?}", err);
|
||||
Err(anyhow!("error with query: {:?}", err))
|
||||
}
|
||||
},
|
||||
Ok(result) => Ok(result.into_public_type()),
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RawRecord {
|
||||
positive_count: i64,
|
||||
}
|
||||
|
||||
impl RawRecord {
|
||||
pub fn into_public_type(self) -> PositiveRatingCount {
|
||||
PositiveRatingCount {
|
||||
positive_count: self.positive_count as usize,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod get_total_user_rating_count_for_entity;
|
||||
pub mod get_user_rating;
|
||||
pub mod update_tts_model_ratings;
|
||||
pub mod upsert_user_rating;
|
||||
+13
@@ -10,6 +10,7 @@ use enums::by_table::user_ratings::rating_value::UserRatingValue;
|
||||
use http_server_common::request::get_request_ip::get_request_ip;
|
||||
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
|
||||
use mysql_queries::composite_keys::by_table::user_ratings::user_rating_entity::UserRatingEntity;
|
||||
use mysql_queries::queries::users::user_ratings::get_total_user_rating_count_for_entity::get_total_user_rating_count_for_entity;
|
||||
use mysql_queries::queries::users::user_ratings::update_tts_model_ratings::update_tts_model_ratings;
|
||||
use mysql_queries::queries::users::user_ratings::upsert_user_rating::{Args, upsert_user_rating};
|
||||
use tokens::tokens::media_files::MediaFileToken;
|
||||
@@ -39,6 +40,10 @@ pub struct SetUserRatingRequest {
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub struct SetUserRatingResponse {
|
||||
pub success: bool,
|
||||
|
||||
/// This is the new positive rating count (across all users) for the entity in question.
|
||||
/// This does not include negative ratings.
|
||||
pub new_positive_rating_count_for_entity: usize,
|
||||
}
|
||||
|
||||
// =============== Error Response ===============
|
||||
@@ -179,8 +184,16 @@ pub async fn set_user_rating_handler(
|
||||
}
|
||||
}
|
||||
|
||||
let count = get_total_user_rating_count_for_entity(&entity, &mut mysql_connection)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
error!("Error getting total user rating count for entity: {:?}", err);
|
||||
SetUserRatingError::ServerError
|
||||
})?;
|
||||
|
||||
let response = SetUserRatingResponse {
|
||||
success: true,
|
||||
new_positive_rating_count_for_entity: count.positive_count,
|
||||
};
|
||||
|
||||
let body = serde_json::to_string(&response)
|
||||
|
||||
Reference in new issue
Block a user