GeneIT/geneit_backend/src/controllers/families_controller.rs

25 lines
710 B
Rust
Raw Normal View History

2023-06-16 15:51:51 +00:00
use crate::constants::StaticConstraints;
use crate::controllers::HttpResult;
use crate::services::families_service;
use crate::services::login_token_service::LoginToken;
use actix_web::{web, HttpResponse};
#[derive(Debug, serde::Deserialize)]
pub struct CreateFamilyReq {
name: String,
}
/// Create a new family
pub async fn create(req: web::Json<CreateFamilyReq>, token: LoginToken) -> HttpResult {
if !StaticConstraints::default()
.family_name_len
.validate(&req.name)
{
return Ok(HttpResponse::BadRequest().body("Invalid family name!"));
}
let family = families_service::create(&req.name, token.user_id).await?;
Ok(HttpResponse::Created().json(family))
}