Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Test Suite (pull_request) Successful in 1m10s
Rust Build / Clippy (pull_request) Successful in 1m42s
Rust Build / Check (pull_request) Successful in 2m33s
Rust Build / build (pull_request) Successful in 3m20s
114 lines
3.8 KiB
Rust
114 lines
3.8 KiB
Rust
pub mod endpoints {
|
|
pub const ROOT: &str = "/";
|
|
|
|
/// Constant for adding Contact endpoint
|
|
pub const ADD_CONTACT: &str = "/api/v1/contact/new";
|
|
}
|
|
|
|
pub mod response {
|
|
pub const SUCCESSFUL: &str = "SUCCESSFUL";
|
|
}
|
|
|
|
/// Basic handler that responds with a static string
|
|
pub async fn root() -> &'static str {
|
|
"Hello, World!"
|
|
}
|
|
|
|
pub mod contact {
|
|
pub mod request {
|
|
use serde::{Deserialize, Serialize};
|
|
use utoipa::ToSchema;
|
|
|
|
#[derive(Debug, Deserialize, Serialize, ToSchema)]
|
|
pub struct AddContactRequest {
|
|
pub phone_number: String,
|
|
pub firstname: Option<String>,
|
|
pub lastname: Option<String>,
|
|
pub nickname: Option<String>,
|
|
pub user_id: uuid::Uuid,
|
|
}
|
|
|
|
impl AddContactRequest {
|
|
pub fn is_valid(&self) -> bool {
|
|
self.phone_number.is_empty()
|
|
|| self.firstname.is_none()
|
|
|| self.lastname.is_none()
|
|
|| self.user_id.is_nil()
|
|
}
|
|
}
|
|
}
|
|
|
|
pub mod response {
|
|
use serde::{Deserialize, Serialize};
|
|
use utoipa::ToSchema;
|
|
|
|
#[derive(Debug, Default, Deserialize, Serialize, ToSchema)]
|
|
pub struct AddContactResponse {
|
|
pub message: String,
|
|
// TODO: Should be this. Update code in textsender_models
|
|
// pub data: Vec<textsender_models::contact::Contact>,
|
|
pub data: Vec<uuid::Uuid>,
|
|
}
|
|
}
|
|
|
|
pub mod endpoint {
|
|
// use axum::{Json, response::IntoResponse};
|
|
|
|
// use crate::repo;
|
|
// use crate::repo::contact as contact_repo;
|
|
|
|
/// Endpoint to create Contact
|
|
#[utoipa::path(
|
|
post,
|
|
path = super::super::endpoints::ADD_CONTACT,
|
|
request_body(
|
|
content = super::request::AddContactRequest,
|
|
description = "Data needed to create a Contact",
|
|
content_type = "application/json"
|
|
),
|
|
responses(
|
|
(status = 201, description = "Contact created", body = super::response::AddContactResponse),
|
|
(status = 400, description = "Error", body = super::response::AddContactResponse),
|
|
(status = 500, description = "Error creating Contact", body = super::response::AddContactResponse)
|
|
)
|
|
)]
|
|
pub async fn create_contact(
|
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
|
axum::Json(payload): axum::Json<super::request::AddContactRequest>,
|
|
) -> (
|
|
axum::http::StatusCode,
|
|
axum::Json<super::response::AddContactResponse>,
|
|
) {
|
|
let mut response = super::response::AddContactResponse::default();
|
|
|
|
if payload.is_valid() {
|
|
let contact = textsender_models::contact::Contact {
|
|
firstname: payload.firstname,
|
|
lastname: payload.lastname,
|
|
phone_number: payload.phone_number,
|
|
user_id: Some(payload.user_id),
|
|
..Default::default()
|
|
};
|
|
match crate::repo::contact::insert(&pool, &contact).await {
|
|
Ok(_) => (
|
|
axum::http::StatusCode::NOT_IMPLEMENTED,
|
|
axum::Json(response),
|
|
),
|
|
Err(err) => {
|
|
eprintln!("Error: {err:?}");
|
|
response.message = String::from("Contact not created");
|
|
|
|
(
|
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
|
axum::Json(response),
|
|
)
|
|
}
|
|
}
|
|
} else {
|
|
response.message = String::from("Request body is not valid");
|
|
(axum::http::StatusCode::BAD_REQUEST, axum::Json(response))
|
|
}
|
|
}
|
|
}
|
|
}
|