progress-pile/progress-pile-server/src/entity/progress_entry.rs

71 lines
1.9 KiB
Rust
Raw Normal View History

2025-05-12 08:49:51 +09:00
use async_graphql::*;
2025-05-13 08:54:15 +09:00
use chrono::Local;
use sea_orm::{entity::prelude::*, ActiveValue::Set};
2025-05-12 08:49:51 +09:00
use serde::{Deserialize, Serialize};
2025-05-13 08:54:15 +09:00
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "progress_entry")]
2025-05-12 08:49:51 +09:00
pub struct Model {
2025-05-13 08:54:15 +09:00
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub user_id: i32,
#[sea_orm(indexed)]
pub progress_category_id: Uuid,
#[sea_orm(indexed)]
pub created_at: DateTimeWithTimeZone,
#[sea_orm(indexed)]
pub updated_at: DateTimeWithTimeZone,
#[sea_orm(indexed)]
pub deleted_at: Option<DateTimeWithTimeZone>,
#[sea_orm(indexed)]
pub progressed_at: DateTimeWithTimeZone,
pub quantity: i32,
pub note: String,
2025-05-12 08:49:51 +09:00
}
#[derive(Copy, Clone, Debug, DeriveRelation, EnumIter)]
pub enum Relation {
#[sea_orm(
2025-05-13 08:54:15 +09:00
belongs_to = "super::progress_category::Entity",
from = "Column::ProgressCategoryId",
to = "super::progress_category::Column::Id"
2025-05-12 08:49:51 +09:00
)]
2025-05-13 08:54:15 +09:00
ProgressCategory,
2025-05-12 08:49:51 +09:00
#[sea_orm(
2025-05-13 08:54:15 +09:00
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id"
2025-05-12 08:49:51 +09:00
)]
2025-05-13 08:54:15 +09:00
User,
2025-05-12 08:49:51 +09:00
}
2025-05-13 08:54:15 +09:00
impl Related<super::progress_category::Entity> for Entity {
2025-05-12 08:49:51 +09:00
fn to() -> RelationDef {
2025-05-13 08:54:15 +09:00
Relation::ProgressCategory.def()
2025-05-12 08:49:51 +09:00
}
}
2025-05-13 08:54:15 +09:00
impl Related<super::user::Entity> for Entity {
2025-05-12 08:49:51 +09:00
fn to() -> RelationDef {
2025-05-13 08:54:15 +09:00
Relation::User.def()
2025-05-12 08:49:51 +09:00
}
}
2025-05-13 08:54:15 +09:00
impl ActiveModelBehavior for ActiveModel {}
impl ActiveModel {
pub fn new() -> Self {
let timestamp: DateTimeWithTimeZone = Local::now().fixed_offset();
Self{
id: Set(Uuid::new_v4()),
created_at: Set(timestamp),
updated_at: Set(timestamp),
progressed_at: Set(timestamp),
quantity: Set(1),
note: Set("".to_string()),
..Default::default()
}
}
}