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.

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