Skip to content

Commit

Permalink
Merge pull request #24 from Bill2015/develop-resource-rename-the-file
Browse files Browse the repository at this point in the history
Add feature that can rename the file from resource page
  • Loading branch information
Bill2015 authored Feb 25, 2024
2 parents ad61ede + c950461 commit ae134cb
Show file tree
Hide file tree
Showing 21 changed files with 311 additions and 38 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn main() {
resource::application::add_resource_tag,
resource::application::remove_resource_tag,
resource::application::update_resource_tag,
resource::application::rename_resource_file_name,
resource::application::get_resource_detail,
resource::application::list_resource,
resource::application::querying_by_string,
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/modules/resource/application/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pub use resource_remove_tag::*;

mod resource_update_tag;
pub use resource_update_tag::*;

mod rename_file;
pub use rename_file::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct ResourceRenameFileDto {
pub id: String,

pub new_name: Option<String>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use anyhow::Error;
use async_trait::async_trait;
use serde::Deserialize;

use crate::command_from_dto;
use crate::modules::resource::domain::{ResourceGenericError, ResourceID};
use crate::modules::resource::repository::ResourceRepository;
use crate::modules::common::application::ICommandHandler;

mod dto;
pub use dto::*;

#[derive(Deserialize)]
pub struct ResourceRenameFileCommand {
pub id: String,

pub new_name: Option<String>,
}
command_from_dto!(ResourceRenameFileCommand, ResourceRenameFileDto);

// =====================================
pub struct ResourceRenameFileHandler<'a> {
resource_repo: &'a ResourceRepository<'a>,
}

impl<'a> ResourceRenameFileHandler<'a> {
pub fn register(resource_repo: &'a ResourceRepository) -> Self {
Self { resource_repo }
}
}

#[async_trait]
impl ICommandHandler<ResourceRenameFileCommand> for ResourceRenameFileHandler<'_> {

fn get_name() -> String {
String::from("Create Resource Command")
}

type Output = ResourceID;

async fn execute(&self, command: ResourceRenameFileCommand) -> Result<Self::Output, Error> {
let ResourceRenameFileCommand {
id,
new_name,
} = command;

// find by id
let mut resource = self.resource_repo
.find_by_id(id)
.await
.or(Err(ResourceGenericError::DBInternalError()))?
.ok_or(ResourceGenericError::IdNotFound())?;

// rename the file
resource.rename_file(new_name)?;

// save
let result = self.resource_repo
.save(resource)
.await;

match result {
Ok(value) => Ok(value.take_id()),
_ => Err(ResourceGenericError::DBInternalError().into()),
}
}
}
7 changes: 6 additions & 1 deletion src-tauri/src/modules/resource/application/dto/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ impl Into<TaggingAttrPayload> for ResourceTaggingAttrPayloadDto {
Self::Bool(val) => TaggingAttrPayload::Bool(val),
}
}
}
}

#[derive(Serialize, Deserialize)]
pub struct ResourceIdOnlyDto {
id: String,
}
2 changes: 1 addition & 1 deletion src-tauri/src/modules/resource/application/dto/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct ResourceFileDto {

pub path: String,

pub ext: String,
pub ext: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
11 changes: 11 additions & 0 deletions src-tauri/src/modules/resource/application/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ impl<'a> ResourceService<'a> {
Ok(result)
}

pub async fn rename_file(&self, data: ResourceRenameFileDto) -> Result<ResourceID, ResourceError> {
let command = ResourceRenameFileCommand::from(data);

let result = ResourceRenameFileHandler::register(self.resource_repository)
.execute(command)
.await
.map_err(|err| ResourceError::Rename(anyhow!(err)))?;

Ok(result)
}

pub async fn update_resource(&self, data: UpdateResourceDto) -> Result<ResourceID, ResourceError> {
let command = UpdateResourceCommand::from(data);

Expand Down
9 changes: 9 additions & 0 deletions src-tauri/src/modules/resource/application/tauri-command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ pub async fn remove_resource_tag(data: ResourceRemoveTagDto) -> Result<ResourceI
Ok(result)
}

#[tauri::command(rename_all = "snake_case")]
pub async fn rename_resource_file_name(data: ResourceRenameFileDto) -> Result<ResourceID, ResourceError> {
let result = RESOURCE_SERVICE
.rename_file(data)
.await?;

Ok(result)
}

#[tauri::command(rename_all = "snake_case")]
pub async fn update_resource_tag(data: ResourceUpdateTagDto) -> Result<ResourceID, ResourceError> {
let result = RESOURCE_SERVICE
Expand Down
10 changes: 10 additions & 0 deletions src-tauri/src/modules/resource/domain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub enum ResourceError {

#[error("Explore file failed")]
ExploreFile(Error),

#[error("Rename file failed")]
Rename(Error),
}

impl Serialize for ResourceError {
Expand All @@ -57,6 +60,7 @@ impl Serialize for ResourceError {
ResourceError::RemoveTag(source) => serialize_error!(self, source),
ResourceError::UpdateTag(source) => serialize_error!(self, source),
ResourceError::ExploreFile(source) => serialize_error!(self, source),
ResourceError::Rename(source) => serialize_error!(self, source),
};
error_message.serialize(serializer)
}
Expand All @@ -79,6 +83,9 @@ pub enum ResourceGenericError {
#[error("No File name")]
FileNameIsEmpty(),

#[error("Rename file failed")]
RenameFileFailed(),

#[error("Name is empty")]
NameIsEmpty(),

Expand All @@ -97,6 +104,9 @@ pub enum ResourceGenericError {
#[error("Id not found")]
IdNotFound(),

#[error("File Is Empty")]
FileIsEmpty(),

#[error("Belong Category id is not exists")]
BelongCategoryNotExists(),

Expand Down
52 changes: 52 additions & 0 deletions src-tauri/src/modules/resource/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::fs;
use std::path::Path;

use chrono::{DateTime, Utc};
use serde::Serialize;
use crate::base_aggregate;
use crate::modules::category::domain::CategoryID;
use crate::modules::common::domain::ToPlainObject;
use crate::modules::common::infrastructure::dateutils;
use crate::utils::StringUtils;

mod id;
pub use id::ResourceID;
Expand Down Expand Up @@ -51,6 +55,54 @@ impl Resource {
Ok(())
}

pub fn rename_file(&mut self, new_name: Option<String>) -> Result<(), ResourceGenericError> {
if self.file.is_none() {
return Err(ResourceGenericError::FileIsEmpty());
}

// get new name
let new_name = new_name.unwrap_or(self.name.clone());
let ResourceFileVO { uuid, name, path, ext } = self.file.to_owned().unwrap();

// if same as new, do nothing
if name == new_name {
return Ok(());
}

let file_path = String::from(Path::new(&path).file_name().unwrap().to_str().unwrap());
let new_path = Path::new(path.slice(..path.chars().count() - file_path.chars().count()))
.join(&new_name)
.to_str()
.unwrap()
.to_string();

let new_path: String = match &ext {
Some(ex) => [new_path, ex.to_string()].join("."),
None => new_path,
};

// check filename is already exist (conflict)
if Path::new(&self.root_path).join(&new_path).exists() {
return Err(ResourceGenericError::RenameFileFailed());
}

fs::rename(
Path::new(&self.root_path).join(&path),
Path::new(&self.root_path).join(&new_path)
).or(Err(ResourceGenericError::RenameFileFailed()))?;

self.file = Some(ResourceFileVO {
name: new_name.clone(),
path: new_path,
uuid: uuid,
ext: ext,
});

self.name = new_name;

Ok(())
}

pub fn change_description(&mut self, new_description: String) -> Result<(), ResourceGenericError> {
self.description = new_description;
Ok(())
Expand Down
21 changes: 15 additions & 6 deletions src-tauri/src/modules/resource/domain/valueobj/filevo.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::ffi::OsStr;
use std::path::Path;

use serde::Serialize;

use crate::modules::resource::domain::ResourceGenericError;
use crate::utils::StringUtils;

#[derive(Debug, Serialize, Clone)]
pub struct ResourceFileVO {
pub uuid: String,
pub name: String,
pub path: String,
pub ext: String,
pub ext: Option<String>,
}

impl ResourceFileVO {
Expand All @@ -32,15 +32,24 @@ impl ResourceFileVO {
}

let ext = match path.is_file() {
true => path.extension().unwrap_or(OsStr::new("txt")),
false => OsStr::new("folder"),
true => path.extension()
.map(|osr| Some(String::from(osr.to_str().unwrap())))
.unwrap_or(None),
false => Some("folder".to_string()),
};

let name = String::from(path.file_name().unwrap().to_str().unwrap());
let name = match path.is_file() {
true if ext.is_none()=> name,
true => String::from(name.slice(..name.chars().count() - ext.as_ref().unwrap().chars().count() - 1)),
false => name,
};

Ok(
ResourceFileVO {
uuid: String::from("id"),
name: String::from(path.file_name().unwrap().to_str().unwrap()),
ext: String::from(ext.to_str().unwrap()),
name: name,
ext: ext,
path: String::from(main_path),
}
)
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/modules/resource/repository/data_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct ResourceFileDo {
pub uuid: String,
pub name: String,
pub path: String,
pub ext: String,
pub ext: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down
6 changes: 6 additions & 0 deletions src/api/resource/Dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export interface ResourceCreateDto {
url_path?: string | null,
}

export interface ResourceRenameFileDto {
id: string,

new_name?: string,
}

export interface ResourceUpdateDto {
id: string,

Expand Down
5 changes: 5 additions & 0 deletions src/api/resource/ResourceAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
QueryResoruceDto,
ResourceCreateDto,
ResourceDetailDto,
ResourceRenameFileDto,
ResourceResDto,
ResourceTagOperateDto,
ResourceUpdateDto,
Expand Down Expand Up @@ -40,6 +41,10 @@ export namespace ResourceAPI {
return invoke<string>('update_resource', { data });
}

export function renameTheFile(data: ResourceRenameFileDto) {
return invoke<string>('rename_resource_file_name', { data });
}

export function exporeTheFile(filePath: string) {
return invoke('explore_the_file', { file_path: filePath });
}
Expand Down
8 changes: 7 additions & 1 deletion src/api/resource/ResourceMutation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMutation } from '@tanstack/react-query';
import { ResourceAPI } from './ResourceAPI';
import { ResourceCreateDto, ResourceTagOperateDto, ResourceUpdateDto, ResourceUpdateTagDto } from './Dto';
import { ResourceCreateDto, ResourceRenameFileDto, ResourceTagOperateDto, ResourceUpdateDto, ResourceUpdateTagDto } from './Dto';

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace ResourceMutation {
Expand All @@ -22,6 +22,12 @@ export namespace ResourceMutation {
return useMutation({ mutationFn: mutationFn });
}

export function useRenameFile() {
const mutationFn = (data: ResourceRenameFileDto) => ResourceAPI.renameTheFile(data);

return useMutation({ mutationFn: mutationFn });
}

export function useAddTag() {
const mutationFn = (data: ResourceTagOperateDto) => ResourceAPI.addTag(data);

Expand Down
9 changes: 9 additions & 0 deletions src/assets/locales/pages/resource-detail/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Main": {
"name": "name",
"description": "description"
},
"Icons": {
"rename": "Rename the file by current name"
}
}
9 changes: 9 additions & 0 deletions src/assets/locales/pages/resource-detail/zh-TW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Main": {
"name": "名稱",
"description": "描述"
},
"Icons": {
"rename": "使用現在的名稱重新命名此檔案"
}
}
Loading

0 comments on commit ae134cb

Please sign in to comment.