use crate::error::HttpError;
use axum::http::StatusCode;
use docbox_core::{database::models::folder::FolderId, links::create_link::CreateLinkError};
use garde::Validate;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use utoipa::ToSchema;
#[derive(Debug, Validate, Deserialize, ToSchema)]
pub struct CreateLink {
#[garde(length(min = 1, max = 255))]
#[schema(min_length = 1, max_length = 255)]
pub name: String,
#[garde(length(min = 1))]
#[schema(min_length = 1)]
pub value: String,
#[garde(skip)]
#[schema(value_type = Uuid)]
pub folder_id: FolderId,
}
#[derive(Debug, Validate, Deserialize, ToSchema)]
pub struct UpdateLinkRequest {
#[garde(inner(length(min = 1, max = 255)))]
#[schema(min_length = 1, max_length = 255)]
pub name: Option<String>,
#[garde(inner(length(min = 1)))]
#[schema(min_length = 1)]
pub value: Option<String>,
#[garde(skip)]
#[schema(value_type = Option<Uuid>)]
pub folder_id: Option<FolderId>,
#[garde(skip)]
#[schema(value_type = Option<bool>)]
pub pinned: Option<bool>,
}
#[derive(Debug, Serialize, ToSchema)]
pub struct LinkMetadataResponse {
pub title: Option<String>,
pub og_title: Option<String>,
pub og_description: Option<String>,
pub favicon: bool,
pub image: bool,
}
#[derive(Debug, Error)]
pub enum HttpLinkError {
#[error("unknown link")]
UnknownLink,
#[error("invalid link url")]
InvalidLinkUrl,
#[error(transparent)]
CreateError(CreateLinkError),
#[error("failed to resolve metadata")]
FailedResolve,
#[error("website favicon not present")]
NoFavicon,
#[error("website image not present")]
NoImage,
}
impl HttpError for HttpLinkError {
fn status(&self) -> axum::http::StatusCode {
match self {
HttpLinkError::UnknownLink
| HttpLinkError::NoFavicon
| HttpLinkError::NoImage
| HttpLinkError::FailedResolve => StatusCode::NOT_FOUND,
HttpLinkError::InvalidLinkUrl => StatusCode::BAD_REQUEST,
HttpLinkError::CreateError(_) => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}