Преглед изворни кода

Create test route for creating user

Lee Morgan пре 1 месец
родитељ
комит
04274b0f5a
9 измењених фајлова са 41 додато и 2 уклоњено
  1. 1 0
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 1 0
      src/controllers/mod.rs
  4. 20 0
      src/controllers/user/create.rs
  5. 1 0
      src/controllers/user/mod.rs
  6. 1 0
      src/http_error.rs
  7. 7 1
      src/main.rs
  8. 1 0
      src/routes/mod.rs
  9. 8 0
      src/routes/user.rs

+ 1 - 0
Cargo.lock

@@ -1072,6 +1072,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
 dependencies = [
  "serde_core",
+ "serde_derive",
 ]
 
 [[package]]

+ 1 - 1
Cargo.toml

@@ -7,5 +7,5 @@ edition = "2024"
 actix-cors = "0.7.1"
 actix-web = "4.13.0"
 dotenvy = "0.15.7"
-serde = "1.0.228"
+serde = { version = "1.0.228", features = ["derive"]}
 thiserror = "2.0.18"

+ 1 - 0
src/controllers/mod.rs

@@ -0,0 +1 @@
+pub mod user;

+ 20 - 0
src/controllers/user/create.rs

@@ -0,0 +1,20 @@
+use actix_web::{HttpResponse, web, post};
+use serde::Deserialize;
+use crate::{
+    http_error::HttpError
+};
+
+#[derive(Deserialize)]
+struct Body {
+    name: String,
+    email: String,
+    password: String,
+    confirm_password: String
+}
+
+#[post("/user")]
+pub async fn route(
+    body: web::Json<Body>
+) -> Result<HttpResponse, HttpError> {
+    Ok(HttpResponse::Ok().body("Test"))
+}

+ 1 - 0
src/controllers/user/mod.rs

@@ -0,0 +1 @@
+pub mod create;

+ 1 - 0
src/http_error.rs

@@ -1,5 +1,6 @@
 use actix_web::{HttpResponse, ResponseError, http::StatusCode};
 use thiserror::Error;
+use serde::Serialize;
 
 #[derive(Serialize)]
 struct ErrorBody {

+ 7 - 1
src/main.rs

@@ -1,5 +1,11 @@
 use actix_web::{HttpServer, web, App, middleware};
 use actix_cors::Cors;
+use http_error::HttpError;
+use std::env;
+
+mod http_error;
+mod routes;
+mod controllers;
 
 #[actix_web::main]
 async fn main() -> std::io::Result<()> {
@@ -32,7 +38,7 @@ async fn main() -> std::io::Result<()> {
             .wrap(cors)
             .app_data(
                 web::JsonConfig::default().error_handler(|err, _req| {
-                    AppError::JsonDeserializationError(err.to_string()).into()
+                    HttpError::JsonDeserializationError(err.to_string()).into()
                 })
             )
             .configure(routes::user::config)

+ 1 - 0
src/routes/mod.rs

@@ -0,0 +1 @@
+pub mod user;

+ 8 - 0
src/routes/user.rs

@@ -0,0 +1,8 @@
+use actix_web::web;
+use crate::controllers::user::{
+    create
+};
+
+pub fn config(cfg: &mut web::ServiceConfig) {
+    cfg.service(create::route);
+}