Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set the default season to the current season #90

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions vingo/src/routes/util/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{env, sync::LazyLock};
use axum::extract::State;
use chrono::Local;
use reqwest::StatusCode;
use sea_orm::EntityTrait;
use sea_orm::{sea_query::Expr, DatabaseConnection, EntityTrait, QueryFilter};
use tower_sessions::Session;

use super::errors::{ResponseResult, ResultAndLogError};
Expand Down Expand Up @@ -58,12 +58,31 @@ pub async fn get_season(

let season_or_default = match season {
Some(season_model) => season_model,
None => Season::find_by_id(1)
.one(&state.db)
.await
.or_log((StatusCode::INTERNAL_SERVER_ERROR, "failed to get season"))?
.ok_or((StatusCode::INTERNAL_SERVER_ERROR, "no season 1"))?,
None => get_current_season_or_all(&state.db).await?,
};

Ok(season_or_default)
}

async fn get_current_season_or_all(db: &DatabaseConnection) -> ResponseResult<season::Model> {
let curr_season = Season::find()
.filter(
Expr::col(season::Column::Id)
.ne(1)
.and(Expr::col(season::Column::End).gte(Expr::current_date()))
.and(Expr::col(season::Column::Start).lte(Expr::current_date())),
)
.one(db)
.await
.or_log((StatusCode::INTERNAL_SERVER_ERROR, "failed to get season"))?;

if let Some(curr_season) = curr_season {
Ok(curr_season)
} else {
Ok(Season::find_by_id(1)
.one(db)
.await
.or_log((StatusCode::INTERNAL_SERVER_ERROR, "failed to get season"))?
.ok_or((StatusCode::INTERNAL_SERVER_ERROR, "no season 1"))?)
}
}
Loading