zino_model/collection/
mod.rs1use crate::{group::Group, source::Source};
4use serde::{Deserialize, Serialize};
5use zino_core::{
6 Map, Uuid,
7 datetime::DateTime,
8 error::Error,
9 extension::JsonObjectExt,
10 model::{Model, ModelHooks},
11 validation::Validation,
12};
13use zino_derive::{DecodeRow, Entity, ModelAccessor, Schema};
14
15#[cfg(feature = "tags")]
16use crate::tag::Tag;
17
18#[cfg(any(feature = "owner-id", feature = "maintainer-id"))]
19use crate::user::User;
20
21#[cfg(feature = "maintainer-id")]
22use zino_auth::UserSession;
23
24#[derive(
26 Debug, Clone, Default, Serialize, Deserialize, DecodeRow, Entity, Schema, ModelAccessor,
27)]
28#[serde(default)]
29#[schema(auto_rename)]
30pub struct Collection {
31 #[schema(read_only)]
33 id: Uuid,
34 #[schema(not_null)]
35 name: String,
36 #[cfg(feature = "namespace")]
37 #[schema(default_value = "Collection::model_namespace", index_type = "hash")]
38 namespace: String,
39 #[cfg(feature = "visibility")]
40 #[schema(default_value = "Internal")]
41 visibility: String,
42 #[schema(default_value = "Active", index_type = "hash")]
43 status: String,
44 description: String,
45
46 #[schema(reference = "Group")]
48 consumer_id: Option<Uuid>, #[schema(reference = "Source")]
50 source_id: Uuid, #[cfg(feature = "tags")]
52 #[schema(reference = "Tag", index_type = "gin")]
53 tags: Vec<Uuid>, extra: Map,
57
58 #[cfg(feature = "owner-id")]
60 #[schema(reference = "User")]
61 owner_id: Option<Uuid>, #[cfg(feature = "maintainer-id")]
63 #[schema(reference = "User")]
64 maintainer_id: Option<Uuid>, #[schema(read_only, default_value = "now", index_type = "btree")]
66 created_at: DateTime,
67 #[schema(default_value = "now", index_type = "btree")]
68 updated_at: DateTime,
69 version: u64,
70 #[cfg(feature = "edition")]
71 edition: u32,
72}
73
74impl Model for Collection {
75 const MODEL_NAME: &'static str = "collection";
76
77 #[inline]
78 fn new() -> Self {
79 Self {
80 id: Uuid::now_v7(),
81 ..Self::default()
82 }
83 }
84
85 fn read_map(&mut self, data: &Map) -> Validation {
86 let mut validation = Validation::new();
87 if let Some(result) = data.parse_uuid("id") {
88 match result {
89 Ok(id) => self.id = id,
90 Err(err) => validation.record_fail("id", err),
91 }
92 }
93 if let Some(name) = data.parse_string("name") {
94 self.name = name.into_owned();
95 }
96 if let Some(description) = data.parse_string("description") {
97 self.description = description.into_owned();
98 }
99 #[cfg(feature = "tags")]
100 if let Some(result) = data.parse_array("tags") {
101 match result {
102 Ok(tags) => self.tags = tags,
103 Err(err) => validation.record_fail("tags", err),
104 }
105 }
106 #[cfg(feature = "owner-id")]
107 if let Some(result) = data.parse_uuid("owner_id") {
108 match result {
109 Ok(owner_id) => self.owner_id = Some(owner_id),
110 Err(err) => validation.record_fail("owner_id", err),
111 }
112 }
113 #[cfg(feature = "maintainer-id")]
114 if let Some(result) = data.parse_uuid("maintainer_id") {
115 match result {
116 Ok(maintainer_id) => self.maintainer_id = Some(maintainer_id),
117 Err(err) => validation.record_fail("maintainer_id", err),
118 }
119 }
120 validation
121 }
122}
123
124impl ModelHooks for Collection {
125 type Data = ();
126 #[cfg(feature = "maintainer-id")]
127 type Extension = UserSession<Uuid, String>;
128 #[cfg(not(feature = "maintainer-id"))]
129 type Extension = ();
130
131 #[cfg(feature = "maintainer-id")]
132 #[inline]
133 async fn after_extract(&mut self, session: Self::Extension) -> Result<(), Error> {
134 self.maintainer_id = Some(*session.user_id());
135 Ok(())
136 }
137
138 #[cfg(feature = "maintainer-id")]
139 #[inline]
140 async fn before_validation(
141 data: &mut Map,
142 extension: Option<&Self::Extension>,
143 ) -> Result<(), Error> {
144 if let Some(session) = extension {
145 data.upsert("maintainer_id", session.user_id().to_string());
146 }
147 Ok(())
148 }
149}