add campaings

This commit is contained in:
Aravinth Manivannan 2021-10-11 13:57:07 +05:30
parent f1bc63d2c1
commit 5f3c21a9a9
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 74 additions and 29 deletions

View file

@ -1,20 +1,5 @@
{
"db": "PostgreSQL",
"07dd9f4c2edd99714b3de90365fdae4f874c66a736c308df6e668ef9b86737dc": {
"query": "INSERT INTO survey_responses (\n user_id, \n device_user_provided,\n device_software_recognised,\n threads\n ) VALUES ($1, $2, $3, $4);",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Uuid",
"Varchar",
"Varchar",
"Int4"
]
},
"nullable": []
}
},
"0d22134cc5076304b7895827f006ee8269cc500f400114a7472b83f0f1c568b5": {
"query": "INSERT INTO survey_admins \n (name , password, secret) VALUES ($1, $2, $3)",
"describe": {
@ -102,6 +87,22 @@
"nullable": []
}
},
"3e31af624e06a2261fa85f25b5ec4c62a8408cd7283042e24b2aee2998931426": {
"query": "\nINSERT INTO survey_campaigns (\n user_id, ID, name, difficulties, created_at\n ) VALUES(\n (SELECT id FROM survey_admins WHERE name = $1),\n $2, $3, $4, $5\n );",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Uuid",
"Varchar",
"Int4Array",
"Timestamptz"
]
},
"nullable": []
}
},
"43b3e771f38bf8059832169227705be06a28925af1b3799ffef5371d511fd138": {
"query": "\n INSERT INTO survey_users (created_at, id) VALUES($1, $2)",
"describe": {
@ -229,6 +230,22 @@
"nullable": []
}
},
"b4cd1e5240de1968c8b6d56672cec639b22f41ebf2754dadbf00efe0948c7e68": {
"query": "INSERT INTO survey_responses (\n user_id, \n campaign_id,\n device_user_provided,\n device_software_recognised,\n threads\n ) VALUES ($1, $2, $3, $4, $5);",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Uuid",
"Uuid",
"Varchar",
"Varchar",
"Int4"
]
},
"nullable": []
}
},
"c757589ef26a005e3285e7ab20d8a44c4f2e1cb125f8db061dd198cc380bf807": {
"query": "UPDATE survey_admins set name = $1\n WHERE name = $2",
"describe": {

View file

@ -18,6 +18,7 @@ use std::borrow::Cow;
use actix_identity::Identity;
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use sqlx::types::time::OffsetDateTime;
use super::get_uuid;
@ -25,14 +26,14 @@ use crate::errors::*;
use crate::AppData;
pub mod routes {
pub struct Challenges {
pub struct Campaign {
pub add: &'static str,
}
impl Challenges {
pub const fn new() -> Challenges {
let add = "/api/v1/admin/challenges/add";
Challenges { add }
impl Campaign {
pub const fn new() -> Campaign {
let add = "/api/v1/admin/campaign/add";
Campaign { add }
}
}
}
@ -42,7 +43,11 @@ pub mod runners {
use super::*;
pub async fn add_runner(data: &AppData) -> ServiceResult<uuid::Uuid> {
pub async fn add_runner(
username: &str,
payload: &AddCapmaign,
data: &AppData,
) -> ServiceResult<uuid::Uuid> {
let mut uuid;
let now = OffsetDateTime::now_utc();
@ -51,9 +56,17 @@ pub mod runners {
let res = sqlx::query!(
"
INSERT INTO survey_users (created_at, id) VALUES($1, $2)",
&now,
&uuid
INSERT INTO survey_campaigns (
user_id, ID, name, difficulties, created_at
) VALUES(
(SELECT id FROM survey_admins WHERE name = $1),
$2, $3, $4, $5
);",
username,
&uuid,
&payload.name,
&payload.difficulties,
&now
)
.execute(&data.db)
.await;
@ -62,7 +75,7 @@ pub mod runners {
break;
} else if let Err(sqlx::Error::Database(err)) = res {
if err.code() == Some(Cow::from("23505"))
&& err.message().contains("survey_users_id_key")
&& err.message().contains("survey_admins_id_key")
{
continue;
} else {
@ -74,12 +87,23 @@ pub mod runners {
}
}
#[derive(Serialize, Deserialize)]
pub struct AddCapmaign {
name: String,
difficulties: Vec<i32>,
}
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(add);
}
#[my_codegen::post(path = "crate::V1_API_ROUTES.auth.add")]
async fn add(data: AppData, id: Identity) -> ServiceResult<impl Responder> {
let uuid = runners::add_runner(&data).await?;
id.remember(uuid.to_string());
#[my_codegen::post(path = "crate::V1_API_ROUTES.admin.campaign.add")]
async fn add(
payload: web::Json<AddCapmaign>,
data: AppData,
id: Identity,
) -> ServiceResult<impl Responder> {
let username = id.identity().unwrap();
let payload = payload.into_inner();
let _campaign_id = runners::add_runner(&username, &payload, &data).await?;
Ok(HttpResponse::Ok())
}

View file

@ -18,6 +18,7 @@ use actix_web::web::ServiceConfig;
pub mod account;
pub mod auth;
pub mod campaigns;
#[cfg(test)]
mod tests;
@ -35,10 +36,12 @@ pub fn get_admin_check_login() -> crate::CheckLogin {
pub mod routes {
use super::account::routes::Account;
use super::auth::routes::Auth;
use super::campaigns::routes::Campaign;
pub struct Admin {
pub auth: Auth,
pub account: Account,
pub campaign: Campaign,
}
impl Admin {
@ -46,6 +49,7 @@ pub mod routes {
Admin {
account: Account::new(),
auth: Auth::new(),
campaign: Campaign::new(),
}
}
}