summaryrefslogtreecommitdiffstats
path: root/rust/src/database_entity_crud.rs
blob: d532d166b0a963b120a7b808f19db5359610eb0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

use diesel::prelude::*;
use diesel::{self, insert_into};
use database_schema::*;
use database_models::*;
use errors::*;
use fatcat_api::models::*;
use api_helpers::{FatCatId, DbConn};
use uuid::Uuid;
use std::marker::Sized;
use std::str::FromStr;
use serde_json;

pub struct EditContext {
    pub editor_id: FatCatId,
    pub editgroup_id: FatCatId,
    pub extra_json: Option<serde_json::Value>,
}

/* One goal here is to abstract the non-entity-specific bits into generic traits or functions,
 * instead of macros.
 *
 * Notably:
 *
 *   db_get
 *   db_get_rev
 *   db_create
 *   db_create_batch
 *   db_update
 *   db_delete
 *   db_get_history
 *
 * For now, these will probably be macros, until we can level up our trait/generics foo.
 */

// Associated Type, not parametric
pub trait EntityCrud where Self: Sized {
    type EditRow; // EntityEditRow
    type IdentRow; // EntityIdentRow
    type RevRow;

    fn parse_editgroup_id(&self) -> Result<Option<FatCatId>>;

    // Generic Methods
    fn db_get(conn: &DbConn, ident: FatCatId) -> Result<Self>;
    fn db_get_rev(conn: &DbConn, rev_id: Uuid) -> Result<Self>;
    fn db_create(&self, conn: &DbConn, edit_context: &EditContext) -> Result<Self::EditRow>;
    fn db_create_batch(conn: &DbConn, edit_context: &EditContext, models: &[Self]) -> Result<Vec<Self::EditRow>>;
    fn db_update(&self, conn: &DbConn, edit_context: &EditContext, ident: FatCatId) -> Result<Self::EditRow>;
    fn db_delete(conn: &DbConn, edit_context: &EditContext, ident: FatCatId) -> Result<Self::EditRow>;
    fn db_get_history(conn: &DbConn, ident: FatCatId, limit: Option<i64>) -> Result<Vec<EntityHistoryEntry>>;

    // Entity-specific Methods
    fn db_from_row(conn: &DbConn, rev_row: Self::RevRow, ident_row: Option<Self::IdentRow>) -> Result<Self>;
    fn db_insert_rev(&self, conn: &DbConn, edit_context: &EditContext) -> Result<Uuid>;
}

// TODO: this could be a separate trait on all entities?
macro_rules! generic_parse_editgroup_id{
    () => {
        fn parse_editgroup_id(&self) -> Result<Option<FatCatId>> {
            match &self.editgroup_id {
                Some(s) => Ok(Some(FatCatId::from_str(&s)?)),
                None => Ok(None),
            }
        }
    }
}

macro_rules! generic_db_get {
    ($ident_table: ident, $rev_table: ident) => {
        fn db_get(conn: &DbConn, ident: FatCatId) -> Result<Self> {
            let (ident, rev): (Self::IdentRow, Self::RevRow) = $ident_table::table
                .find(ident.to_uuid())
                .inner_join($rev_table::table)
                .first(conn)?;

            Self::db_from_row(conn, rev, Some(ident))
        }
    }
}

macro_rules! generic_db_get_rev {
    ($rev_table: ident) => {
        fn db_get_rev(conn: &DbConn, rev_id: Uuid) -> Result<Self> {
            let rev = $rev_table::table
                .find(rev_id)
                .first(conn)?;
            
            Self::db_from_row(conn, rev, None)
        }
    }
}

macro_rules! generic_db_create_batch {
    () => {
        fn db_create_batch(conn: &DbConn, edit_context: &EditContext, models: &[Self]) -> Result<Vec<Self::EditRow>> {
            let mut ret: Vec<Self::EditRow> = vec![];
            for entity in models {
                ret.push(entity.db_create(conn, edit_context)?);
            }
            Ok(ret)
        }
    }
}

macro_rules! generic_db_delete {
    ($ident_table: ident, $edit_table:ident) => {
        fn db_delete(conn: &DbConn, edit_context: &EditContext, ident: FatCatId) -> Result<Self::EditRow> {

            let current: Self::IdentRow = $ident_table::table.find(ident.to_uuid()).first(conn)?;
            if current.is_live != true {
                // TODO: what if isn't live? 4xx not 5xx
                bail!("can't delete an entity that doesn't exist yet");
            }
            if current.rev_id.is_none() {
                // TODO: what if it's already deleted? 4xx not 5xx
                bail!("entity was already deleted");
            }
            let edit: Self::EditRow = insert_into($edit_table::table)
                .values((
                    $edit_table::editgroup_id.eq(edit_context.editgroup_id.to_uuid()),
                    $edit_table::ident_id.eq(ident.to_uuid()),
                    $edit_table::rev_id.eq(None::<Uuid>),
                    $edit_table::redirect_id.eq(None::<Uuid>),
                    $edit_table::prev_rev.eq(current.rev_id),
                    $edit_table::extra_json.eq(&edit_context.extra_json),
                ))
                .get_result(conn)?;

            Ok(edit)
        }
    }
}

macro_rules! generic_db_get_history {
    // TODO: only actually need edit table? and maybe not that?
    ($edit_table:ident) => {
        fn db_get_history(conn: &DbConn, ident: FatCatId, limit: Option<i64>) -> Result<Vec<EntityHistoryEntry>> {
            let limit = limit.unwrap_or(50); // XXX: make a static

            let rows: Vec<(EditgroupRow, ChangelogRow, Self::EditRow)> = editgroup::table
                .inner_join(changelog::table)
                .inner_join($edit_table::table)
                .filter($edit_table::ident_id.eq(ident.to_uuid()))
                .order(changelog::id.desc())
                .limit(limit)
                .get_results(conn)?;

            let history: Vec<EntityHistoryEntry> = rows.into_iter()
                .map(|(eg_row, cl_row, e_row)| EntityHistoryEntry {
                    edit: e_row.into_model().expect("edit row to model"),
                    editgroup: eg_row.into_model_partial(),
                    changelog_entry: cl_row.into_model(),
                })
                .collect();
            Ok(history)
        }
    }
}

impl EntityCrud for WorkEntity {
    type EditRow = WorkEditRow;
    type IdentRow = WorkIdentRow;
    type RevRow = WorkRevRow;

    generic_parse_editgroup_id!();
    generic_db_get!(work_ident, work_rev);
    generic_db_get_rev!(work_rev);
    generic_db_create_batch!();
    generic_db_delete!(work_ident, work_edit);
    generic_db_get_history!(work_edit);

    fn db_create(&self, conn: &DbConn, edit_context: &EditContext) -> Result<Self::EditRow> {

        // TODO: refactor to use insert_rev
        let edit: WorkEditRow =
            diesel::sql_query(
                "WITH rev AS ( INSERT INTO work_rev (extra_json)
                        VALUES ($1)
                        RETURNING id ),
                ident AS ( INSERT INTO work_ident (rev_id)
                            VALUES ((SELECT rev.id FROM rev))
                            RETURNING id )
            INSERT INTO work_edit (editgroup_id, ident_id, rev_id) VALUES
                ($2, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev))
            RETURNING *",
            ).bind::<diesel::sql_types::Nullable<diesel::sql_types::Json>, _>(&self.extra)
                .bind::<diesel::sql_types::Uuid, _>(edit_context.editgroup_id.to_uuid())
                .get_result(conn)?;

        Ok(edit)
    }

    fn db_update(&self, conn: &DbConn, edit_context: &EditContext, ident: FatCatId) -> Result<Self::EditRow> {
        // TODO: refactor this into a check on WorkIdentRow
        let current: WorkIdentRow = work_ident::table.find(ident.to_uuid()).first(conn)?;
        if current.is_live != true {
            // TODO: what if isn't live? 4xx not 5xx
            bail!("can't delete an entity that doesn't exist yet");
        }
        if current.rev_id.is_none() {
            // TODO: what if it's already deleted? 4xx not 5xx
            bail!("entity was already deleted");
        }

        let edit: WorkEditRow =
            diesel::sql_query(
                "WITH rev AS ( INSERT INTO work_rev (extra_json)
                        VALUES ($1)
                        RETURNING id ),
            INSERT INTO work_edit (editgroup_id, ident_id, rev_id, prev_rev) VALUES
                ($2, $3, (SELECT rev.id FROM rev), $4)
            RETURNING *",
            ).bind::<diesel::sql_types::Nullable<diesel::sql_types::Json>, _>(&self.extra)
                .bind::<diesel::sql_types::Uuid, _>(edit_context.editgroup_id.to_uuid())
                .bind::<diesel::sql_types::Uuid, _>(ident.to_uuid())
                .bind::<diesel::sql_types::Uuid, _>(current.rev_id.unwrap())
                .get_result(conn)?;

        Ok(edit)
    }


    fn db_from_row(conn: &DbConn, rev_row: Self::RevRow, ident_row: Option<Self::IdentRow>) -> Result<Self> {

        let (state, ident_id, redirect_id) = match ident_row {
            Some(i) => (
                Some(i.state().unwrap().shortname()),
                Some(FatCatId::from_uuid(&i.id).to_string()),
                i.redirect_id.map(|u| FatCatId::from_uuid(&u).to_string()),
            ),
            None => (None, None, None),
        };

        Ok(WorkEntity {
            state: state,
            ident: ident_id,
            revision: Some(rev_row.id.to_string()),
            redirect: redirect_id,
            editgroup_id: None,
            extra: rev_row.extra_json,
        })
    }

    fn db_insert_rev(&self, conn: &DbConn, edit_context: &EditContext) -> Result<Uuid> {
        unimplemented!()
    }
}