|
| 1 | +use crate::{models::{ |
| 2 | + activities::{Activity, ActivityStatus}, groups::GroupPermission, response::{ErrorResponse, ResponseStatus, SuccessResponse} |
| 3 | +}, utils::jwt::UserData}; |
| 4 | +use axum::{ |
| 5 | + extract::{Extension, Path}, |
| 6 | + http::StatusCode, |
| 7 | + response::{IntoResponse, Json}, |
| 8 | +}; |
| 9 | +use bson::{doc, oid::ObjectId}; |
| 10 | +use mongodb::{Database, Collection}; |
| 11 | +use std::sync::Arc; |
| 12 | +use tokio::sync::Mutex; |
| 13 | +use serde::{Deserialize, Serialize}; |
| 14 | + |
| 15 | +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] |
| 16 | +pub struct UpdateActivityName { |
| 17 | + pub name: String, |
| 18 | +} |
| 19 | + |
| 20 | +pub async fn update_activity_name( |
| 21 | + Extension(db): Extension<Arc<Mutex<Database>>>, |
| 22 | + user: UserData, |
| 23 | + Path(id): Path<String>, |
| 24 | + Json(data): Json<UpdateActivityName>, |
| 25 | +) -> impl IntoResponse { |
| 26 | + let db = db.lock().await; |
| 27 | + let collection: Collection<Activity> = db.collection("activities"); |
| 28 | + let id = ObjectId::parse_str(&id); |
| 29 | + if let Err(_) = id { |
| 30 | + let response = ErrorResponse { |
| 31 | + status: ResponseStatus::Error, |
| 32 | + code: 400, |
| 33 | + message: "Invalid activity id".to_string(), |
| 34 | + }; |
| 35 | + let response = serde_json::to_string(&response).unwrap(); |
| 36 | + return (StatusCode::BAD_REQUEST, Json(response)); |
| 37 | + } |
| 38 | + let id = id.unwrap(); |
| 39 | + let activity = collection.find_one(doc! {"_id": id}, None).await; |
| 40 | + if let Err(e) = activity { |
| 41 | + let response = ErrorResponse { |
| 42 | + status: ResponseStatus::Error, |
| 43 | + code: 500, |
| 44 | + message: "Failed to find activity: ".to_string() + &e.to_string(), |
| 45 | + }; |
| 46 | + let response = serde_json::to_string(&response).unwrap(); |
| 47 | + return (StatusCode::INTERNAL_SERVER_ERROR, Json(response)); |
| 48 | + } |
| 49 | + let activity = activity.unwrap(); |
| 50 | + if let None = activity { |
| 51 | + let response = ErrorResponse { |
| 52 | + status: ResponseStatus::Error, |
| 53 | + code: 404, |
| 54 | + message: "Activity not found".to_string(), |
| 55 | + }; |
| 56 | + let response = serde_json::to_string(&response).unwrap(); |
| 57 | + return (StatusCode::NOT_FOUND, Json(response)); |
| 58 | + } |
| 59 | + let activity = activity.unwrap(); |
| 60 | + let creator = activity.creator; |
| 61 | + if user.perms.contains(&GroupPermission::Admin) || user.perms.contains(&GroupPermission::Department) || id == creator { |
| 62 | + let result = collection.update_one(doc! {"_id": id}, doc! {"$set": {"name": data.name}}, None).await; |
| 63 | + if let Err(e) = result { |
| 64 | + let response = ErrorResponse { |
| 65 | + status: ResponseStatus::Error, |
| 66 | + code: 500, |
| 67 | + message: "Failed to update activity: ".to_string() + &e.to_string(), |
| 68 | + }; |
| 69 | + let response = serde_json::to_string(&response).unwrap(); |
| 70 | + return (StatusCode::INTERNAL_SERVER_ERROR, Json(response)); |
| 71 | + } |
| 72 | + let response: SuccessResponse<Vec<Activity>, ()> = SuccessResponse { |
| 73 | + status: ResponseStatus::Success, |
| 74 | + code: 200, |
| 75 | + data: vec![], |
| 76 | + metadata: None, |
| 77 | + }; |
| 78 | + let response = serde_json::to_string(&response).unwrap(); |
| 79 | + return (StatusCode::OK, Json(response)); |
| 80 | + } else { |
| 81 | + let response = ErrorResponse { |
| 82 | + status: ResponseStatus::Error, |
| 83 | + code: 403, |
| 84 | + message: "Permission denied".to_string(), |
| 85 | + }; |
| 86 | + let response = serde_json::to_string(&response).unwrap(); |
| 87 | + return (StatusCode::FORBIDDEN, Json(response)); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] |
| 92 | +pub struct UpdateActivityDescription { |
| 93 | + pub description: String, |
| 94 | +} |
| 95 | + |
| 96 | +pub async fn update_activity_description( |
| 97 | + Extension(db): Extension<Arc<Mutex<Database>>>, |
| 98 | + user: UserData, |
| 99 | + Path(id): Path<String>, |
| 100 | + Json(data): Json<UpdateActivityDescription>, |
| 101 | +) -> impl IntoResponse { |
| 102 | + let db = db.lock().await; |
| 103 | + let collection: Collection<Activity> = db.collection("activities"); |
| 104 | + let id = ObjectId::parse_str(&id); |
| 105 | + if let Err(_) = id { |
| 106 | + let response = ErrorResponse { |
| 107 | + status: ResponseStatus::Error, |
| 108 | + code: 400, |
| 109 | + message: "Invalid activity id".to_string(), |
| 110 | + }; |
| 111 | + let response = serde_json::to_string(&response).unwrap(); |
| 112 | + return (StatusCode::BAD_REQUEST, Json(response)); |
| 113 | + } |
| 114 | + let id = id.unwrap(); |
| 115 | + let activity = collection.find_one(doc! {"_id": id}, None).await; |
| 116 | + if let Err(e) = activity { |
| 117 | + let response = ErrorResponse { |
| 118 | + status: ResponseStatus::Error, |
| 119 | + code: 500, |
| 120 | + message: "Failed to find activity: ".to_string() + &e.to_string(), |
| 121 | + }; |
| 122 | + let response = serde_json::to_string(&response).unwrap(); |
| 123 | + return (StatusCode::INTERNAL_SERVER_ERROR, Json(response)); |
| 124 | + } |
| 125 | + let activity = activity.unwrap(); |
| 126 | + if let None = activity { |
| 127 | + let response = ErrorResponse { |
| 128 | + status: ResponseStatus::Error, |
| 129 | + code: 404, |
| 130 | + message: "Activity not found".to_string(), |
| 131 | + }; |
| 132 | + let response = serde_json::to_string(&response).unwrap(); |
| 133 | + return (StatusCode::NOT_FOUND, Json(response)); |
| 134 | + } |
| 135 | + let activity = activity.unwrap(); |
| 136 | + let creator = activity.creator; |
| 137 | + if user.perms.contains(&GroupPermission::Admin) || user.perms.contains(&GroupPermission::Department) || id == creator { |
| 138 | + let result = collection.update_one(doc! {"_id": id}, doc! {"$set": {"description": data.description}}, None).await; |
| 139 | + if let Err(e) = result { |
| 140 | + let response = ErrorResponse { |
| 141 | + status: ResponseStatus::Error, |
| 142 | + code: 500, |
| 143 | + message: "Failed to update activity: ".to_string() + &e.to_string(), |
| 144 | + }; |
| 145 | + let response = serde_json::to_string(&response).unwrap(); |
| 146 | + return (StatusCode::INTERNAL_SERVER_ERROR, Json(response)); |
| 147 | + } |
| 148 | + let response: SuccessResponse<Vec<Activity>, ()> = SuccessResponse { |
| 149 | + status: ResponseStatus::Success, |
| 150 | + code: 200, |
| 151 | + data: vec![], |
| 152 | + metadata: None, |
| 153 | + }; |
| 154 | + let response = serde_json::to_string(&response).unwrap(); |
| 155 | + return (StatusCode::OK, Json(response)); |
| 156 | + } else { |
| 157 | + let response = ErrorResponse { |
| 158 | + status: ResponseStatus::Error, |
| 159 | + code: 403, |
| 160 | + message: "Permission denied".to_string(), |
| 161 | + }; |
| 162 | + let response = serde_json::to_string(&response).unwrap(); |
| 163 | + return (StatusCode::FORBIDDEN, Json(response)); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] |
| 168 | +pub struct UpdateActivityStatus { |
| 169 | + status: ActivityStatus, |
| 170 | +} |
0 commit comments