mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-15 14:22:14 +00:00
port storyteller-web query: insert w2l template upload job
This commit is contained in:
1 parent
910caa1000
commit
e2b98491cb
3 files changed
+78
-68
No files matched your search
+60
@@ -0,0 +1,60 @@
|
||||
use anyhow::anyhow;
|
||||
use sqlx::MySqlPool;
|
||||
use container_common::anyhow_result::AnyhowResult;
|
||||
use sqlx::error::Error::Database;
|
||||
use crate::tokens::Tokens;
|
||||
|
||||
pub struct InsertW2lTemplateUploadJobArgs<'a> {
|
||||
pub uuid: &'a str,
|
||||
pub creator_user_token: &'a str,
|
||||
pub creator_ip_address: &'a str,
|
||||
pub creator_set_visibility: &'a str,
|
||||
pub title: &'a str,
|
||||
pub template_type: &'a str,
|
||||
pub download_url: &'a str,
|
||||
pub mysql_pool: &'a MySqlPool,
|
||||
}
|
||||
|
||||
// NB: Returns the new token
|
||||
pub async fn insert_w2l_template_upload_job(args: InsertW2lTemplateUploadJobArgs<'_>) -> AnyhowResult<String> {
|
||||
// This token is returned to the client.
|
||||
let job_token = Tokens::new_w2l_template_upload_job()
|
||||
.map_err(|_e| {
|
||||
anyhow!("error creating token")
|
||||
})?;
|
||||
|
||||
let query_result = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO w2l_template_upload_jobs
|
||||
SET
|
||||
token = ?,
|
||||
uuid_idempotency_token = ?,
|
||||
creator_user_token = ?,
|
||||
creator_ip_address = ?,
|
||||
creator_set_visibility = ?,
|
||||
title = ?,
|
||||
template_type = ?,
|
||||
download_url = ?,
|
||||
status = "pending"
|
||||
"#,
|
||||
job_token.to_string(),
|
||||
args.uuid.to_string(),
|
||||
args.creator_user_token.to_string(),
|
||||
args.creator_ip_address.to_string(),
|
||||
args.creator_set_visibility.to_string(),
|
||||
args.title.to_string(),
|
||||
args.template_type,
|
||||
args.download_url,
|
||||
)
|
||||
.execute(args.mysql_pool)
|
||||
.await;
|
||||
|
||||
let _record_id = match query_result {
|
||||
Ok(res) => res.last_insert_id(),
|
||||
Err(err) => {
|
||||
return Err(anyhow!("failure to create w2l template upload job: {:?}", err));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(job_token)
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod get_w2l_template_upload_job_status;
|
||||
pub mod get_w2l_template_upload_job_status;
|
||||
pub mod insert_w2l_template_upload_job;
|
||||
+16
-67
@@ -10,11 +10,10 @@ use config::is_bad_video_download_url::is_bad_video_download_url;
|
||||
use crate::http_server::web_utils::response_error_helpers::to_simple_json_error;
|
||||
use crate::server_state::ServerState;
|
||||
use crate::validations::model_uploads::validate_model_title;
|
||||
use database_queries::tokens::Tokens;
|
||||
use database_queries::queries::w2l::w2l_template_upload_jobs::insert_w2l_template_upload_job::{insert_w2l_template_upload_job, InsertW2lTemplateUploadJobArgs};
|
||||
use enums::common::visibility::Visibility;
|
||||
use http_server_common::request::get_request_ip::get_request_ip;
|
||||
use log::{info, warn};
|
||||
use sqlx::error::Error::Database;
|
||||
use log::warn;
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
use user_input_common::check_for_slurs::contains_slurs;
|
||||
@@ -133,70 +132,20 @@ pub async fn upload_w2l_template_handler(
|
||||
let template_type = "unknown".to_string();
|
||||
let creator_set_visibility = "public".to_string();
|
||||
|
||||
// This token is returned to the client.
|
||||
let job_token = Tokens::new_w2l_template_upload_job()
|
||||
.map_err(|_e| {
|
||||
warn!("Error creating token");
|
||||
UploadW2lTemplateError::ServerError
|
||||
})?;
|
||||
|
||||
|
||||
let query_result = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO w2l_template_upload_jobs
|
||||
SET
|
||||
token = ?,
|
||||
uuid_idempotency_token = ?,
|
||||
creator_user_token = ?,
|
||||
creator_ip_address = ?,
|
||||
creator_set_visibility = ?,
|
||||
title = ?,
|
||||
template_type = ?,
|
||||
download_url = ?,
|
||||
status = "pending"
|
||||
"#,
|
||||
job_token.to_string(),
|
||||
uuid.to_string(),
|
||||
user_session.user_token.to_string(),
|
||||
ip_address.to_string(),
|
||||
creator_set_visibility.to_string(),
|
||||
title.to_string(),
|
||||
template_type,
|
||||
download_url,
|
||||
)
|
||||
.execute(&server_state.mysql_pool)
|
||||
.await;
|
||||
|
||||
let record_id = match query_result {
|
||||
Ok(res) => {
|
||||
res.last_insert_id()
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("New w2l template upload creation DB error: {:?}", err);
|
||||
|
||||
// NB: SQLSTATE[23000]: Integrity constraint violation
|
||||
// NB: MySQL Error Code 1062: Duplicate key insertion (this is harder to access)
|
||||
match err {
|
||||
Database(err) => {
|
||||
let _maybe_code = err.code().map(|c| c.into_owned());
|
||||
/*match maybe_code.as_deref() {
|
||||
Some("23000") => {
|
||||
if err.message().contains("username") {
|
||||
return Err(UsernameTaken);
|
||||
} else if err.message().contains("email_address") {
|
||||
return Err(EmailTaken);
|
||||
}
|
||||
}
|
||||
_ => {},
|
||||
}*/
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
return Err(UploadW2lTemplateError::ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
info!("new w2l template upload job id: {}", record_id);
|
||||
let job_token = insert_w2l_template_upload_job(InsertW2lTemplateUploadJobArgs {
|
||||
uuid: &uuid,
|
||||
creator_user_token: user_session.user_token.as_str(),
|
||||
creator_ip_address: &ip_address,
|
||||
creator_set_visibility: &creator_set_visibility,
|
||||
title: &title,
|
||||
template_type: &template_type,
|
||||
download_url: &download_url,
|
||||
mysql_pool: &server_state.mysql_pool,
|
||||
}).await
|
||||
.map_err(|err| {
|
||||
warn!("New w2l template upload creation DB error: {:?}", err);
|
||||
UploadW2lTemplateError::ServerError
|
||||
})?;
|
||||
|
||||
server_state.firehose_publisher.enqueue_w2l_template_upload(&user_session.user_token, &job_token)
|
||||
.await
|
||||
|
||||
Reference in new issue
Block a user