(toggl-portal): Add project caching

This commit is contained in:
Joshua Coles 2023-11-06 20:10:10 +00:00
parent 8e9f8538de
commit 64570190c8
5 changed files with 41 additions and 7 deletions

View File

@ -11,8 +11,8 @@ impl MigrationTrait for Migration {
Table::create() Table::create()
.table(Project::Table) .table(Project::Table)
.if_not_exists() .if_not_exists()
.col(ColumnDef::new(Project::Id).integer().not_null()) .col(ColumnDef::new(Project::Id).integer().primary_key().auto_increment().not_null())
.col(ColumnDef::new(Project::TogglId).big_unsigned().not_null()) .col(ColumnDef::new(Project::TogglId).big_unsigned().not_null().unique_key())
.col( .col(
ColumnDef::new(Project::WorkspaceId) ColumnDef::new(Project::WorkspaceId)
.big_unsigned() .big_unsigned()

View File

@ -1,5 +1,5 @@
use sea_orm::{NotSet, Set}; use sea_orm::{NotSet, Set};
use crate::entity::{time_entry, client}; use crate::entity::{time_entry, client, project};
use crate::types::{Project, ProjectClient, ReportEntry}; use crate::types::{Project, ProjectClient, ReportEntry};
impl 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()),
}
}
}

View File

@ -6,7 +6,9 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "project")] #[sea_orm(table_name = "project")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)]
pub id: i32, pub id: i32,
#[sea_orm(unique)]
pub toggl_id: i64, pub toggl_id: i64,
pub workspace_id: i64, pub workspace_id: i64,
pub client_id: Option<i32>, pub client_id: Option<i32>,

View File

@ -85,8 +85,26 @@ pub async fn start_time_entry(
Ok((StatusCode::OK, "Ok")) Ok((StatusCode::OK, "Ok"))
} }
async fn projects(Extension(toggl_client): Extension<TogglClient>) -> Result<Json<Vec<Project>>> { async fn projects(
Extension(db): Extension<DatabaseConnection>,
Extension(toggl_client): Extension<TogglClient>
) -> Result<Json<Vec<Project>>> {
let projects = toggl_client.fetch_projects().await?; 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)) Ok(Json(projects))
} }

View File

@ -48,10 +48,10 @@ pub struct Current {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Project { pub struct Project {
pub id: u64, pub id: u64,
workspace_id: u64, pub workspace_id: u64,
pub client_id: Option<u64>, pub client_id: Option<u64>,
name: String, pub name: String,
active: bool, pub active: bool,
#[serde(flatten)] #[serde(flatten)]
pub rest: HashMap<String, Value>, pub rest: HashMap<String, Value>,