Skip to content

Commit

Permalink
vingo: use time with timezone for seasons start and end
Browse files Browse the repository at this point in the history
  • Loading branch information
xerbalind committed Dec 5, 2024
1 parent 6159433 commit 363c99d
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 15 deletions.
2 changes: 2 additions & 0 deletions vingo/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod m20240829_234032_create_card;
mod m20240903_194156_create_scan;
mod m20240909_195645_create_days;
mod m20240909_214352_create_seasons;
mod m20241205_210208_add_timezone;

pub struct Migrator;

Expand All @@ -17,6 +18,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240903_194156_create_scan::Migration),
Box::new(m20240909_195645_create_days::Migration),
Box::new(m20240909_214352_create_seasons::Migration),
Box::new(m20241205_210208_add_timezone::Migration),
]
}
}
46 changes: 46 additions & 0 deletions vingo/migration/src/m20241205_210208_add_timezone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[derive(DeriveIden)]
enum Season {
Table,
Start,
End,
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Season::Table)
.modify_column(
ColumnDef::new(Season::Start)
.timestamp_with_time_zone()
.default(Expr::current_timestamp()),
)
.modify_column(
ColumnDef::new(Season::End)
.timestamp_with_time_zone()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Season::Table)
.modify_column(ColumnDef::new(Season::Start).date())
.modify_column(ColumnDef::new(Season::End).date())
.to_owned(),
)
.await
}
}
2 changes: 1 addition & 1 deletion vingo/src/entities/card.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion vingo/src/entities/day.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion vingo/src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
pub mod prelude;

Expand Down
2 changes: 1 addition & 1 deletion vingo/src/entities/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
pub use super::card::Entity as Card;
pub use super::day::Entity as Day;
Expand Down
2 changes: 1 addition & 1 deletion vingo/src/entities/scan.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
6 changes: 3 additions & 3 deletions vingo/src/entities/season.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand All @@ -10,8 +10,8 @@ pub struct Model {
pub id: i32,
#[sea_orm(column_type = "Text")]
pub name: String,
pub start: Date,
pub end: Date,
pub start: DateTimeWithTimeZone,
pub end: DateTimeWithTimeZone,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
2 changes: 1 addition & 1 deletion vingo/src/entities/user.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
11 changes: 5 additions & 6 deletions vingo/src/routes/seasons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use axum::{
extract::{Path, State},
Json,
};
use chrono::NaiveDate;

use reqwest::StatusCode;
use sea_orm::{sea_query::Expr, DatabaseConnection};
use sea_orm::{prelude::DateTimeWithTimeZone, sea_query::Expr, DatabaseConnection};
use sea_orm::{
ActiveModelTrait, EntityTrait, FromQueryResult, QueryFilter, QuerySelect, QueryTrait, Set,
};
Expand All @@ -22,8 +21,8 @@ use super::util::errors::{ResponseResult, ResultAndLogError};
pub struct SeasonGet {
id: i32,
name: String,
start: NaiveDate,
end: NaiveDate,
start: DateTimeWithTimeZone,
end: DateTimeWithTimeZone,
is_current: bool,
}

Expand Down Expand Up @@ -56,8 +55,8 @@ pub async fn db_seasons(db: &DatabaseConnection, future: bool) -> ResponseResult
#[derive(Debug, Serialize, Deserialize)]
pub struct SeasonAddBody {
name: String,
start: NaiveDate,
end: NaiveDate,
start: DateTimeWithTimeZone,
end: DateTimeWithTimeZone,
}
pub async fn add(
state: State<AppState>,
Expand Down

0 comments on commit 363c99d

Please sign in to comment.