docbox_database/models/
shared.rs1use ::sqlx::{
2 Postgres,
3 encode::{Encode, IsNull},
4 postgres::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo},
5};
6use serde::{Deserialize, Serialize};
7use sqlx::{postgres::types::PgRecordEncoder, prelude::FromRow};
8use utoipa::ToSchema;
9use uuid::Uuid;
10
11use crate::models::{
12 document_box::DocumentBoxScopeRaw,
13 folder::{Folder, FolderId},
14};
15
16#[derive(Debug, FromRow)]
17pub struct TotalSizeResult {
18 pub total_size: i64,
19}
20
21#[derive(Debug, FromRow)]
22pub struct CountResult {
23 pub count: i64,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, PartialEq, Eq, FromRow, sqlx::Type)]
27#[sqlx(type_name = "docbox_path_segment")]
28pub struct FolderPathSegment {
29 #[schema(value_type = Uuid)]
30 pub id: FolderId,
31 pub name: String,
32}
33
34impl FolderPathSegment {
35 pub fn new(id: FolderId, name: impl Into<String>) -> Self {
36 Self {
37 id,
38 name: name.into(),
39 }
40 }
41}
42
43impl<'a> From<&'a Folder> for FolderPathSegment {
44 fn from(value: &'a Folder) -> Self {
45 Self::new(value.id, &value.name)
46 }
47}
48
49pub struct DocboxInputPair<'a> {
50 pub scope: &'a str,
51 pub id: Uuid,
52}
53
54impl<'a> Encode<'_, Postgres> for DocboxInputPair<'a> {
55 fn encode_by_ref(
56 &self,
57 buf: &mut PgArgumentBuffer,
58 ) -> Result<IsNull, sqlx::error::BoxDynError> {
59 let mut encoder = PgRecordEncoder::new(buf);
60 encoder.encode(self.scope)?;
61 encoder.encode(self.id)?;
62 encoder.finish();
63 Ok(IsNull::No)
64 }
65
66 fn size_hint(&self) -> ::std::primitive::usize {
67 2usize * (4 + 4)
68 + <&'a str as Encode<Postgres>>::size_hint(&self.scope)
69 + <Uuid as Encode<Postgres>>::size_hint(&self.id)
70 }
71}
72
73impl sqlx::Type<Postgres> for DocboxInputPair<'_> {
74 fn type_info() -> PgTypeInfo {
75 PgTypeInfo::with_name("docbox_input_pair")
76 }
77}
78
79impl PgHasArrayType for DocboxInputPair<'_> {
80 fn array_type_info() -> PgTypeInfo {
81 PgTypeInfo::array_of("docbox_input_pair")
82 }
83}
84
85impl<'a> DocboxInputPair<'a> {
86 pub fn new(scope: &'a str, id: Uuid) -> Self {
87 Self { scope, id }
88 }
89}
90
91#[derive(Debug, Clone, FromRow, Serialize, Deserialize, ToSchema)]
92pub struct WithFullPathScope<T> {
93 #[serde(flatten)]
94 #[sqlx(flatten)]
95 pub data: T,
96 pub full_path: Vec<FolderPathSegment>,
97 pub document_box: DocumentBoxScopeRaw,
98}
99
100#[derive(Debug, Clone, FromRow, Serialize, Deserialize, ToSchema)]
101pub struct WithFullPath<T> {
102 #[serde(flatten)]
103 #[sqlx(flatten)]
104 pub data: T,
105 pub full_path: Vec<FolderPathSegment>,
106}