(toggl-portal): Add project caching
This commit is contained in:
parent
8e9f8538de
commit
64570190c8
@ -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()
|
||||
|
||||
16
src/db.rs
16
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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<i32>,
|
||||
|
||||
20
src/main.rs
20
src/main.rs
@ -85,8 +85,26 @@ pub async fn start_time_entry(
|
||||
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?;
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
|
||||
@ -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<u64>,
|
||||
name: String,
|
||||
active: bool,
|
||||
pub name: String,
|
||||
pub active: bool,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub rest: HashMap<String, Value>,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user