aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src/api_helpers.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-09-10 18:37:16 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-09-10 18:38:12 -0700
commitbf769e17ccdb788cb6127c26fb7f0e60e1fd0a9e (patch)
tree70b7bd2cd4569a7592709c641512d245873e58be /rust/src/api_helpers.rs
parentd57a807ee0481fd6534307f0870a0f96ea19cd25 (diff)
downloadfatcat-bf769e17ccdb788cb6127c26fb7f0e60e1fd0a9e.tar.gz
fatcat-bf769e17ccdb788cb6127c26fb7f0e60e1fd0a9e.zip
continue CRUD refactor, removing CUD handlers
Can't remove the "get" handlers because of the expand parameter, which is only implemented for releases and in the handler (not in the CRUD trait, yet). Also didn't do the batch handler stuff yet.
Diffstat (limited to 'rust/src/api_helpers.rs')
-rw-r--r--rust/src/api_helpers.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs
index 456646b4..2d203232 100644
--- a/rust/src/api_helpers.rs
+++ b/rust/src/api_helpers.rs
@@ -2,6 +2,7 @@ use data_encoding::BASE32_NOPAD;
use database_models::*;
use database_schema::*;
use fatcat_api::models::*;
+use serde_json;
use diesel;
use diesel::prelude::*;
use errors::*;
@@ -13,6 +14,34 @@ use api_entity_crud::EntityCrud;
pub type DbConn =
diesel::r2d2::PooledConnection<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
+pub struct EditContext {
+ pub editor_id: FatCatId,
+ pub editgroup_id: FatCatId,
+ pub extra_json: Option<serde_json::Value>,
+ pub autoaccept: bool,
+}
+
+pub fn make_edit_context(conn: &DbConn, editgroup_id: Option<FatCatId>, autoaccept: bool) -> Result<EditContext> {
+ let editor_id = Uuid::parse_str("00000000-0000-0000-AAAA-000000000001")?; // TODO: auth
+ let editgroup_id: FatCatId = match (editgroup_id, autoaccept) {
+ (Some(eg), _) => eg,
+ // If autoaccept and no editgroup_id passed, always create a new one for this transaction
+ (None, true) => {
+ let eg_row: EditgroupRow = diesel::insert_into(editgroup::table)
+ .values((editgroup::editor_id.eq(editor_id),))
+ .get_result(conn)?;
+ FatCatId::from_uuid(&eg_row.id)
+ },
+ (None, false) => FatCatId::from_uuid(&get_or_create_editgroup(editor_id, conn)?),
+ };
+ Ok(EditContext {
+ editor_id: FatCatId::from_uuid(&editor_id),
+ editgroup_id: editgroup_id,
+ extra_json: None,
+ autoaccept: autoaccept,
+ })
+}
+
/// This function should always be run within a transaction
pub fn get_or_create_editgroup(editor_id: Uuid, conn: &DbConn) -> Result<Uuid> {
// check for current active