Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::sync::Arc;
- use axum::{
- extract::{Path, Query, State},
- http::StatusCode,
- response::IntoResponse,
- Json,
- };
- //use serde_json::json;
- use crate::{
- model::NoteModel,
- model::NoteModel2,
- schema::{FilterOptions},
- AppState,
- };
- pub async fn health_checker_handler() -> impl IntoResponse {
- const MESSAGE: &str = "Simple CRUD API with Rust, SQLX, Postgres,and Axum";
- let json_response = serde_json::json!({
- "status": "success",
- "message": MESSAGE
- });
- Json(json_response)
- }
- pub async fn note_list_handler(
- opts: Option<Query<FilterOptions>>,
- State(data): State<Arc<AppState>>,
- ) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
- let Query(opts) = opts.unwrap_or_default();
- let limit = opts.limit.unwrap_or(10);
- let offset = (opts.page.unwrap_or(1) - 1) * limit;
- let query_result = sqlx::query_as!(
- NoteModel,
- "SELECT c.id,c.idclave_nombre,o.fechaproxima_llamada,c.ubicacion,c.nombres,c.apellidos,c.cedula,c.sector,c.empresa,c.acreedor,c.analista,c.sucursal,o.motivo,c.ps,c.fecha_nacimiento,c.nivel,c.sexo,c.usuario,c.notas
- FROM tbl_clientes AS c
- LEFT JOIN tbl_observaciones AS o ON c.idclave_nombre = o.idclave_nombre
- WHERE o.fechaproxima_llamada IS NOT NULL
- AND o.fechaproxima_llamada != ''
- LIMIT $1 OFFSET $2",
- limit as i32,
- offset as i32
- )
- .fetch_all(&data.db)
- .await;
- if query_result.is_err() {
- let error_response = serde_json::json!({
- "status": "fail",
- "message": "Something bad happened while fetching all note items",
- });
- return Err((StatusCode::INTERNAL_SERVER_ERROR, Json(error_response)));
- }
- let notes = query_result.unwrap();
- let json_response = serde_json::json!({
- "status": "success",
- "results": notes.len(),
- "clientes": notes
- });
- Ok(Json(json_response))
- }
- pub async fn comments_list_handler(
- Path(id): Path<i64>,
- State(data): State<Arc<AppState>>,
- ) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
- let query_result = sqlx::query_as!(NoteModel2, "SELECT id,idclave_nombre,usuario,fecha_actualiza,motivo,observacion,fechaproxima_llamada
- FROM tbl_observaciones
- WHERE id = $1", id
- )
- .fetch_one(&data.db)
- .await;
- match query_result {
- Ok(note) => {
- // Record found, do something with the `note`
- Ok((StatusCode::OK, Json(note)))
- }
- Err(sqlx::Error::RowNotFound) => {
- // No record found
- Err((StatusCode::NOT_FOUND, Json("Record not found")))
- }
- Err(err) => {
- // Other error occurred
- eprintln!("Error fetching record: {:?}", err);
- Err((StatusCode::INTERNAL_SERVER_ERROR, Json("Internal server error")))
- }
- }
- }
Add Comment
Please, Sign In to add comment