summaryrefslogtreecommitdiffstats
path: root/rust/src/api_helpers.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-05-23 20:19:41 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-05-24 15:21:32 -0700
commitaff11938989901327fdf0198d08f97b7105e7b3b (patch)
treedaa370f45ee809fadf56c95d4b08c26c9c181218 /rust/src/api_helpers.rs
parentcf937ed89c3fe10d891c41612017301a7ff0fbae (diff)
downloadfatcat-aff11938989901327fdf0198d08f97b7105e7b3b.tar.gz
fatcat-aff11938989901327fdf0198d08f97b7105e7b3b.zip
get_or_create_editgroup() helper for POSTs
Diffstat (limited to 'rust/src/api_helpers.rs')
-rw-r--r--rust/src/api_helpers.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs
new file mode 100644
index 00000000..200b70a8
--- /dev/null
+++ b/rust/src/api_helpers.rs
@@ -0,0 +1,28 @@
+
+use errors::*;
+use diesel;
+use diesel::prelude::*;
+use database_models::*;
+use database_schema::{editgroup, editor};
+
+pub fn get_or_create_editgroup(editor_id: i64, conn: &PgConnection) -> Result<i64> {
+ // check for current active
+ let ed_row: EditorRow = editor::table.find(editor_id).first(conn)?;
+ if let Some(current) = ed_row.active_editgroup_id {
+ return Ok(current);
+ }
+
+ // need to insert and update
+ conn.build_transaction().run(|| {
+
+ let eg_row: EditgroupRow = diesel::insert_into(editgroup::table)
+ .values((
+ editgroup::editor_id.eq(ed_row.id),
+ ))
+ .get_result(conn)?;
+ diesel::update(editor::table.find(ed_row.id))
+ .set(editor::active_editgroup_id.eq(eg_row.id))
+ .execute(conn)?;
+ Ok(eg_row.id)
+ })
+}