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.

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