#![allow(unused)]
use actix_web::{HttpResponse, http::StatusCode};
use serde::Serialize;
#[derive(Serialize)]
pub struct ApiResponse<T>
where
T: Serialize,
{
success: bool,
message: String,
#[serde(skip_serializing_if = "Option::is_none")]
data: Option<T>,
#[serde(skip_serializing_if = "Option::is_none")]
pagination: Option<serde_json::Value>,
}
impl<T: Serialize> ApiResponse<T> {
fn base(success: bool, message: &str, data: Option<T>) -> Self {
Self {
success,
message: message.to_string(),
data,
pagination: None,
}
}
pub fn ok(message: &str, data: T) -> HttpResponse {
HttpResponse::Ok().json(Self::base(true, message, Some(data)))
}
pub fn _ok_paginated(message: &str, data: T, pagination: serde_json::Value) -> HttpResponse {
HttpResponse::Ok().json(Self {
success: true,
message: message.to_string(),
data: Some(data),
pagination: Some(pagination),
})
}
}
impl ApiResponse<()> {
pub fn error(message: &str, status: StatusCode) -> HttpResponse {
HttpResponse::build(status).json(Self {
success: false,
message: message.to_string(),
data: None,
pagination: None,
})
}
pub fn ok_empty(message: &str, status_code: StatusCode) -> HttpResponse {
HttpResponse::build(status_code).json(Self {
success: true,
message: message.to_string(),
data: None,
pagination: None,
})
}
}