|
@@ -0,0 +1,30 @@
|
|
|
|
|
+use actix_web::{HttpResponse, HttpRequest, web, post};
|
|
|
|
|
+use serde::Deserialize;
|
|
|
|
|
+use sqlx::PgPool;
|
|
|
|
|
+use uuid::Uuid;
|
|
|
|
|
+use crate::{
|
|
|
|
|
+ app_error::AppError,
|
|
|
|
|
+ auth::user_auth,
|
|
|
|
|
+ models::device::Device
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Deserialize)]
|
|
|
|
|
+struct Body{
|
|
|
|
|
+ name: String,
|
|
|
|
|
+ description: Option<String>
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[post("/home/{home_id}/device")]
|
|
|
|
|
+pub async fn route(
|
|
|
|
|
+ db: web::Data<PgPool>,
|
|
|
|
|
+ path: web::Path<String>,
|
|
|
|
|
+ body: web::Json<Body>,
|
|
|
|
|
+ req: HttpRequest
|
|
|
|
|
+) -> Result<HttpResponse, AppError> {
|
|
|
|
|
+ let user = user_auth(&db, &req).await?;
|
|
|
|
|
+ let home_id = Uuid::parse_str(&path.into_inner())?;
|
|
|
|
|
+ let body = body.into_inner();
|
|
|
|
|
+ let device = Device::new(home_id, body.name, body.description);
|
|
|
|
|
+ device.insert(&db, user.id, home_id).await?;
|
|
|
|
|
+ Ok(HttpResponse::Ok().json(device))
|
|
|
|
|
+}
|