Skip to content

Commit

Permalink
Merge pull request #20 from picture-pro/14-move-ownership-from-photo-…
Browse files Browse the repository at this point in the history
…to-photo_group

14 move ownership from photo to photo group
  • Loading branch information
johnbchron authored Feb 19, 2024
2 parents e90423f + b3a4608 commit 04ac923
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
29 changes: 21 additions & 8 deletions crates/bl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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<PhotoGroup> = client
Expand All @@ -141,6 +143,17 @@ pub async fn upload_single_photo(
)
})?;

// update the photo with the group id
let _photo: Option<Photo> = 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())
}

Expand Down
20 changes: 10 additions & 10 deletions crates/core_types/src/photo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<PhotoRecordId>,
pub photos: Vec<PhotoRecordId>,
/// Whether the group is publicly visible.
pub public: bool,
pub public: bool,
}

/// The metadata for uploading a photo group. Not a table.
Expand Down

0 comments on commit 04ac923

Please sign in to comment.