Add more db shite
This commit is contained in:
29
src/api/files/create.rs
Normal file
29
src/api/files/create.rs
Normal file
@@ -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<Database>) -> impl IntoResponse {
|
||||
if false {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(String::new())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(String::new())
|
||||
.unwrap()
|
||||
}
|
||||
30
src/api/files/delete.rs
Normal file
30
src/api/files/delete.rs
Normal file
@@ -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<Database>) -> impl IntoResponse {
|
||||
if false {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(String::new())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(String::new())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
30
src/api/files/get_file.rs
Normal file
30
src/api/files/get_file.rs
Normal file
@@ -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<Database>) -> impl IntoResponse {
|
||||
if false {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(String::new())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(String::new())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
30
src/api/files/mkdir.rs
Normal file
30
src/api/files/mkdir.rs
Normal file
@@ -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<Database>) -> impl IntoResponse {
|
||||
if false {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(String::new())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(String::new())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
21
src/api/files/mod.rs
Normal file
21
src/api/files/mod.rs
Normal file
@@ -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<Database> {
|
||||
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))
|
||||
}
|
||||
30
src/api/files/rmdir.rs
Normal file
30
src/api/files/rmdir.rs
Normal file
@@ -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<Database>) -> impl IntoResponse {
|
||||
if false {
|
||||
return Response::builder()
|
||||
.status(StatusCode::NOT_FOUND)
|
||||
.body(String::new())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.body(String::new())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
@@ -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<Database>) -> (StatusCode, &'static str) {
|
||||
@@ -10,7 +11,8 @@ async fn root(State(state): State<Database>) -> (StatusCode, &'static str) {
|
||||
pub fn register_routes() -> Router<Database> {
|
||||
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)
|
||||
}
|
||||
|
||||
60
src/db/tables/assignables/awards.rs
Normal file
60
src/db/tables/assignables/awards.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
60
src/db/tables/assignables/missions.rs
Normal file
60
src/db/tables/assignables/missions.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
5
src/db/tables/assignables/mod.rs
Normal file
5
src/db/tables/assignables/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod awards;
|
||||
pub mod missions;
|
||||
pub mod qualifications;
|
||||
pub mod ranks;
|
||||
pub mod trainings;
|
||||
64
src/db/tables/assignables/qualifications.rs
Normal file
64
src/db/tables/assignables/qualifications.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
60
src/db/tables/assignables/ranks.rs
Normal file
60
src/db/tables/assignables/ranks.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
60
src/db/tables/assignables/trainings.rs
Normal file
60
src/db/tables/assignables/trainings.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
pub mod assignables;
|
||||
pub mod records;
|
||||
pub mod sessions;
|
||||
pub mod user;
|
||||
|
||||
73
src/db/tables/records/awards.rs
Normal file
73
src/db/tables/records/awards.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
let session = sqlx::query_as!(
|
||||
AwardRecord,
|
||||
"SELECT * FROM records_awards WHERE award_id = $1",
|
||||
id
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
73
src/db/tables/records/missions.rs
Normal file
73
src/db/tables/records/missions.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
let session = sqlx::query_as!(
|
||||
MissionRecord,
|
||||
"SELECT * FROM records_missions WHERE mission_id = $1",
|
||||
id
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
5
src/db/tables/records/mod.rs
Normal file
5
src/db/tables/records/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub mod awards;
|
||||
pub mod missions;
|
||||
pub mod qualifications;
|
||||
pub mod ranks;
|
||||
pub mod trainings;
|
||||
73
src/db/tables/records/qualifications.rs
Normal file
73
src/db/tables/records/qualifications.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
let session = sqlx::query_as!(
|
||||
QualificationRecord,
|
||||
"SELECT * FROM records_qualifications WHERE qualification_id = $1",
|
||||
id
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
69
src/db/tables/records/ranks.rs
Normal file
69
src/db/tables/records/ranks.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
let session = sqlx::query_as!(
|
||||
RankRecord,
|
||||
"SELECT * FROM records_ranks WHERE rank_id = $1",
|
||||
id
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
73
src/db/tables/records/trainings.rs
Normal file
73
src/db/tables/records/trainings.rs
Normal file
@@ -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<Self> {
|
||||
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<Self> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
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<Vec<Self>> {
|
||||
let session = sqlx::query_as!(
|
||||
TrainingRecord,
|
||||
"SELECT * FROM records_trainings WHERE training_id = $1",
|
||||
id
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user