iqps_backend/db/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
//! Database stuff. See submodules also.
use color_eyre::eyre::eyre;
use models::DBAdminDashboardQP;
use sqlx::{postgres::PgPoolOptions, prelude::FromRow, PgPool, Postgres, Transaction};
use std::time::Duration;
use crate::{
env::EnvVars,
pathutils::{PaperCategory, Paths},
qp::{self, AdminDashboardQP, Exam, LibraryQP, Semester},
routing::{EditReq, FileDetails},
};
mod models;
mod queries;
#[derive(Clone)]
/// The database
pub struct Database {
connection: PgPool,
}
#[derive(FromRow)]
/// Needed this to use the `query_as()` function of sqlx. There is probably a better way to do this but this is my first time, sorry.
struct Breh {
id: i32,
}
impl Database {
/// Creates a new database connection given the environment variables.
pub async fn new(env_vars: &EnvVars) -> Result<Self, sqlx::Error> {
let database_url = format!(
"postgres://{}:{}@{}:{}/{}",
env_vars.db_user,
env_vars.db_password,
env_vars.db_host,
env_vars.db_port,
env_vars.db_name
);
let conn_pool = PgPoolOptions::new()
.max_connections(5)
.acquire_timeout(Duration::from_secs(3))
.connect(&database_url)
.await?;
Ok(Self {
connection: conn_pool,
})
}
/// Fetches the list of all unapproved papers
pub async fn get_unapproved_papers(&self) -> Result<Vec<qp::AdminDashboardQP>, sqlx::Error> {
let query_sql = queries::get_all_unapproved_query();
let papers: Vec<models::DBAdminDashboardQP> = sqlx::query_as(&query_sql)
.fetch_all(&self.connection)
.await?;
Ok(papers
.iter()
.map(|qp| qp::AdminDashboardQP::from(qp.clone()))
.collect())
}
/// Searches for papers from a given query. Uses some voodoo black magic by @rajivharlalka
pub async fn search_papers(
&self,
query: &str,
exam_filter: Vec<Exam>,
) -> Result<Vec<qp::BaseQP>, sqlx::Error> {
let query_sql = queries::get_qp_search_query(exam_filter);
let query = sqlx::query_as(&query_sql).bind(query);
let papers: Vec<models::DBBaseQP> = query.fetch_all(&self.connection).await?;
Ok(papers
.iter()
.map(|qp| qp::BaseQP::from(qp.clone()))
.collect())
}
pub async fn get_paper_by_id(&self, id: i32) -> Result<qp::AdminDashboardQP, sqlx::Error> {
let query_sql = queries::get_get_paper_by_id_query();
let query = sqlx::query_as(&query_sql).bind(id);
let paper: models::DBAdminDashboardQP = query.fetch_one(&self.connection).await?;
Ok(paper.into())
}
/// Edit's a paper's details.
///
/// - Sets the `approved_by` field to the username if approved.
/// - Sets the `filelink` to:
/// - For library papers, remains unchanged
/// - For uploaded papers, approved papers are moved to the approved directory and renamed `id_coursecode_coursename_year_semester_exam.pdf` and unapproved papers are moved to the unapproved directory and named `id.pdf`
///
/// Returns the database transaction, the old filelink and the new paper details ([`crate::qp::AdminDashboardQP`])
pub async fn edit_paper<'c>(
&self,
edit_req: EditReq,
username: &str,
env_vars: &EnvVars,
) -> Result<(Transaction<'c, Postgres>, String, AdminDashboardQP), color_eyre::eyre::Error>
{
let EditReq {
id,
course_code,
course_name,
year,
semester,
exam,
approve_status,
note,
} = edit_req;
let current_details = self.get_paper_by_id(id).await?;
// Construct the final values to be inserted into the db
let course_code = course_code.unwrap_or(current_details.qp.course_code);
let course_name = course_name.unwrap_or(current_details.qp.course_name);
let year = year.unwrap_or(current_details.qp.year);
let semester: String = semester
.map(|sem| Semester::try_from(&sem))
.transpose()?
.unwrap_or(current_details.qp.semester)
.into();
let exam: String = exam
.map(|exam| Exam::try_from(&exam))
.transpose()?
.unwrap_or(current_details.qp.exam)
.into();
let approve_status = approve_status.unwrap_or(current_details.approve_status);
// Set the new filelink
let old_filelink = current_details.qp.filelink;
let new_filelink = if current_details.qp.from_library {
old_filelink.clone() // TODO use consistent format
} else if approve_status {
env_vars.paths.get_slug(
&format!(
"{}.pdf",
Paths::sanitize_path(&format!(
"{}_{}_{}_{}_{}_{}",
id, course_code, course_name, year, semester, exam
))
),
PaperCategory::Approved,
)
} else {
env_vars
.paths
.get_slug(&format!("{}.pdf", id), PaperCategory::Unapproved)
};
let mut tx = self.connection.begin().await?;
let query_sql = queries::get_edit_paper_query(approve_status);
let query = sqlx::query_as(&query_sql)
.bind(id)
.bind(&course_code)
.bind(&course_name)
.bind(year)
.bind(&semester)
.bind(&exam)
.bind(¬e)
.bind(approve_status)
.bind(&new_filelink);
let query = if approve_status {
query.bind(username)
} else {
query
};
let new_qp: DBAdminDashboardQP = query.fetch_one(&mut *tx).await?;
let new_qp = AdminDashboardQP::from(new_qp);
Ok((tx, old_filelink, new_qp))
}
// /// Adds a new upload paper's details to the database. Sets the `from_library` field to false.
// ///
// /// Returns the database transaction and the id of the uploaded paper
// pub async fn add_uploaded_paper<'c>(
// &self,
// file_details:
// ) -> Result<(Transaction<'c, Postgres>, i32), color_eyre::eyre::Error> {
// }
/// Sets the `is_deleted` field to true and `approve_status` to false. Only deletes uploaded papers.
///
/// Returns a boolean that represents whether a db entry was affected or not. If more than one entry was affected, an error will be thrown and the transaction will be rolled back.
pub async fn soft_delete(&self, id: i32) -> Result<bool, color_eyre::eyre::Error> {
let mut tx = self.connection.begin().await?;
let rows_affected = sqlx::query(queries::SOFT_DELETE_BY_ID)
.bind(id)
.execute(&mut *tx)
.await?
.rows_affected();
if rows_affected > 1 {
tx.rollback().await?;
Err(eyre!(
"Error: {} (> 1) papers were deleted. Rolling back.",
rows_affected
))
} else {
tx.commit().await?;
Ok(rows_affected == 1)
}
}
/// Returns all papers that match one or more of the specified properties exactly. `course_name` is required, other properties are optional.
pub async fn get_similar_papers(
&self,
course_code: &str,
year: Option<i32>,
semester: Option<&String>,
exam: Option<&String>,
) -> Result<Vec<AdminDashboardQP>, sqlx::Error> {
let query_sql =
queries::get_similar_papers_query(year.is_some(), semester.is_some(), exam.is_some());
let query = sqlx::query_as(&query_sql).bind(course_code);
let query = query.bind(year);
let query = query.bind(semester);
let query = query.bind(exam);
let papers: Vec<models::DBAdminDashboardQP> = query.fetch_all(&self.connection).await?;
Ok(papers
.iter()
.map(|qp| qp::AdminDashboardQP::from(qp.clone()))
.collect())
}
/// Inserts a new uploaded question paper into the database. Uses a placeholder for the filelink which should be replaced once the id is known using the [crate::db::Database::update_filelink] function.
///
/// Returns a tuple with the transaction and the id of the inserted paper.
pub async fn insert_new_uploaded_qp<'c>(
&self,
file_details: &FileDetails,
) -> Result<(Transaction<'c, Postgres>, i32), color_eyre::eyre::Error> {
let mut tx = self.connection.begin().await?;
let FileDetails {
course_code,
course_name,
year,
exam,
semester,
note,
..
} = file_details;
let query = sqlx::query_as(queries::INSERT_NEW_QP)
.bind(course_code)
.bind(course_name)
.bind(year)
.bind(exam)
.bind(semester)
.bind(note)
.bind("placeholder_filelink")
.bind(false);
let Breh { id } = query.fetch_one(&mut *tx).await?;
Ok((tx, id))
}
#[allow(unused)]
/// Inserts a new library question paper into the database. Uses a placeholder for the filelink which should be replaced once the id is known using the [crate::db::Database::update_filelink] function.
///
/// Returns a tuple with the transaction and the id of the inserted paper.
pub async fn insert_new_library_qp<'c>(
&self,
paper: &LibraryQP,
) -> Result<(Transaction<'c, Postgres>, i32), color_eyre::eyre::Error> {
let mut tx = self.connection.begin().await?;
let LibraryQP {
course_code,
course_name,
year,
exam,
semester,
approve_status,
..
} = paper;
let query = sqlx::query_as(queries::INSERT_NEW_LIBRARY_QP)
.bind(course_code)
.bind(course_name)
.bind(year)
.bind(exam)
.bind(semester)
.bind("")
.bind("placeholder_filelink")
.bind(approve_status);
let Breh { id } = query.fetch_one(&mut *tx).await?;
Ok((tx, id))
}
/// Updates filelink for an uploaded question paper uploaded using the [crate::db::Database::insert_new_uploaded_qp] or [crate::db::Database::insert_new_library_qp] function. Takes the same transaction that the previous function used.
pub async fn update_filelink(
&self,
tx: &mut Transaction<'_, Postgres>,
id: i32,
file_link: &str,
) -> Result<(), color_eyre::eyre::Error> {
let query = sqlx::query(queries::UPDATE_FILELINK)
.bind(id)
.bind(file_link);
query.execute(&mut **tx).await?;
Ok(())
}
}