add survey table

This commit is contained in:
Aravinth Manivannan 2021-10-06 19:00:53 +05:30
parent a4251a772a
commit 3a7559d6a8
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
6 changed files with 94 additions and 3 deletions

View File

@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS survey_users (
ID UUID PRIMARY KEY NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL
device VARCHAR(100) NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
device VARCHAR(100) NOT NULL
)

View File

@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS survey_surveys (
ID UUID PRIMARY KEY NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
challenges serial[] NOT NULL
)

23
sqlx-data.json Normal file
View File

@ -0,0 +1,23 @@
{
"db": "PostgreSQL",
"f7c3faa9860de77ac29bb99afd4ed02112cf6c9432c094a3646fe64384f33866": {
"query": "SELECT challenges FROM survey_surveys WHERE id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "challenges",
"type_info": "Int4Array"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
false
]
}
}
}

62
src/api/v1/cache.rs Normal file
View File

@ -0,0 +1,62 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::collections::HashMap;
use std::sync::Arc;
use sqlx::PgPool;
use uuid::Uuid;
#[derive(Clone, Debug)]
pub struct ChallengeCache {
store: HashMap<Uuid, Arc<Vec<i32>>>,
db: PgPool,
}
impl ChallengeCache {
pub fn new(db: PgPool) -> Self {
let store = HashMap::default();
Self { db, store }
}
async fn get_db(&self, id: &Uuid) -> Vec<i32> {
struct Foo {
challenges: Vec<i32>,
}
let res = sqlx::query_as!(
Foo,
"SELECT challenges FROM survey_surveys WHERE id = $1",
&id
)
.fetch_one(&self.db)
.await
.unwrap();
res.challenges
}
pub async fn get(&mut self, k: &Uuid) -> Arc<Vec<i32>> {
match self.store.get(k) {
Some(val) => val.clone(),
None => {
let resp = self.get_db(k).await;
let resp = Arc::new(resp);
self.store.insert(k.clone(), resp.clone());
resp
}
}
}
}

View File

@ -18,6 +18,7 @@ use actix_web::web::ServiceConfig;
use uuid::Uuid;
pub mod auth;
pub mod cache;
mod meta;
pub mod routes;
pub use routes::ROUTES;

View File

@ -128,7 +128,7 @@ pub fn get_identity_service() -> IdentityService<CookieIdentityPolicy> {
let cookie_secret = &SETTINGS.server.cookie_secret;
IdentityService::new(
CookieIdentityPolicy::new(cookie_secret.as_bytes())
.name("Authorization")
.name("survey-id")
//TODO change cookie age
.max_age_secs(216000)
.domain(&SETTINGS.server.domain)