diff --git a/crates/bl/src/lib.rs b/crates/bl/src/lib.rs index 98125b3..0213846 100644 --- a/crates/bl/src/lib.rs +++ b/crates/bl/src/lib.rs @@ -7,6 +7,7 @@ use core_types::{ PrivateArtifact, PublicArtifact, }; use serde::{Deserialize, Serialize}; +use surrealdb::opt::PatchOp; use tracing::instrument; #[derive(Clone, Debug, Deserialize, Serialize, thiserror::Error)] @@ -79,10 +80,10 @@ pub async fn upload_single_photo( // create a photo and upload it to surreal let photo = Photo { - id: core_types::PhotoRecordId(ulid::Ulid::new()), - photographer: user_id, - owner: user_id, - artifacts: PhotoArtifacts { + id: core_types::PhotoRecordId(ulid::Ulid::new()), + // this is set to nil because we don't have a group yet + group: core_types::PhotoGroupRecordId(ulid::Ulid::nil()), + artifacts: PhotoArtifacts { original: core_types::PrivateImageArtifact { artifact_id: original_artifact.id, size: (original_image.width(), original_image.height()), @@ -119,10 +120,11 @@ pub async fn upload_single_photo( // create a photo group and upload it to surreal let group = PhotoGroup { - id: core_types::PhotoGroupRecordId(ulid::Ulid::new()), - owner: user_id, - photos: vec![photo.id], - public: group_meta.public, + id: core_types::PhotoGroupRecordId(ulid::Ulid::new()), + owner: user_id, + photographer: user_id, + photos: vec![photo.id], + public: group_meta.public, }; let group: Vec = client @@ -141,6 +143,17 @@ pub async fn upload_single_photo( ) })?; + // update the photo with the group id + let _photo: Option = client + .update(photo.id) + .patch(PatchOp::replace("/group", group.id)) + .await + .map_err(|e| { + PhotoUploadError::DBError(format!( + "Failed to update photo with group id in surreal: {e}" + )) + })?; + Ok(group.clone()) } diff --git a/crates/core_types/src/photo.rs b/crates/core_types/src/photo.rs index 6764108..034d8ff 100644 --- a/crates/core_types/src/photo.rs +++ b/crates/core_types/src/photo.rs @@ -18,13 +18,11 @@ pub struct PhotoRecordId(pub ulid::Ulid); #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Photo { /// The record ID. - pub id: PhotoRecordId, - /// The user who created the photo. - pub photographer: UserRecordId, - /// The user who owns the photo. - pub owner: UserRecordId, + pub id: PhotoRecordId, + /// The photo group that contains this photo. + pub group: PhotoGroupRecordId, /// The photo's artifacts. - pub artifacts: PhotoArtifacts, + pub artifacts: PhotoArtifacts, } /// The artifacts for a photo. Not a table. @@ -73,13 +71,15 @@ pub struct PhotoGroupRecordId(pub ulid::Ulid); #[derive(Clone, Debug, Deserialize, Serialize)] pub struct PhotoGroup { /// The record ID. - pub id: PhotoGroupRecordId, + pub id: PhotoGroupRecordId, + /// The user who uploaded the photo group. + pub photographer: UserRecordId, /// The user who owns the photo group. - pub owner: UserRecordId, + pub owner: UserRecordId, /// The photos in the group. - pub photos: Vec, + pub photos: Vec, /// Whether the group is publicly visible. - pub public: bool, + pub public: bool, } /// The metadata for uploading a photo group. Not a table.