Skip to main content

coman/core/
collection_manager_ops.rs

1use super::collection_manager::CollectionManager;
2use crate::core::collection_manager::CollectionResult;
3use crate::core::errors::CollectionError;
4use crate::core::utils::merge_headers;
5use crate::models::collection::Collection;
6
7impl CollectionManager {
8    /// Add a new collection
9    ///
10    /// If a collection with the same name exists, it will be updated.
11    pub async fn add_collection(
12        &self,
13        name: &str,
14        url: &str,
15        headers: Vec<(String, String)>,
16    ) -> CollectionResult<()> {
17        match self.get_collection(name).await {
18            Ok(c) => {
19                // Update existing collection
20                let mut c = c.unwrap();
21                c.url = url.to_string();
22                c.headers = merge_headers(c.headers.clone(), &headers);
23                self.update_add_collection(c).await?;
24            }
25            Err(_) => {
26                // Create new collection
27                let new_collection = Collection {
28                    name: name.to_string(),
29                    url: url.to_string(),
30                    headers: headers.clone(),
31                    requests: None,
32                };
33                self.update_add_collection(new_collection).await?;
34            }
35        };
36        Ok(())
37    }
38
39    /// Copy a collection to a new name
40    pub async fn copy_collection(&self, name: &str, new_name: &str) -> CollectionResult<()> {
41        match self.get_collection(name).await {
42            Ok(Some(c)) => {
43                let mut new_col = c.clone();
44                new_col.name = new_name.to_string();
45                self.update_add_collection(new_col).await?;
46                Ok(())
47            }
48            _ => Err(CollectionError::CollectionNotFound(name.to_string())),
49        }
50    }
51}