-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9dc0c1
commit 2862846
Showing
7 changed files
with
4,190 additions
and
5,686 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
Oops, something went wrong.