when adding previously deleted project, restore it

This commit is contained in:
Nikita Galaiko 2023-02-21 15:37:50 +01:00
parent af24341623
commit 8f4605f6e1
No known key found for this signature in database
GPG Key ID: EBAB54E845BA519D
4 changed files with 67 additions and 23 deletions

View File

@ -147,9 +147,25 @@ fn add_project(
}
})? {
if project.path == path {
return Err(Error {
message: "Project already exists".to_string(),
});
if !project.deleted {
return Err(Error {
message: "Project already exists".to_string(),
});
} else {
projects_storage
.update_project(&projects::UpdateRequest {
id: project.id.clone(),
deleted: Some(false),
..Default::default()
})
.map_err(|e| {
log::error!("{}", e);
Error {
message: "Failed to undelete project".to_string(),
}
})?;
return Ok(project);
}
}
}
@ -212,12 +228,18 @@ fn delete_project(handle: tauri::AppHandle, id: &str) -> Result<(), Error> {
}
})?;
projects_storage.delete_project(id).map_err(|e| {
log::error!("{}", e);
Error {
message: "Failed to delete project".to_string(),
}
})?;
projects_storage
.update_project(&projects::UpdateRequest {
id: id.to_string(),
deleted: Some(true),
..Default::default()
})
.map_err(|e| {
log::error!("{}", e);
Error {
message: "Failed to delete project".to_string(),
}
})?;
Ok(())
}

View File

@ -20,6 +20,8 @@ pub struct Project {
pub id: String,
pub title: String,
pub path: String,
#[serde(default)]
pub deleted: bool,
pub api: Option<ApiProject>,
}
@ -68,6 +70,7 @@ impl Project {
.map(|p| p.to_str().unwrap().to_string())
.map(|title| Self {
id: uuid::Uuid::new_v4().to_string(),
deleted: false,
title,
path: path.to_str().unwrap().to_string(),
api: None,

View File

@ -10,11 +10,12 @@ pub struct Storage {
storage: storage::Storage,
}
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct UpdateRequest {
id: String,
title: Option<String>,
api: Option<project::ApiProject>,
pub id: String,
pub title: Option<String>,
pub deleted: Option<bool>,
pub api: Option<project::ApiProject>,
}
impl Storage {
@ -24,14 +25,28 @@ impl Storage {
pub fn list_projects(&self) -> Result<Vec<project::Project>> {
match self.storage.read(PROJECTS_FILE)? {
Some(projects) => Ok(serde_json::from_str(&projects)?),
Some(projects) => {
let all_projects: Vec<project::Project> = serde_json::from_str(&projects)?;
let non_deleted_projects = all_projects
.into_iter()
.filter(|p: &project::Project| !p.deleted)
.collect();
Ok(non_deleted_projects)
}
None => Ok(vec![]),
}
}
pub fn get_project(&self, id: &str) -> Result<Option<project::Project>> {
let projects = self.list_projects()?;
Ok(projects.into_iter().find(|p| p.id == id))
let project = projects.into_iter().find(|p| p.id == id);
match project {
Some(p) => match p.deleted {
true => Ok(None),
false => Ok(Some(p)),
},
None => Ok(None),
}
}
pub fn update_project(&self, update_request: &UpdateRequest) -> Result<project::Project> {
@ -49,6 +64,10 @@ impl Storage {
project.api = Some(api.clone());
}
if let Some(deleted) = &update_request.deleted {
project.deleted = *deleted;
}
let projects = serde_json::to_string(&projects)?;
self.storage.write(PROJECTS_FILE, &projects)?;
Ok(self.get_project(&update_request.id)?.unwrap())
@ -61,12 +80,4 @@ impl Storage {
self.storage.write(PROJECTS_FILE, &projects)?;
Ok(())
}
pub fn delete_project(&self, id: &str) -> Result<()> {
let mut projects = self.list_projects()?;
projects.retain(|p| p.id != id);
let projects = serde_json::to_string(&projects)?;
self.storage.write(PROJECTS_FILE, &projects)?;
Ok(())
}
}

View File

@ -22,6 +22,8 @@ const update = (params: {
const add = (params: { path: string }) =>
invoke<Project>("add_project", params);
const del = (params: { id: string }) => invoke("delete_project", params);
export default async () => {
const init = await list();
const store = writable<Project[]>(init);
@ -55,5 +57,11 @@ export default async () => {
store.update((projects) => [...projects, project]);
return project;
}),
delete: (params: { id: string }) =>
del(params).then(() => {
store.update((projects) =>
projects.filter((p) => p.id !== params.id)
);
}),
};
};