You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. use actix_web::{web, HttpResponse};
  2. use crate::{
  3. actions,
  4. models::{self, ApiResponse, Pagination, UserId, UserSearchRequest},
  5. };
  6. use crate::{errors::DomainError, AppData};
  7. use actix_web::error::ResponseError;
  8. /// Finds user by UID.
  9. #[tracing::instrument(
  10. level = "debug",
  11. skip(app_data),
  12. fields(
  13. user_id = %user_id.0
  14. )
  15. )]
  16. pub async fn get_user(
  17. app_data: web::Data<AppData>,
  18. user_id: web::Path<UserId>,
  19. ) -> Result<HttpResponse, DomainError> {
  20. let u_id = user_id.into_inner();
  21. let u_id2 = u_id.clone();
  22. let _ = tracing::info!("Getting user with id {}", u_id);
  23. // use web::block to offload blocking Diesel code without blocking server thread
  24. let res = web::block(move || {
  25. let pool = &app_data.pool;
  26. let conn = pool.get()?;
  27. actions::find_user_by_uid(&u_id2, &conn)
  28. })
  29. .await
  30. .map_err(|err| DomainError::new_thread_pool_error(err.to_string()))?;
  31. let _ = tracing::trace!("{:?}", res);
  32. if let Some(user) = res {
  33. Ok(HttpResponse::Ok().json(ApiResponse::successful(user)))
  34. } else {
  35. let err = DomainError::new_entity_does_not_exist_error(format!(
  36. "No user found with uid: {}",
  37. u_id
  38. ));
  39. Err(err)
  40. }
  41. }
  42. // #[get("/get/users/{user_id}")]
  43. // pub async fn get_user2(
  44. // user_service: web::Data<dyn UserService>,
  45. // user_id: web::Path<i32>,
  46. // ) -> Result<HttpResponse, DomainError> {
  47. // let u_id = user_id.into_inner();
  48. // let user = user_service.find_user_by_uid(u_id)?;
  49. // if let Some(user) = user {
  50. // Ok(HttpResponse::Ok().json(user))
  51. // } else {
  52. // let err = DomainError::new_entity_does_not_exist_error(format!(
  53. // "No user found with uid: {}",
  54. // u_id
  55. // ));
  56. // Err(err)
  57. // }
  58. // }
  59. ///List all users
  60. #[tracing::instrument(level = "debug", skip(app_data))]
  61. pub async fn get_all_users(
  62. app_data: web::Data<AppData>,
  63. ) -> Result<HttpResponse, DomainError> {
  64. // use web::block to offload blocking Diesel code without blocking server thread
  65. let users = web::block(move || {
  66. let pool = &app_data.pool;
  67. let conn = pool.get()?;
  68. actions::get_all(&conn)
  69. })
  70. .await
  71. .map_err(|err| DomainError::new_thread_pool_error(err.to_string()))?;
  72. let _ = tracing::trace!("{:?}", users);
  73. if !users.is_empty() {
  74. Ok(HttpResponse::Ok().json(ApiResponse::successful(users)))
  75. } else {
  76. Err(DomainError::new_entity_does_not_exist_error(
  77. "No users available".to_owned(),
  78. ))
  79. }
  80. }
  81. #[tracing::instrument(level = "debug", skip(app_data))]
  82. pub async fn get_users_paginated(
  83. app_data: web::Data<AppData>,
  84. pagination: web::Query<Pagination>,
  85. ) -> Result<HttpResponse, DomainError> {
  86. let _ = tracing::info!("Paginated users request");
  87. let users = web::block(move || {
  88. let pool = &app_data.pool;
  89. let conn = pool.get()?;
  90. let p: Pagination = pagination.into_inner();
  91. actions::get_users_paginated(&p, &conn)
  92. })
  93. .await
  94. .map_err(|err| DomainError::new_thread_pool_error(err.to_string()))?;
  95. let _ = tracing::trace!("{:?}", users);
  96. Ok(HttpResponse::Ok().json(ApiResponse::successful(users)))
  97. }
  98. #[tracing::instrument(level = "debug", skip(app_data))]
  99. pub async fn search_users(
  100. app_data: web::Data<AppData>,
  101. query: web::Query<UserSearchRequest>,
  102. pagination: web::Query<Pagination>,
  103. ) -> Result<HttpResponse, DomainError> {
  104. let _ = tracing::info!("Search users request");
  105. let users = web::block(move || {
  106. let pool = &app_data.pool;
  107. let conn = pool.get()?;
  108. let p: Pagination = pagination.into_inner();
  109. actions::search_users(query.q.as_str(), &p, &conn)
  110. })
  111. .await
  112. .map_err(|err| DomainError::new_thread_pool_error(err.to_string()))?;
  113. let _ = tracing::trace!("{:?}", users);
  114. Ok(HttpResponse::Ok().json(ApiResponse::successful(users)))
  115. }
  116. /// Inserts new user with name defined in form.
  117. #[tracing::instrument(level = "debug", skip(app_data))]
  118. pub async fn add_user(
  119. app_data: web::Data<AppData>,
  120. form: web::Json<models::NewUser>,
  121. ) -> Result<HttpResponse, impl ResponseError> {
  122. // use web::block to offload blocking Diesel code without blocking server thread
  123. web::block(move || {
  124. let pool = &app_data.pool;
  125. let conn = pool.get()?;
  126. actions::insert_new_user(form.0, &conn, Some(app_data.config.hash_cost))
  127. })
  128. .await
  129. .map(|user| {
  130. let _ = tracing::trace!("{:?}", user);
  131. HttpResponse::Created().json(ApiResponse::successful(user))
  132. })
  133. }