-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresolvers.rs
More file actions
203 lines (168 loc) · 8.78 KB
/
resolvers.rs
File metadata and controls
203 lines (168 loc) · 8.78 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use async_graphql::{Context, Error, ErrorExtensions, FieldResult, Object};
use event_store_adapter_rs::types::EventStoreWriteError;
use std::str::FromStr;
use command_domain::group_chat::{GroupChatId, GroupChatName, MemberRole, Message, MessageId};
use command_domain::user_account::UserAccountId;
use command_interface_adaptor_if::GroupChatRepositoryError;
use command_processor::group_chat_command_processor::CommandProcessError;
use crate::gateways::group_chat_repository::GroupChatRepositoryImpl;
use crate::graphql::inputs::{
AddMemberInput, CreateGroupChatInput, DeleteGroupChatInput, DeleteMessageInput, EditMessageInput, PostMessageInput,
RemoveMemberInput, RenameGroupChatInput,
};
use crate::graphql::outputs::{GroupChatOut, MessageOut};
use crate::graphql::{MutationRoot, ServiceContext, ES};
#[Object]
impl MutationRoot {
async fn create_group_chat<'ctx>(
&self,
ctx: &Context<'ctx>,
input: CreateGroupChatInput,
) -> FieldResult<GroupChatOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_name = validate_group_chat_name(&input.name)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.create_group_chat(group_chat_name, executor_id)
.await
.map(|group_chat_id| GroupChatOut::new(group_chat_id.to_string()))
.map_err(error_handling)
}
async fn delete_group_chat<'ctx>(
&self,
ctx: &Context<'ctx>,
input: DeleteGroupChatInput,
) -> FieldResult<GroupChatOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_id = validate_group_chat_id(&input.group_chat_id)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.delete_group_chat(group_chat_id, executor_id)
.await
.map(|group_chat_id| GroupChatOut::new(group_chat_id.to_string()))
.map_err(error_handling)
}
async fn rename_group_chat<'ctx>(
&self,
ctx: &Context<'ctx>,
input: RenameGroupChatInput,
) -> FieldResult<GroupChatOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_id = validate_group_chat_id(&input.group_chat_id)?;
let group_chat_name = validate_group_chat_name(&input.name)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.rename_group_chat(group_chat_id, group_chat_name, executor_id)
.await
.map(|group_chat_id| GroupChatOut::new(group_chat_id.to_string()))
.map_err(error_handling)
}
async fn add_member<'ctx>(&self, ctx: &Context<'ctx>, input: AddMemberInput) -> FieldResult<GroupChatOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_id = validate_group_chat_id(&input.group_chat_id)?;
let user_account_id = validate_user_account_id(&input.user_account_id)?;
let role = validate_member_role(&input.role)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.add_member(group_chat_id, user_account_id, role, executor_id)
.await
.map(|group_chat_id| GroupChatOut::new(group_chat_id.to_string()))
.map_err(error_handling)
}
async fn remove_member<'ctx>(&self, ctx: &Context<'ctx>, input: RemoveMemberInput) -> FieldResult<GroupChatOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_id = validate_group_chat_id(&input.group_chat_id)?;
let user_account_id = validate_user_account_id(&input.user_account_id)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.remove_member(group_chat_id, user_account_id, executor_id)
.await
.map(|group_chat_id| GroupChatOut::new(group_chat_id.to_string()))
.map_err(error_handling)
}
async fn post_message<'ctx>(&self, ctx: &Context<'ctx>, input: PostMessageInput) -> FieldResult<MessageOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_id = validate_group_chat_id(&input.group_chat_id)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let message = validate_message(&input.content, MessageId::new(), executor_id.clone())?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.post_message(group_chat_id, message, executor_id)
.await
.map(|(group_chat_id, message_id)| MessageOut::new(group_chat_id.to_string(), message_id.to_string()))
.map_err(error_handling)
}
async fn edit_message<'ctx>(&self, ctx: &Context<'ctx>, input: EditMessageInput) -> FieldResult<GroupChatOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_id = validate_group_chat_id(&input.group_chat_id)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let message_id = validate_message_id(&input.message_id)?;
let message = validate_message(&input.content, message_id, executor_id.clone())?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.edit_message(group_chat_id, message, executor_id)
.await
.map(|group_chat_id| GroupChatOut::new(group_chat_id.to_string()))
.map_err(error_handling)
}
async fn delete_message<'ctx>(&self, ctx: &Context<'ctx>, input: DeleteMessageInput) -> FieldResult<GroupChatOut> {
let service_ctx = ctx.data::<ServiceContext<GroupChatRepositoryImpl<ES>>>().unwrap();
let group_chat_id = validate_group_chat_id(&input.group_chat_id)?;
let message_id = validate_message_id(&input.message_id)?;
let executor_id = validate_user_account_id(&input.executor_id)?;
let mut processor = service_ctx.group_chat_command_processor.lock().await;
processor
.delete_message(group_chat_id, message_id, executor_id)
.await
.map(|group_chat_id| GroupChatOut::new(group_chat_id.to_string()))
.map_err(error_handling)
}
}
fn error_handling_repository_error(error: &CommandProcessError, cause: &GroupChatRepositoryError) -> Error {
match cause {
GroupChatRepositoryError::StoreError(_, EventStoreWriteError::OptimisticLockError(_)) => {
Error::new(error.to_string())
.extend_with(|_, e| e.set("code", "409"))
.extend_with(|_, e| e.set("cause", cause.to_string()))
}
GroupChatRepositoryError::StoreError(_, _) => Error::new(error.to_string())
.extend_with(|_, e| e.set("code", "500"))
.extend_with(|_, e| e.set("cause", cause.to_string())),
GroupChatRepositoryError::FindByIdError(_, _) => Error::new(error.to_string())
.extend_with(|_, e| e.set("code", "500"))
.extend_with(|_, e| e.set("cause", cause.to_string())),
}
}
fn error_handling(error: CommandProcessError) -> Error {
match error {
CommandProcessError::DomainLogicError(ref cause) => Error::new(error.to_string())
.extend_with(|_, e| e.set("code", "422"))
.extend_with(|_, e| e.set("cause", cause.to_string())),
CommandProcessError::NotFoundError => Error::new(error.to_string()).extend_with(|_, e| e.set("code", "404")),
CommandProcessError::RepositoryError(ref cause) => error_handling_repository_error(&error, cause),
}
}
fn validate_group_chat_id(value: &str) -> Result<GroupChatId, Error> {
GroupChatId::from_str(value).map_err(|error| Error::new(error.to_string()).extend_with(|_, e| e.set("code", "400")))
}
fn validate_group_chat_name(value: &str) -> Result<GroupChatName, Error> {
GroupChatName::from_str(value).map_err(|error| Error::new(error.to_string()).extend_with(|_, e| e.set("code", "400")))
}
fn validate_member_role(value: &str) -> Result<MemberRole, Error> {
MemberRole::from_str(value).map_err(|error| Error::new(error.to_string()).extend_with(|_, e| e.set("code", "400")))
}
fn validate_message_id(value: &str) -> Result<MessageId, Error> {
MessageId::from_str(value).map_err(|error| Error::new(error.to_string()).extend_with(|_, e| e.set("code", "400")))
}
fn validate_message(value: &str, message_id: MessageId, sender_id: UserAccountId) -> Result<Message, Error> {
Message::validate(&value, message_id, sender_id.clone())
.map_err(|error| Error::new(error.to_string()).extend_with(|_, e| e.set("code", "400")))
}
fn validate_user_account_id(value: &str) -> Result<UserAccountId, Error> {
UserAccountId::from_str(value).map_err(|error| Error::new(error.to_string()).extend_with(|_, e| e.set("code", "400")))
}