forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace_service.rs
More file actions
68 lines (58 loc) · 1.94 KB
/
Copy pathworkspace_service.rs
File metadata and controls
68 lines (58 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::model::workspace::{Workspace, WorkspaceType};
use crate::repositories::{
collection_repository::CollectionRepository, workspace_repository::WorkspaceRepository,
};
use crate::utils::error::AppError;
use mongodb::bson::oid::ObjectId;
#[derive(Clone)]
pub struct WorkspaceService {
workspace_repo: WorkspaceRepository,
collection_repo: CollectionRepository,
}
impl WorkspaceService {
pub fn new(workspace_repo: WorkspaceRepository, collection_repo: CollectionRepository) -> Self {
Self {
workspace_repo,
collection_repo,
}
}
pub async fn create_workspace(
&self,
user_id: ObjectId,
name: String,
workspace_type: WorkspaceType,
) -> Result<Workspace, AppError> {
let existing_workspaces = self
.workspace_repo
.find_all_by_user(user_id.clone())
.await?;
let workspace = self
.workspace_repo
.save(&Workspace::new(user_id.clone(), name, workspace_type))
.await?;
if existing_workspaces.is_empty() {
if let Some(workspace_id) = workspace.id.clone() {
self.collection_repo
.assign_workspace_to_unscoped_user_collections(user_id, workspace_id)
.await?;
}
}
Ok(workspace)
}
pub async fn get_user_workspaces(&self, user_id: ObjectId) -> Result<Vec<Workspace>, AppError> {
self.workspace_repo.find_all_by_user(user_id).await
}
pub async fn get_workspace_for_user(
&self,
workspace_id: ObjectId,
user_id: ObjectId,
) -> Result<Workspace, AppError> {
let workspace = self.workspace_repo.find_by_id(workspace_id).await?;
if workspace.user_id != user_id {
return Err(AppError::Forbidden(
"Not authorized to access this workspace".into(),
));
}
Ok(workspace)
}
}