-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathemail_template.rs
More file actions
125 lines (115 loc) · 4.13 KB
/
email_template.rs
File metadata and controls
125 lines (115 loc) · 4.13 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
//! Email template handler for the Chaos application.
//!
//! This module provides HTTP request handlers for managing email templates, including:
//! - Retrieving template details
//! - Updating templates
//! - Deleting templates
use crate::models::auth::EmailTemplateAdmin;
use crate::models::email_template::EmailTemplate;
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 crate::models::app::{AppMessage, AppState};
/// Handler for email template-related HTTP requests.
pub struct EmailTemplateHandler;
impl EmailTemplateHandler {
/// Retrieves the details of a specific email template.
///
/// This handler allows email template admins to view template details.
///
/// # Arguments
///
/// * `transaction` - Database transaction
/// * `id` - The ID of the template to retrieve
/// * `_user` - The authenticated user (must be an email template admin)
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Template details or error
pub async fn get(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_user: EmailTemplateAdmin,
) -> Result<impl IntoResponse, ChaosError> {
let email_template = EmailTemplate::get(id, &mut transaction.tx).await?;
Ok((StatusCode::OK, Json(email_template)))
}
/// Updates an email template.
///
/// This handler allows email template admins to update template details.
///
/// # Arguments
///
/// * `_user` - The authenticated user (must be an email template admin)
/// * `id` - The ID of the template to update
/// * `state` - The application state
/// * `request_body` - The new template details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn update(
_user: EmailTemplateAdmin,
Path(id): Path<i64>,
mut transaction: DBTransaction<'_>,
Json(request_body): Json<EmailTemplate>,
) -> Result<impl IntoResponse, ChaosError> {
EmailTemplate::update(
id,
request_body.name,
request_body.template_subject,
request_body.template_body,
&mut transaction.tx,
)
.await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully updated email template"))
}
/// Deletes an email template.
///
/// This handler allows email template admins to delete templates.
///
/// # Arguments
///
/// * `_user` - The authenticated user (must be an email template admin)
/// * `id` - The ID of the template to delete
/// * `state` - The application state
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn delete(
_user: EmailTemplateAdmin,
Path(id): Path<i64>,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
EmailTemplate::delete(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully deleted email template"))
}
/// Duplicates an email template.
///
/// This handler allows email template admins to duplicate templates.
///
/// # Arguments
///
/// * `_user` - The authenticated user (must be an email template admin)
/// * `id` - The ID of the template to delete
/// * `state` - The application state
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn duplicate(
_user: EmailTemplateAdmin,
Path(id): Path<i64>,
State(mut state): State<AppState>,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
EmailTemplate::duplicate(id, &mut transaction.tx, &mut state.snowflake_generator).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully duplicated email template"))
}
}