Implement api to create tag and change file tags

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 6c51808fe0
commit 0d3fffe980

@ -828,8 +828,8 @@ checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
[[package]] [[package]]
name = "mediarepo-api" name = "mediarepo-api"
version = "0.2.0" version = "0.3.0"
source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=ea72553cd5284e785abea815d6d6b9ad262d880a#ea72553cd5284e785abea815d6d6b9ad262d880a" source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=6cd483a4a7d0a101f391b755b73fa253e70b4a29#6cd483a4a7d0a101f391b755b73fa253e70b4a29"
dependencies = [ dependencies = [
"chrono", "chrono",
"serde", "serde",

@ -308,6 +308,19 @@ impl File {
Ok(()) Ok(())
} }
/// Removes multiple tags from the file
#[tracing::instrument(level = "debug", skip(self))]
pub async fn remove_tags(&self, tag_ids: Vec<i64>) -> RepoResult<()> {
let hash_id = self.hash.id;
hash_tag::Entity::delete_many()
.filter(hash_tag::Column::HashId.eq(hash_id))
.filter(hash_tag::Column::TagId.is_in(tag_ids))
.exec(&self.db)
.await?;
Ok(())
}
/// Returns the reader for the file /// Returns the reader for the file
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn get_reader(&self) -> RepoResult<BufReader<tokio::fs::File>> { pub async fn get_reader(&self) -> RepoResult<BufReader<tokio::fs::File>> {

@ -769,8 +769,8 @@ checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
[[package]] [[package]]
name = "mediarepo-api" name = "mediarepo-api"
version = "0.2.0" version = "0.3.0"
source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=ea72553cd5284e785abea815d6d6b9ad262d880a#ea72553cd5284e785abea815d6d6b9ad262d880a" source = "git+https://github.com/Trivernis/mediarepo-api.git?rev=6cd483a4a7d0a101f391b755b73fa253e70b4a29#6cd483a4a7d0a101f391b755b73fa253e70b4a29"
dependencies = [ dependencies = [
"chrono", "chrono",
"serde", "serde",

@ -33,4 +33,4 @@ features = ["tokio-executor"]
[dependencies.mediarepo-api] [dependencies.mediarepo-api]
git = "https://github.com/Trivernis/mediarepo-api.git" git = "https://github.com/Trivernis/mediarepo-api.git"
rev = "ea72553cd5284e785abea815d6d6b9ad262d880a" rev = "6cd483a4a7d0a101f391b755b73fa253e70b4a29"

@ -1,7 +1,7 @@
use crate::from_model::FromModel; use crate::from_model::FromModel;
use crate::utils::{file_by_identifier, get_repo_from_context}; use crate::utils::{file_by_identifier, get_repo_from_context};
use mediarepo_api::types::files::{GetFileTagsRequest, GetFilesTagsRequest}; use mediarepo_api::types::files::{GetFileTagsRequest, GetFilesTagsRequest};
use mediarepo_api::types::tags::TagResponse; use mediarepo_api::types::tags::{ChangeFileTagsRequest, TagResponse};
use mediarepo_core::rmp_ipc::prelude::*; use mediarepo_core::rmp_ipc::prelude::*;
pub struct TagsNamespace; pub struct TagsNamespace;
@ -15,7 +15,9 @@ impl NamespaceProvider for TagsNamespace {
events!(handler, events!(handler,
"all_tags" => Self::all_tags, "all_tags" => Self::all_tags,
"tags_for_file" => Self::tags_for_file, "tags_for_file" => Self::tags_for_file,
"tags_for_files" => Self::tags_for_files "tags_for_files" => Self::tags_for_files,
"create_tags" => Self::create_tags,
"change_file_tags" => Self::change_file_tags
); );
} }
} }
@ -71,4 +73,48 @@ impl TagsNamespace {
Ok(()) Ok(())
} }
/// Creates all tags given as input or returns the existing tag
#[tracing::instrument(skip_all)]
async fn create_tags(ctx: &Context, event: Event) -> IPCResult<()> {
let repo = get_repo_from_context(ctx).await;
let tags = event.data::<Vec<String>>()?;
let mut created_tags = Vec::new();
for tag in tags {
let created_tag = repo.add_or_find_tag(tag).await?;
created_tags.push(created_tag);
}
let responses: Vec<TagResponse> = created_tags
.into_iter()
.map(TagResponse::from_model)
.collect();
ctx.emitter
.emit_response_to(event.id(), Self::name(), "create_tags", responses)
.await?;
Ok(())
}
/// Changes tags of a file
/// it removes the tags from the removed list and adds the one from the add list
#[tracing::instrument(skip_all)]
async fn change_file_tags(ctx: &Context, event: Event) -> IPCResult<()> {
let repo = get_repo_from_context(ctx).await;
let request = event.data::<ChangeFileTagsRequest>()?;
let file = file_by_identifier(request.file_id, &repo).await?;
file.add_tags(request.added_tags).await?;
file.remove_tags(request.removed_tags).await?;
let responses: Vec<TagResponse> = file
.tags()
.await?
.into_iter()
.map(TagResponse::from_model)
.collect();
ctx.emitter
.emit_response_to(event.id(), Self::name(), "change_file_tags", responses)
.await?;
Ok(())
}
} }

Loading…
Cancel
Save