-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathanswer.rs
More file actions
174 lines (157 loc) · 6 KB
/
answer.rs
File metadata and controls
174 lines (157 loc) · 6 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
//! Answer handler for the Chaos application.
//!
//! This module provides HTTP request handlers for managing application answers, including:
//! - Creating and retrieving answers
//! - Updating and deleting answers
//! - Managing role-specific answers
use crate::models::answer::{Answer, NewAnswer};
use crate::models::app::{AppMessage, AppState, IdMessage};
use crate::models::auth::{AnswerOwner, ApplicationOwner, ApplicationOwnerOrReviewer};
use crate::models::error::ChaosError;
use crate::models::transaction::DBTransaction;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use serde_json::json;
use crate::models::application::{OpenApplicationByAnswerId, OpenApplicationByApplicationId};
/// Handler for answer-related HTTP requests.
pub struct AnswerHandler;
impl AnswerHandler {
/// Creates a new answer for an application.
///
/// This handler allows application owners to create answers for their application.
/// The application must be open and not already submitted.
///
/// # Arguments
///
/// * `state` - The application state
/// * `application_id` - The ID of the application
/// * `_user` - The authenticated user (must be the application owner)
/// * `_` - Ensures the application is open
/// * `transaction` - Database transaction
/// * `data` - The answer details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Created answer ID or error
pub async fn create(
State(mut state): State<AppState>,
Path(application_id): Path<i64>,
_user: ApplicationOwner,
_: OpenApplicationByApplicationId,
mut transaction: DBTransaction<'_>,
Json(data): Json<NewAnswer>,
) -> Result<impl IntoResponse, ChaosError> {
// TODO: Check whether the question is contained in the campaign being applied to
let id = Answer::create(
application_id,
data.question_id,
data.data,
&mut state.snowflake_generator,
&mut transaction.tx,
)
.await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(IdMessage { id })))
}
/// Retrieves all common answers for an application.
///
/// This handler allows application owners to view all common answers.
///
/// # Arguments
///
/// * `application_id` - The ID of the application
/// * `_owner` - The authenticated user (must be the application owner)
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of answers or error
pub async fn get_all_common_by_application(
Path(application_id): Path<i64>,
_owner: ApplicationOwnerOrReviewer,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let answers =
Answer::get_all_common_by_application(application_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(( Json(answers)))
}
/// Retrieves all answers for a specific role in an application.
///
/// This handler allows application owners to view role-specific answers.
///
/// # Arguments
///
/// * `application_id` - The ID of the application
/// * `role_id` - The ID of the role
/// * `_owner` - The authenticated user (must be the application owner)
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of answers or error
pub async fn get_all_by_application_and_role(
Path((application_id, role_id)): Path<(i64, i64)>,
_owner: ApplicationOwnerOrReviewer,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let answers =
Answer::get_all_by_application_and_role(application_id, role_id, &mut transaction.tx)
.await?;
transaction.tx.commit().await?;
Ok(( Json(answers)))
}
/// Updates an existing answer.
///
/// This handler allows answer owners to update their answers.
/// The application must be open and not already submitted.
///
/// # Arguments
///
/// * `answer_id` - The ID of the answer to update
/// * `_owner` - The authenticated user (must be the answer owner)
/// * `_` - Ensures the application is open
/// * `transaction` - Database transaction
/// * `data` - The new answer details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn update(
Path(answer_id): Path<i64>,
_owner: AnswerOwner,
_: OpenApplicationByAnswerId, // Troublesome throws BadRequest
mut transaction: DBTransaction<'_>,
Json(new_answer): Json<NewAnswer>,
) -> Result<impl IntoResponse, ChaosError> {
Answer::update(answer_id, new_answer.data, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully updated answer"))
}
/// Deletes an answer.
///
/// This handler allows answer owners to delete their answers.
/// The application must be open and not already submitted.
///
/// # Arguments
///
/// * `answer_id` - The ID of the answer to delete
/// * `_owner` - The authenticated user (must be the answer owner)
/// * `_` - Ensures the application is open
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn delete(
Path(answer_id): Path<i64>,
_owner: AnswerOwner,
_: OpenApplicationByAnswerId,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
Answer::delete(answer_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully deleted answer"))
}
}