From 64570190c8f169a0275395c33491751b6f263781 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Mon, 6 Nov 2023 20:10:10 +0000 Subject: [PATCH] (toggl-portal): Add project caching --- .../src/m20231106_195401_create_projects.rs | 4 ++-- src/db.rs | 16 ++++++++++++++- src/entity/project.rs | 2 ++ src/main.rs | 20 ++++++++++++++++++- src/types.rs | 6 +++--- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/migration/src/m20231106_195401_create_projects.rs b/migration/src/m20231106_195401_create_projects.rs index 9ca3e37..0780d07 100644 --- a/migration/src/m20231106_195401_create_projects.rs +++ b/migration/src/m20231106_195401_create_projects.rs @@ -11,8 +11,8 @@ impl MigrationTrait for Migration { Table::create() .table(Project::Table) .if_not_exists() - .col(ColumnDef::new(Project::Id).integer().not_null()) - .col(ColumnDef::new(Project::TogglId).big_unsigned().not_null()) + .col(ColumnDef::new(Project::Id).integer().primary_key().auto_increment().not_null()) + .col(ColumnDef::new(Project::TogglId).big_unsigned().not_null().unique_key()) .col( ColumnDef::new(Project::WorkspaceId) .big_unsigned() diff --git a/src/db.rs b/src/db.rs index bc3ba77..f2a27ff 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,5 @@ use sea_orm::{NotSet, Set}; -use crate::entity::{time_entry, client}; +use crate::entity::{time_entry, client, project}; use crate::types::{Project, ProjectClient, ReportEntry}; impl ReportEntry { @@ -28,3 +28,17 @@ impl ProjectClient { } } } + +impl Project { + pub fn as_model(&self) -> project::ActiveModel { + project::ActiveModel { + id: NotSet, + toggl_id: Set(self.id as i64), + name: Set(self.name.clone()), + active: Set(self.active), + client_id: Set(self.client_id.map(|id| id as i32)), + workspace_id: Set(self.workspace_id as i64), + raw_json: Set(serde_json::to_value(self).unwrap()), + } + } +} diff --git a/src/entity/project.rs b/src/entity/project.rs index 6861d53..887f301 100644 --- a/src/entity/project.rs +++ b/src/entity/project.rs @@ -6,7 +6,9 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] #[sea_orm(table_name = "project")] pub struct Model { + #[sea_orm(primary_key)] pub id: i32, + #[sea_orm(unique)] pub toggl_id: i64, pub workspace_id: i64, pub client_id: Option, diff --git a/src/main.rs b/src/main.rs index 80e4622..ff168b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,8 +85,26 @@ pub async fn start_time_entry( Ok((StatusCode::OK, "Ok")) } -async fn projects(Extension(toggl_client): Extension) -> Result>> { +async fn projects( + Extension(db): Extension, + Extension(toggl_client): Extension +) -> Result>> { let projects = toggl_client.fetch_projects().await?; + +entity::project::Entity::insert_many(projects.iter().map(Project::as_model)) + .on_conflict( + OnConflict::column(entity::project::Column::TogglId) + .update_columns(vec![ + entity::project::Column::Name, + entity::project::Column::Active, + entity::project::Column::ClientId, + entity::project::Column::WorkspaceId, + entity::project::Column::RawJson, + ]) + .to_owned(), + ) + .exec(&db).await?; + Ok(Json(projects)) } diff --git a/src/types.rs b/src/types.rs index 60022fb..500b873 100644 --- a/src/types.rs +++ b/src/types.rs @@ -48,10 +48,10 @@ pub struct Current { #[derive(Debug, Serialize, Deserialize)] pub struct Project { pub id: u64, - workspace_id: u64, + pub workspace_id: u64, pub client_id: Option, - name: String, - active: bool, + pub name: String, + pub active: bool, #[serde(flatten)] pub rest: HashMap,