projects update api

This commit is contained in:
Nikita Galaiko 2023-02-15 15:44:00 +01:00
parent c19e374476
commit a68e520f4d
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
4 changed files with 59 additions and 5 deletions

View File

@ -73,6 +73,22 @@ fn list_sessions(
}
}
#[tauri::command]
fn update_project(
state: State<'_, AppState>,
project: projects::storage::UpdateRequest,
) -> Result<projects::project::Project, Error> {
state
.projects_storage
.update_project(&project)
.map_err(|e| {
log::error!("{}", e);
Error {
message: "Failed to update project".to_string(),
}
})
}
#[tauri::command]
fn add_project<R: Runtime>(
window: Window<R>,
@ -339,6 +355,7 @@ fn main() {
add_project,
list_projects,
delete_project,
update_project,
list_deltas,
list_sessions,
list_session_files,

View File

@ -1,6 +1,7 @@
use crate::projects::project;
use crate::storage;
use anyhow::Result;
use serde::{Deserialize, Serialize};
const PROJECTS_FILE: &str = "projects.json";
@ -8,6 +9,12 @@ pub struct Storage {
storage: storage::Storage,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateRequest {
id: String,
title: Option<String>,
}
impl Storage {
pub fn new(storage: storage::Storage) -> Self {
Self { storage }
@ -25,6 +32,20 @@ impl Storage {
Ok(projects.into_iter().find(|p| p.id == id))
}
pub fn update_project(&self, update_request: &UpdateRequest) -> Result<project::Project> {
let mut projects = self.list_projects()?;
let project = projects
.iter_mut()
.find(|p| p.id == update_request.id)
.ok_or_else(|| anyhow::anyhow!("Project not found"))?;
if let Some(title) = &update_request.title {
project.title = title.clone();
}
let projects = serde_json::to_string(&projects)?;
self.storage.write(PROJECTS_FILE, &projects)?;
Ok(self.get_project(&update_request.id)?.unwrap())
}
pub fn add_project(&self, project: &project::Project) -> Result<()> {
let mut projects = self.list_projects()?;
projects.push(project.clone());

View File

@ -9,12 +9,16 @@ export type Project = {
const list = () => invoke<Project[]>("list_projects");
const update = (params: { project: { id: string; title?: string } }) =>
invoke<Project>("update_project", params);
const add = (params: { path: string }) =>
invoke<Project>("add_project", params);
export default async () => {
const init = await list();
const store = writable<Project[]>(init);
return {
subscribe: store.subscribe,
add: (params: { path: string }) =>
@ -22,5 +26,14 @@ export default async () => {
store.update((projects) => [...projects, project]);
return project;
}),
update: (params: { project: { id: string; title?: string } }) =>
update(params).then((project) => {
console.log(project);
store.update((projects) =>
projects.map((p) => (p.id === project.id ? project : p))
);
return project;
}),
};
};

View File

@ -10,11 +10,14 @@ export const csr = true;
export const load: LayoutLoad = async () => {
const projects = building
? {
...readable<Project[]>([]),
add: () => {
throw new Error("not implemented");
},
}
...readable<Project[]>([]),
add: () => {
throw new Error("not implemented");
},
update: () => {
throw new Error("not implemented");
},
}
: await (await import("$lib/projects")).default();
const user = building
? writable<undefined>(undefined)