From 7d88ac6db8ffd580a1a89a84e69acd0229a236ce Mon Sep 17 00:00:00 2001 From: MCorange Date: Sat, 13 Sep 2025 15:52:49 +0300 Subject: [PATCH] Add more db shite --- .env => .cargo/config.toml | 3 +- Cargo.lock | 13 ++++ Cargo.toml | 3 +- migrations/20250906104612_users.up.sql | 3 +- migrations/20250906202709_sessions.up.sql | 3 +- migrations/20250913111557_ranks.down.sql | 1 + migrations/20250913111557_ranks.up.sql | 7 ++ migrations/20250913111613_awards.down.sql | 1 + migrations/20250913111613_awards.up.sql | 7 ++ migrations/20250913111614_trainings.down.sql | 1 + migrations/20250913111614_trainings.up.sql | 7 ++ migrations/20250913111615_missions.down.sql | 1 + migrations/20250913111615_missions.up.sql | 7 ++ .../20250913111617_qualifications.down.sql | 1 + .../20250913111617_qualifications.up.sql | 7 ++ .../20250913111736_records_ranks.down.sql | 1 + .../20250913111736_records_ranks.up.sql | 11 +++ .../20250913111748_records_awards.down.sql | 1 + .../20250913111748_records_awards.up.sql | 11 +++ .../20250913111802_records_training.down.sql | 1 + .../20250913111802_records_training.up.sql | 11 +++ ...0913111810_records_qualifications.down.sql | 1 + ...250913111810_records_qualifications.up.sql | 11 +++ .../20250913112041_records_missions.down.sql | 1 + .../20250913112041_records_missions.up.sql | 11 +++ src/api/files/create.rs | 29 ++++++++ src/api/files/delete.rs | 30 ++++++++ src/api/files/get_file.rs | 30 ++++++++ src/api/files/mkdir.rs | 30 ++++++++ src/api/files/mod.rs | 21 ++++++ src/api/files/rmdir.rs | 30 ++++++++ src/api/mod.rs | 4 +- src/db/tables/assignables/awards.rs | 60 +++++++++++++++ src/db/tables/assignables/missions.rs | 60 +++++++++++++++ src/db/tables/assignables/mod.rs | 5 ++ src/db/tables/assignables/qualifications.rs | 64 ++++++++++++++++ src/db/tables/assignables/ranks.rs | 60 +++++++++++++++ src/db/tables/assignables/trainings.rs | 60 +++++++++++++++ src/db/tables/mod.rs | 2 + src/db/tables/records/awards.rs | 73 +++++++++++++++++++ src/db/tables/records/missions.rs | 73 +++++++++++++++++++ src/db/tables/records/mod.rs | 5 ++ src/db/tables/records/qualifications.rs | 73 +++++++++++++++++++ src/db/tables/records/ranks.rs | 69 ++++++++++++++++++ src/db/tables/records/trainings.rs | 73 +++++++++++++++++++ 45 files changed, 971 insertions(+), 5 deletions(-) rename .env => .cargo/config.toml (87%) create mode 100644 migrations/20250913111557_ranks.down.sql create mode 100644 migrations/20250913111557_ranks.up.sql create mode 100644 migrations/20250913111613_awards.down.sql create mode 100644 migrations/20250913111613_awards.up.sql create mode 100644 migrations/20250913111614_trainings.down.sql create mode 100644 migrations/20250913111614_trainings.up.sql create mode 100644 migrations/20250913111615_missions.down.sql create mode 100644 migrations/20250913111615_missions.up.sql create mode 100644 migrations/20250913111617_qualifications.down.sql create mode 100644 migrations/20250913111617_qualifications.up.sql create mode 100644 migrations/20250913111736_records_ranks.down.sql create mode 100644 migrations/20250913111736_records_ranks.up.sql create mode 100644 migrations/20250913111748_records_awards.down.sql create mode 100644 migrations/20250913111748_records_awards.up.sql create mode 100644 migrations/20250913111802_records_training.down.sql create mode 100644 migrations/20250913111802_records_training.up.sql create mode 100644 migrations/20250913111810_records_qualifications.down.sql create mode 100644 migrations/20250913111810_records_qualifications.up.sql create mode 100644 migrations/20250913112041_records_missions.down.sql create mode 100644 migrations/20250913112041_records_missions.up.sql create mode 100644 src/api/files/create.rs create mode 100644 src/api/files/delete.rs create mode 100644 src/api/files/get_file.rs create mode 100644 src/api/files/mkdir.rs create mode 100644 src/api/files/mod.rs create mode 100644 src/api/files/rmdir.rs create mode 100644 src/db/tables/assignables/awards.rs create mode 100644 src/db/tables/assignables/missions.rs create mode 100644 src/db/tables/assignables/mod.rs create mode 100644 src/db/tables/assignables/qualifications.rs create mode 100644 src/db/tables/assignables/ranks.rs create mode 100644 src/db/tables/assignables/trainings.rs create mode 100644 src/db/tables/records/awards.rs create mode 100644 src/db/tables/records/missions.rs create mode 100644 src/db/tables/records/mod.rs create mode 100644 src/db/tables/records/qualifications.rs create mode 100644 src/db/tables/records/ranks.rs create mode 100644 src/db/tables/records/trainings.rs diff --git a/.env b/.cargo/config.toml similarity index 87% rename from .env rename to .cargo/config.toml index 2997c2d..8af9bc9 100644 --- a/.env +++ b/.cargo/config.toml @@ -1 +1,2 @@ -DATABASE_URL="postgres://postgres:postgres@127.0.0.1:5432/persmgr"" +[env] +DATABASE_URL="postgres://postgres:postgres@127.0.0.1:5432/persmgr" diff --git a/Cargo.lock b/Cargo.lock index b6e10d6..fb8a175 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -153,6 +153,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5" dependencies = [ "axum-core", + "axum-macros", "bytes", "form_urlencoded", "futures-util", @@ -200,6 +201,17 @@ dependencies = [ "tracing", ] +[[package]] +name = "axum-macros" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "604fde5e028fea851ce1d8570bbdc034bec850d157f7569d10f347d06808c05c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "backtrace" version = "0.3.75" @@ -1271,6 +1283,7 @@ dependencies = [ "askama", "axum", "base64", + "cfg-if", "pulldown-cmark", "rand 0.9.2", "serde", diff --git a/Cargo.toml b/Cargo.toml index 6e55d14..39fdc8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,9 @@ edition = "2024" anyhow = "1.0.99" argon2 = { version = "0.5.3", features = ["simple", "std"] } askama = "0.14.0" -axum = "0.8.4" +axum = { version = "0.8.4", features = ["macros"] } base64 = "0.22.1" +cfg-if = "1.0.3" pulldown-cmark = "0.13.0" rand = "0.9.2" serde = { version = "1.0.219", features = ["derive"] } diff --git a/migrations/20250906104612_users.up.sql b/migrations/20250906104612_users.up.sql index 6059d30..b23f28a 100644 --- a/migrations/20250906104612_users.up.sql +++ b/migrations/20250906104612_users.up.sql @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS users ( username TEXT NOT NULL UNIQUE, pw_hash TEXT NOT NULL, pw_salt TEXT NOT NULL, - pfp_id BIGINT NOT NULL + pfp_id BIGINT NOT NULL, + rank_id BIGINT NOT NULL ); diff --git a/migrations/20250906202709_sessions.up.sql b/migrations/20250906202709_sessions.up.sql index 3a5e9ac..8594fbb 100644 --- a/migrations/20250906202709_sessions.up.sql +++ b/migrations/20250906202709_sessions.up.sql @@ -3,5 +3,6 @@ CREATE TABLE IF NOT EXISTS sessions ( user_id BIGINT NOT NULL, session_key TEXT NOT NULL UNIQUE, - expires BIGINT NOT NULL + expires BIGINT NOT NULL, + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE ) diff --git a/migrations/20250913111557_ranks.down.sql b/migrations/20250913111557_ranks.down.sql new file mode 100644 index 0000000..f37b13f --- /dev/null +++ b/migrations/20250913111557_ranks.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS ranks; diff --git a/migrations/20250913111557_ranks.up.sql b/migrations/20250913111557_ranks.up.sql new file mode 100644 index 0000000..cd3f547 --- /dev/null +++ b/migrations/20250913111557_ranks.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS ranks ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); diff --git a/migrations/20250913111613_awards.down.sql b/migrations/20250913111613_awards.down.sql new file mode 100644 index 0000000..396e1ae --- /dev/null +++ b/migrations/20250913111613_awards.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS awards; diff --git a/migrations/20250913111613_awards.up.sql b/migrations/20250913111613_awards.up.sql new file mode 100644 index 0000000..9d037f7 --- /dev/null +++ b/migrations/20250913111613_awards.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS awards ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); diff --git a/migrations/20250913111614_trainings.down.sql b/migrations/20250913111614_trainings.down.sql new file mode 100644 index 0000000..45f557a --- /dev/null +++ b/migrations/20250913111614_trainings.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS trainings; diff --git a/migrations/20250913111614_trainings.up.sql b/migrations/20250913111614_trainings.up.sql new file mode 100644 index 0000000..5931fb8 --- /dev/null +++ b/migrations/20250913111614_trainings.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS trainings ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); diff --git a/migrations/20250913111615_missions.down.sql b/migrations/20250913111615_missions.down.sql new file mode 100644 index 0000000..751b303 --- /dev/null +++ b/migrations/20250913111615_missions.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS missions; diff --git a/migrations/20250913111615_missions.up.sql b/migrations/20250913111615_missions.up.sql new file mode 100644 index 0000000..5b199d5 --- /dev/null +++ b/migrations/20250913111615_missions.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS missions ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); diff --git a/migrations/20250913111617_qualifications.down.sql b/migrations/20250913111617_qualifications.down.sql new file mode 100644 index 0000000..0e7e962 --- /dev/null +++ b/migrations/20250913111617_qualifications.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS qualifications; diff --git a/migrations/20250913111617_qualifications.up.sql b/migrations/20250913111617_qualifications.up.sql new file mode 100644 index 0000000..a298b10 --- /dev/null +++ b/migrations/20250913111617_qualifications.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS qualifications ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + description TEXT NOT NULL, + created_at BIGINT NOT NULL, + modified_at BIGINT NOT NULL +); diff --git a/migrations/20250913111736_records_ranks.down.sql b/migrations/20250913111736_records_ranks.down.sql new file mode 100644 index 0000000..59761a9 --- /dev/null +++ b/migrations/20250913111736_records_ranks.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS record_ranks; diff --git a/migrations/20250913111736_records_ranks.up.sql b/migrations/20250913111736_records_ranks.up.sql new file mode 100644 index 0000000..8690371 --- /dev/null +++ b/migrations/20250913111736_records_ranks.up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS records_ranks ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + rank_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_rank FOREIGN KEY (rank_id) REFERENCES ranks(id) ON DELETE CASCADE +); diff --git a/migrations/20250913111748_records_awards.down.sql b/migrations/20250913111748_records_awards.down.sql new file mode 100644 index 0000000..343108e --- /dev/null +++ b/migrations/20250913111748_records_awards.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS record_awards; diff --git a/migrations/20250913111748_records_awards.up.sql b/migrations/20250913111748_records_awards.up.sql new file mode 100644 index 0000000..b2acceb --- /dev/null +++ b/migrations/20250913111748_records_awards.up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS records_awards ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + award_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_award FOREIGN KEY (award_id) REFERENCES awards(id) ON DELETE CASCADE +); diff --git a/migrations/20250913111802_records_training.down.sql b/migrations/20250913111802_records_training.down.sql new file mode 100644 index 0000000..63ca939 --- /dev/null +++ b/migrations/20250913111802_records_training.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS record_training; diff --git a/migrations/20250913111802_records_training.up.sql b/migrations/20250913111802_records_training.up.sql new file mode 100644 index 0000000..c2623e0 --- /dev/null +++ b/migrations/20250913111802_records_training.up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS records_trainings ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + training_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_training FOREIGN KEY (training_id) REFERENCES trainings(id) ON DELETE CASCADE +); diff --git a/migrations/20250913111810_records_qualifications.down.sql b/migrations/20250913111810_records_qualifications.down.sql new file mode 100644 index 0000000..3e2a7f4 --- /dev/null +++ b/migrations/20250913111810_records_qualifications.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS record_qualifications; diff --git a/migrations/20250913111810_records_qualifications.up.sql b/migrations/20250913111810_records_qualifications.up.sql new file mode 100644 index 0000000..7702c35 --- /dev/null +++ b/migrations/20250913111810_records_qualifications.up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS records_qualifications ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + qualification_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_qualification FOREIGN KEY (qualification_id) REFERENCES qualifications(id) ON DELETE CASCADE +); diff --git a/migrations/20250913112041_records_missions.down.sql b/migrations/20250913112041_records_missions.down.sql new file mode 100644 index 0000000..6955a54 --- /dev/null +++ b/migrations/20250913112041_records_missions.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS records_missions; diff --git a/migrations/20250913112041_records_missions.up.sql b/migrations/20250913112041_records_missions.up.sql new file mode 100644 index 0000000..35e9bb8 --- /dev/null +++ b/migrations/20250913112041_records_missions.up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS records_missions ( + id BIGSERIAL PRIMARY KEY NOT NULL, + user_id BIGINT NOT NULL, + mission_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + created_at BIGINT NOT NULL, + + CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES users(id) ON DELETE SET NULL, + CONSTRAINT fk_mission FOREIGN KEY (mission_id) REFERENCES missions(id) ON DELETE CASCADE +); diff --git a/src/api/files/create.rs b/src/api/files/create.rs new file mode 100644 index 0000000..a975deb --- /dev/null +++ b/src/api/files/create.rs @@ -0,0 +1,29 @@ +use axum::{ + body::{Body, Bytes}, + extract::{Path, State}, + http::{HeaderMap, HeaderValue, StatusCode}, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; + +use crate::db::Database; + +#[derive(Debug, Deserialize)] +pub struct Data { + path: String, +} + +#[axum::debug_handler] +pub async fn route(State(db): State) -> impl IntoResponse { + if false { + return Response::builder() + .status(StatusCode::NOT_FOUND) + .body(String::new()) + .unwrap(); + } + + Response::builder() + .status(StatusCode::OK) + .body(String::new()) + .unwrap() +} diff --git a/src/api/files/delete.rs b/src/api/files/delete.rs new file mode 100644 index 0000000..06a56ee --- /dev/null +++ b/src/api/files/delete.rs @@ -0,0 +1,30 @@ +use axum::{ + body::{Body, Bytes}, + extract::{Path, State}, + http::{HeaderMap, HeaderValue, StatusCode}, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; + +use crate::db::Database; + +#[derive(Debug, Deserialize)] +pub struct Data { + id: String, +} + +#[axum::debug_handler] +pub async fn route(State(db): State) -> impl IntoResponse { + if false { + return Response::builder() + .status(StatusCode::NOT_FOUND) + .body(String::new()) + .unwrap(); + } + + Response::builder() + .status(StatusCode::OK) + .body(String::new()) + .unwrap() +} + diff --git a/src/api/files/get_file.rs b/src/api/files/get_file.rs new file mode 100644 index 0000000..06a56ee --- /dev/null +++ b/src/api/files/get_file.rs @@ -0,0 +1,30 @@ +use axum::{ + body::{Body, Bytes}, + extract::{Path, State}, + http::{HeaderMap, HeaderValue, StatusCode}, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; + +use crate::db::Database; + +#[derive(Debug, Deserialize)] +pub struct Data { + id: String, +} + +#[axum::debug_handler] +pub async fn route(State(db): State) -> impl IntoResponse { + if false { + return Response::builder() + .status(StatusCode::NOT_FOUND) + .body(String::new()) + .unwrap(); + } + + Response::builder() + .status(StatusCode::OK) + .body(String::new()) + .unwrap() +} + diff --git a/src/api/files/mkdir.rs b/src/api/files/mkdir.rs new file mode 100644 index 0000000..06a56ee --- /dev/null +++ b/src/api/files/mkdir.rs @@ -0,0 +1,30 @@ +use axum::{ + body::{Body, Bytes}, + extract::{Path, State}, + http::{HeaderMap, HeaderValue, StatusCode}, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; + +use crate::db::Database; + +#[derive(Debug, Deserialize)] +pub struct Data { + id: String, +} + +#[axum::debug_handler] +pub async fn route(State(db): State) -> impl IntoResponse { + if false { + return Response::builder() + .status(StatusCode::NOT_FOUND) + .body(String::new()) + .unwrap(); + } + + Response::builder() + .status(StatusCode::OK) + .body(String::new()) + .unwrap() +} + diff --git a/src/api/files/mod.rs b/src/api/files/mod.rs new file mode 100644 index 0000000..582cd0a --- /dev/null +++ b/src/api/files/mod.rs @@ -0,0 +1,21 @@ +use axum::{ + Router, + routing::{get, post}, +}; + +use crate::db::Database; + +mod create; +mod delete; +mod get_file; +mod mkdir; +mod rmdir; + +pub fn register_routes() -> Router { + Router::new() + .route("/file/upload", post(create::route)) + .route("/file/get", get(get_file::route)) + .route("/file/delete", get(delete::route)) + .route("/dir/create", get(mkdir::route)) + .route("/dir/remove", get(rmdir::route)) +} diff --git a/src/api/files/rmdir.rs b/src/api/files/rmdir.rs new file mode 100644 index 0000000..06a56ee --- /dev/null +++ b/src/api/files/rmdir.rs @@ -0,0 +1,30 @@ +use axum::{ + body::{Body, Bytes}, + extract::{Path, State}, + http::{HeaderMap, HeaderValue, StatusCode}, + response::{IntoResponse, Response}, +}; +use serde::Deserialize; + +use crate::db::Database; + +#[derive(Debug, Deserialize)] +pub struct Data { + id: String, +} + +#[axum::debug_handler] +pub async fn route(State(db): State) -> impl IntoResponse { + if false { + return Response::builder() + .status(StatusCode::NOT_FOUND) + .body(String::new()) + .unwrap(); + } + + Response::builder() + .status(StatusCode::OK) + .body(String::new()) + .unwrap() +} + diff --git a/src/api/mod.rs b/src/api/mod.rs index f54dd0f..670e713 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,6 +1,7 @@ use crate::db::Database; use axum::{Router, extract::State, http::StatusCode, routing::get}; +pub mod files; pub mod user; async fn root(State(state): State) -> (StatusCode, &'static str) { @@ -10,7 +11,8 @@ async fn root(State(state): State) -> (StatusCode, &'static str) { pub fn register_routes() -> Router { let router = Router::new() .route("/", get(root)) - .nest("/user", user::register_routes()); + .nest("/user", user::register_routes()) + .nest("/files", files::register_routes()); Router::new().nest("/api", router) } diff --git a/src/db/tables/assignables/awards.rs b/src/db/tables/assignables/awards.rs new file mode 100644 index 0000000..90f614a --- /dev/null +++ b/src/db/tables/assignables/awards.rs @@ -0,0 +1,60 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct Award { + pub id: i64, + pub name: String, + pub description: String, + pub created_at: i64, + pub modified_at: i64, +} + +impl Award { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + Award, + r#" + INSERT INTO awards (name, description, created_at, modified_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.name, + self.description, + self.created_at, + self.modified_at + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!(Award, "SELECT * FROM awards WHERE id = $1", id) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { + let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); + let session = sqlx::query_as!( + Award, + r#" + UPDATE awards SET + name = $2, + description = $3, + modified_at = $4 + WHERE id = $1 + RETURNING * + "#, + self.id, + self.name, + self.description, + curr_time + ) + .fetch_one(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/assignables/missions.rs b/src/db/tables/assignables/missions.rs new file mode 100644 index 0000000..994ea55 --- /dev/null +++ b/src/db/tables/assignables/missions.rs @@ -0,0 +1,60 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct Mission { + pub id: i64, + pub name: String, + pub description: String, + pub created_at: i64, + pub modified_at: i64, +} + +impl Mission { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + Mission, + r#" + INSERT INTO missions (name, description, created_at, modified_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.name, + self.description, + self.created_at, + self.modified_at + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!(Mission, "SELECT * FROM missions WHERE id = $1", id) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { + let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); + let session = sqlx::query_as!( + Mission, + r#" + UPDATE missions SET + name = $2, + description = $3, + modified_at = $4 + WHERE id = $1 + RETURNING * + "#, + self.id, + self.name, + self.description, + curr_time + ) + .fetch_one(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/assignables/mod.rs b/src/db/tables/assignables/mod.rs new file mode 100644 index 0000000..1782dfb --- /dev/null +++ b/src/db/tables/assignables/mod.rs @@ -0,0 +1,5 @@ +pub mod awards; +pub mod missions; +pub mod qualifications; +pub mod ranks; +pub mod trainings; diff --git a/src/db/tables/assignables/qualifications.rs b/src/db/tables/assignables/qualifications.rs new file mode 100644 index 0000000..a141007 --- /dev/null +++ b/src/db/tables/assignables/qualifications.rs @@ -0,0 +1,64 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct Qualification { + pub id: i64, + pub name: String, + pub description: String, + pub created_at: i64, + pub modified_at: i64, +} + +impl Qualification { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + Qualification, + r#" + INSERT INTO qualifications (name, description, created_at, modified_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.name, + self.description, + self.created_at, + self.modified_at + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!( + Qualification, + "SELECT * FROM qualifications WHERE id = $1", + id + ) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { + let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); + let session = sqlx::query_as!( + Qualification, + r#" + UPDATE qualifications SET + name = $2, + description = $3, + modified_at = $4 + WHERE id = $1 + RETURNING * + "#, + self.id, + self.name, + self.description, + curr_time + ) + .fetch_one(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/assignables/ranks.rs b/src/db/tables/assignables/ranks.rs new file mode 100644 index 0000000..8d07d16 --- /dev/null +++ b/src/db/tables/assignables/ranks.rs @@ -0,0 +1,60 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct Rank { + pub id: i64, + pub name: String, + pub description: String, + pub created_at: i64, + pub modified_at: i64, +} + +impl Rank { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + Rank, + r#" + INSERT INTO ranks (name, description, created_at, modified_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.name, + self.description, + self.created_at, + self.modified_at + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!(Rank, "SELECT * FROM ranks WHERE id = $1", id) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { + let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); + let session = sqlx::query_as!( + Rank, + r#" + UPDATE ranks SET + name = $2, + description = $3, + modified_at = $4 + WHERE id = $1 + RETURNING * + "#, + self.id, + self.name, + self.description, + curr_time + ) + .fetch_one(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/assignables/trainings.rs b/src/db/tables/assignables/trainings.rs new file mode 100644 index 0000000..7cdb889 --- /dev/null +++ b/src/db/tables/assignables/trainings.rs @@ -0,0 +1,60 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct Training { + pub id: i64, + pub name: String, + pub description: String, + pub created_at: i64, + pub modified_at: i64, +} + +impl Training { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + Training, + r#" + INSERT INTO trainings (name, description, created_at, modified_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.name, + self.description, + self.created_at, + self.modified_at + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!(Training, "SELECT * FROM trainings WHERE id = $1", id) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { + let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); + let session = sqlx::query_as!( + Training, + r#" + UPDATE trainings SET + name = $2, + description = $3, + modified_at = $4 + WHERE id = $1 + RETURNING * + "#, + self.id, + self.name, + self.description, + curr_time + ) + .fetch_one(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/mod.rs b/src/db/tables/mod.rs index e2f7bd7..f16be4c 100644 --- a/src/db/tables/mod.rs +++ b/src/db/tables/mod.rs @@ -1,2 +1,4 @@ +pub mod assignables; +pub mod records; pub mod sessions; pub mod user; diff --git a/src/db/tables/records/awards.rs b/src/db/tables/records/awards.rs new file mode 100644 index 0000000..b7b22ac --- /dev/null +++ b/src/db/tables/records/awards.rs @@ -0,0 +1,73 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct AwardRecord { + pub id: i64, + pub user_id: i64, + pub award_id: i64, + pub author_id: i64, + pub created_at: i64, +} + +impl AwardRecord { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + AwardRecord, + r#" + INSERT INTO records_awards (user_id, award_id, author_id, created_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.user_id, + self.award_id, + self.author_id, + self.created_at, + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!( + AwardRecord, + "SELECT * FROM records_awards WHERE id = $1", + id + ) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn get_by_user_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + AwardRecord, + "SELECT * FROM records_awards WHERE user_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_author_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + AwardRecord, + "SELECT * FROM records_awards WHERE author_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_award_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + AwardRecord, + "SELECT * FROM records_awards WHERE award_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/records/missions.rs b/src/db/tables/records/missions.rs new file mode 100644 index 0000000..5b0ecda --- /dev/null +++ b/src/db/tables/records/missions.rs @@ -0,0 +1,73 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct MissionRecord { + pub id: i64, + pub user_id: i64, + pub mission_id: i64, + pub author_id: i64, + pub created_at: i64, +} + +impl MissionRecord { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + MissionRecord, + r#" + INSERT INTO records_missions (user_id, mission_id, author_id, created_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.user_id, + self.mission_id, + self.author_id, + self.created_at, + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!( + MissionRecord, + "SELECT * FROM records_missions WHERE id = $1", + id + ) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn get_by_user_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + MissionRecord, + "SELECT * FROM records_missions WHERE user_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_author_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + MissionRecord, + "SELECT * FROM records_missions WHERE author_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_mission_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + MissionRecord, + "SELECT * FROM records_missions WHERE mission_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/records/mod.rs b/src/db/tables/records/mod.rs new file mode 100644 index 0000000..1782dfb --- /dev/null +++ b/src/db/tables/records/mod.rs @@ -0,0 +1,5 @@ +pub mod awards; +pub mod missions; +pub mod qualifications; +pub mod ranks; +pub mod trainings; diff --git a/src/db/tables/records/qualifications.rs b/src/db/tables/records/qualifications.rs new file mode 100644 index 0000000..6a0b83a --- /dev/null +++ b/src/db/tables/records/qualifications.rs @@ -0,0 +1,73 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct QualificationRecord { + pub id: i64, + pub user_id: i64, + pub qualification_id: i64, + pub author_id: i64, + pub created_at: i64, +} + +impl QualificationRecord { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + QualificationRecord, + r#" + INSERT INTO records_qualifications (user_id, qualification_id, author_id, created_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.user_id, + self.qualification_id, + self.author_id, + self.created_at, + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!( + QualificationRecord, + "SELECT * FROM records_qualifications WHERE id = $1", + id + ) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn get_by_user_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + QualificationRecord, + "SELECT * FROM records_qualifications WHERE user_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_author_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + QualificationRecord, + "SELECT * FROM records_qualifications WHERE author_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_qualification_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + QualificationRecord, + "SELECT * FROM records_qualifications WHERE qualification_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/records/ranks.rs b/src/db/tables/records/ranks.rs new file mode 100644 index 0000000..28ae48b --- /dev/null +++ b/src/db/tables/records/ranks.rs @@ -0,0 +1,69 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct RankRecord { + pub id: i64, + pub user_id: i64, + pub rank_id: i64, + pub author_id: i64, + pub created_at: i64, +} + +impl RankRecord { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + RankRecord, + r#" + INSERT INTO records_ranks (user_id, rank_id, author_id, created_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.user_id, + self.rank_id, + self.author_id, + self.created_at, + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!(RankRecord, "SELECT * FROM records_ranks WHERE id = $1", id) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn get_by_user_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + RankRecord, + "SELECT * FROM records_ranks WHERE user_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_author_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + RankRecord, + "SELECT * FROM records_ranks WHERE author_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_rank_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + RankRecord, + "SELECT * FROM records_ranks WHERE rank_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/records/trainings.rs b/src/db/tables/records/trainings.rs new file mode 100644 index 0000000..62eabbd --- /dev/null +++ b/src/db/tables/records/trainings.rs @@ -0,0 +1,73 @@ +use anyhow::Result; + +use crate::db::CurrPool; + +#[derive(Debug, Default, Clone)] +pub struct TrainingRecord { + pub id: i64, + pub user_id: i64, + pub training_id: i64, + pub author_id: i64, + pub created_at: i64, +} + +impl TrainingRecord { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + TrainingRecord, + r#" + INSERT INTO records_trainings (user_id, training_id, author_id, created_at) + VALUES ($1, $2, $3, $4) + RETURNING * + "#, + self.user_id, + self.training_id, + self.author_id, + self.created_at, + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!( + TrainingRecord, + "SELECT * FROM records_trainings WHERE id = $1", + id + ) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn get_by_user_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + TrainingRecord, + "SELECT * FROM records_trainings WHERE user_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_author_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + TrainingRecord, + "SELECT * FROM records_trainings WHERE author_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } + pub async fn get_by_training_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + TrainingRecord, + "SELECT * FROM records_trainings WHERE training_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } +}