Skip to content

Commit

Permalink
implement frontend term selecting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Cordry committed May 31, 2024
1 parent d9dc0c1 commit 2862846
Show file tree
Hide file tree
Showing 7 changed files with 4,190 additions and 5,686 deletions.
2 changes: 1 addition & 1 deletion backend/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn serve() {
)
.app_data(app_data.clone())
})
.bind(("0.0.0.0", 8080))
.bind(("0.0.0.0", 3000))
.unwrap()
.run()
.await
Expand Down
46 changes: 42 additions & 4 deletions backend/src/api/terms.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,65 @@
use crate::{api::AppState, model::Term};
use crate::{
api::AppState,
model::{Term, Year},
};
use actix_web::{get, web::Data, HttpResponse, Responder};
use log::{log, Level};
use sqlx::query_as;

#[utoipa::path(
context_path = "/api",
responses(
(status = 200, description = "List all terms available", body = [Vec<i32>]),
(status = 200, description = "List all terms available, grouped by year", body = [Vec<Year>]),
(status = 500, description = "Error Created by Query"),
)
)]
#[get("/terms")]
pub async fn get_terms(state: Data<AppState>) -> impl Responder {
log!(Level::Info, "GET /terms");

struct TempTerm {
term: i32,
}

match query_as!(
Term,
TempTerm,
"SELECT DISTINCT term FROM academicterms ORDER BY term DESC",
)
.fetch_all(&state.db)
.await
{
Ok(terms) => HttpResponse::Ok().json(terms),
Ok(mut terms) => {
let mut years: Vec<Year> = vec![];

while !terms.is_empty() {
let base: i32 = terms[0].term % 1000 / 10;
let year_terms: Vec<Term> = terms
.iter()
.take_while(|&t| t.term % 1000 / 10 == base)
.map(|t| Term {
term_id: t.term,
term_name: match t.term % 10 {
1 => format!("Fall 20{}", base),
5 => format!("Spring 20{}", base + 1),
8 => format!("Summer 20{}", base + 1),
_ => String::new(),
},
})
.collect();

terms = terms
.into_iter()
.filter(|t| t.term % 1000 / 10 != base)
.collect::<Vec<TempTerm>>();

years.push(Year {
year: format!("20{} - 20{}", base, base + 1),
terms: year_terms,
});
}

HttpResponse::Ok().json(years)
}
Err(e) => HttpResponse::InternalServerError().json(e.to_string()),
}
}
10 changes: 9 additions & 1 deletion backend/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,15 @@ pub struct Department {
#[derive(Serialize, Debug, Clone, ToSchema, FromRow)]
#[serde(rename_all = "camelCase")]
pub struct Term {
pub term: i32,
pub term_id: i32,
pub term_name: String,
}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Year {
pub year: String,
pub terms: Vec<Term>,
}

/// Represents a course that fulfills the criteria set by the search
Expand Down
2 changes: 1 addition & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20.10.0 as build
FROM node:20.14.0 as build

WORKDIR /app

Expand Down
Loading

0 comments on commit 2862846

Please sign in to comment.