Skip to content

Commit

Permalink
Merge pull request #147 from sogrim/optimize-course-retrieval
Browse files Browse the repository at this point in the history
Optimize course retrieval
  • Loading branch information
liadaram1 authored Sep 15, 2022
2 parents f227f47 + 0fa6a3a commit 1c61e2c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
7 changes: 4 additions & 3 deletions packages/server/src/api/students.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ pub async fn get_courses_by_filter(
.map_err(|e| AppError::BadRequest(e.to_string()))?;
match (params.get("name"), params.get("number")) {
(Some(name), None) => {
let courses = db::services::get_all_courses_by_name(name, &client).await?;
let courses = db::services::get_courses_filtered_by_name(name, &client).await?;
Ok(HttpResponse::Ok().json(courses))
}
(None, Some(number)) => {
let courses = db::services::get_all_courses_by_number(number, &client).await?;
let courses = db::services::get_courses_filtered_by_number(number, &client).await?;
Ok(HttpResponse::Ok().json(courses))
}
(Some(_), Some(_)) => Err(AppError::BadRequest("Invalid query params".into())),
Expand Down Expand Up @@ -137,7 +137,8 @@ pub async fn compute_degree_status(

user_details.modified = false;

let vec_courses = db::services::get_all_courses(&client).await?;
let vec_courses =
db::services::get_courses_by_ids(catalog.get_all_course_ids(), &client).await?;
let malag_courses = db::services::get_all_malags(&client).await?[0]
.malag_list
.clone(); // The collection malags contain one item with the list of all malags
Expand Down
39 changes: 25 additions & 14 deletions packages/server/src/db/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::resources::catalog::{Catalog, DisplayCatalog};
use crate::resources::course::{Course, Malags};
use crate::resources::user::User;
use bson::oid::ObjectId;
pub use bson::{doc, Document};
pub use bson::{doc, Bson, Document};
use futures_util::TryStreamExt;
use mongodb::options::{FindOneAndUpdateOptions, ReturnDocument, UpdateModifications};
use mongodb::Client;
Expand All @@ -17,7 +17,6 @@ macro_rules! impl_get {
db_item=$db_item:ty,
db_key_type=$db_key_type:ty
) => {
#[allow(dead_code)] // TODO: remove this
pub async fn $fn_name(id: $db_key_type, client: &Client) -> Result<$db_item, AppError> {
match client
.database(CONFIG.profile)
Expand Down Expand Up @@ -62,18 +61,22 @@ macro_rules! impl_get_all {
}

#[macro_export]
macro_rules! impl_get_all_filtered {
macro_rules! impl_get_filtered {
(
fn_name=$fn_name:ident,
db_item=$db_item:ty,
db_coll_name=$db_coll_name:literal,
filter_name=$filter_name:literal
filter_by=$filter_by:literal,
filter_type=$filter_type:literal
) => {
pub async fn $fn_name(filter: &str, client: &Client) -> Result<Vec<$db_item>, AppError> {
pub async fn $fn_name(
filter: impl Into<Bson>,
client: &Client,
) -> Result<Vec<$db_item>, AppError> {
match client
.database(CONFIG.profile)
.collection::<$db_item>($db_coll_name)
.find(doc! {$filter_name: { "$regex": filter}}, None)
.find(doc! {$filter_by: { $filter_type: filter.into()}}, None)
.await
{
Ok(docs) => Ok(docs
Expand All @@ -94,7 +97,6 @@ macro_rules! impl_update {
db_key_type=$db_key_type:ty,
db_coll_name=$db_coll_name:literal
) => {
#[allow(dead_code)] // TODO: remove this
pub async fn $fn_name(
id: $db_key_type,
document: Document,
Expand Down Expand Up @@ -131,7 +133,6 @@ macro_rules! impl_delete {
db_key_type=$db_key_type:ty,
db_coll_name=$db_coll_name:literal
) => {
#[allow(dead_code)] // TODO: remove this
pub async fn $fn_name(id: $db_key_type, client: &Client) -> Result<(), AppError> {
match client
.database(CONFIG.profile)
Expand Down Expand Up @@ -181,18 +182,28 @@ impl_get_all!(
db_coll_name = "Courses"
);

impl_get_all_filtered!(
fn_name = get_all_courses_by_name,
impl_get_filtered!(
fn_name = get_courses_by_ids,
db_item = Course,
db_coll_name = "Courses",
filter_by = "_id",
filter_type = "$in"
);

impl_get_filtered!(
fn_name = get_courses_filtered_by_name,
db_item = Course,
db_coll_name = "Courses",
filter_name = "name"
filter_by = "name",
filter_type = "$regex"
);

impl_get_all_filtered!(
fn_name = get_all_courses_by_number,
impl_get_filtered!(
fn_name = get_courses_filtered_by_number,
db_item = Course,
db_coll_name = "Courses",
filter_name = "_id"
filter_by = "_id",
filter_type = "$regex"
);

impl_update!(
Expand Down
35 changes: 34 additions & 1 deletion packages/server/src/db/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::config::CONFIG;
use crate::{config::CONFIG, db, init_mongodb_client};
use actix_rt::test;
use actix_web::{body::MessageBody, http::StatusCode, web::Bytes, ResponseError};

Expand Down Expand Up @@ -52,3 +52,36 @@ pub async fn test_db_internal_error() {
);
}
}

#[test]
pub async fn test_get_courses_by_filters() {
dotenv().ok();

let client = init_mongodb_client!();

let courses = db::services::get_courses_filtered_by_name("חשבון אינפיניטסימלי 1מ'", &client)
.await
.expect("Failed to get courses by name");

assert_eq!(courses.len(), 1);
assert_eq!(courses[0].name, "חשבון אינפיניטסימלי 1מ'");
assert_eq!(courses[0].id, "104031");

let courses = db::services::get_courses_filtered_by_number("104031", &client)
.await
.expect("Failed to get courses by number");

assert_eq!(courses.len(), 1);
assert_eq!(courses[0].name, "חשבון אינפיניטסימלי 1מ'");
assert_eq!(courses[0].id, "104031");

let courses = db::services::get_courses_by_ids(vec!["104031", "104166"], &client)
.await
.expect("Failed to get courses by number");

assert_eq!(courses.len(), 2);
assert_eq!(courses[0].name, "חשבון אינפיניטסימלי 1מ'");
assert_eq!(courses[0].id, "104031");
assert_eq!(courses[1].name, "אלגברה אמ'");
assert_eq!(courses[1].id, "104166");
}
4 changes: 4 additions & 0 deletions packages/server/src/resources/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ impl Catalog {
}
names
}

pub fn get_all_course_ids(&self) -> Vec<CourseId> {
self.course_to_bank.clone().into_keys().collect()
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down

0 comments on commit 1c61e2c

Please sign in to comment.