zino_model/dataset/
mod.rs1use crate::{project::Project, task::Task};
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 Dataset {
31 #[schema(read_only)]
33 id: Uuid,
34 #[schema(not_null)]
35 name: String,
36 #[cfg(feature = "namespace")]
37 #[schema(default_value = "Dataset::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 = "Project")]
48 project_id: Uuid, #[schema(reference = "Task")]
50 task_id: Option<Uuid>, valid_from: DateTime,
52 expires_at: DateTime,
53 #[cfg(feature = "tags")]
54 #[schema(reference = "Tag", index_type = "gin")]
55 tags: Vec<Uuid>, extra: Map,
59
60 #[cfg(feature = "owner-id")]
62 #[schema(reference = "User")]
63 owner_id: Option<Uuid>, #[cfg(feature = "maintainer-id")]
65 #[schema(reference = "User")]
66 maintainer_id: Option<Uuid>, #[schema(read_only, default_value = "now", index_type = "btree")]
68 created_at: DateTime,
69 #[schema(default_value = "now", index_type = "btree")]
70 updated_at: DateTime,
71 version: u64,
72 #[cfg(feature = "edition")]
73 edition: u32,
74}
75
76impl Model for Dataset {
77 const MODEL_NAME: &'static str = "dataset";
78
79 #[inline]
80 fn new() -> Self {
81 Self {
82 id: Uuid::now_v7(),
83 ..Self::default()
84 }
85 }
86
87 fn read_map(&mut self, data: &Map) -> Validation {
88 let mut validation = Validation::new();
89 if let Some(result) = data.parse_uuid("id") {
90 match result {
91 Ok(id) => self.id = id,
92 Err(err) => validation.record_fail("id", err),
93 }
94 }
95 if let Some(name) = data.parse_string("name") {
96 self.name = name.into_owned();
97 }
98 if let Some(description) = data.parse_string("description") {
99 self.description = description.into_owned();
100 }
101 #[cfg(feature = "tags")]
102 if let Some(result) = data.parse_array("tags") {
103 match result {
104 Ok(tags) => self.tags = tags,
105 Err(err) => validation.record_fail("tags", err),
106 }
107 }
108 #[cfg(feature = "owner-id")]
109 if let Some(result) = data.parse_uuid("owner_id") {
110 match result {
111 Ok(owner_id) => self.owner_id = Some(owner_id),
112 Err(err) => validation.record_fail("owner_id", err),
113 }
114 }
115 #[cfg(feature = "maintainer-id")]
116 if let Some(result) = data.parse_uuid("maintainer_id") {
117 match result {
118 Ok(maintainer_id) => self.maintainer_id = Some(maintainer_id),
119 Err(err) => validation.record_fail("maintainer_id", err),
120 }
121 }
122 validation
123 }
124}
125
126impl ModelHooks for Dataset {
127 type Data = ();
128 #[cfg(feature = "maintainer-id")]
129 type Extension = UserSession<Uuid, String>;
130 #[cfg(not(feature = "maintainer-id"))]
131 type Extension = ();
132
133 #[cfg(feature = "maintainer-id")]
134 #[inline]
135 async fn after_extract(&mut self, session: Self::Extension) -> Result<(), Error> {
136 self.maintainer_id = Some(*session.user_id());
137 Ok(())
138 }
139
140 #[cfg(feature = "maintainer-id")]
141 #[inline]
142 async fn before_validation(
143 data: &mut Map,
144 extension: Option<&Self::Extension>,
145 ) -> Result<(), Error> {
146 if let Some(session) = extension {
147 data.upsert("maintainer_id", session.user_id().to_string());
148 }
149 Ok(())
150 }
151}