Skip to content

Commit

Permalink
get extended image report info form database and fixed images in temp…
Browse files Browse the repository at this point in the history
…late
  • Loading branch information
worldofjoni committed Jun 18, 2024
1 parent c9f1984 commit 13747a5
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 61 deletions.
2 changes: 1 addition & 1 deletion backend/src/interface/admin_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct ImageReportInfo {
/// Identifier of the image used in the datastore.
pub image_id: Uuid,
/// URL to the image at the image hoster.
pub image_link: String,
pub image_url: String,
/// Number of times this image was reported, including the current report.
pub report_count: u32,
/// Number of positive ratings for this image.
Expand Down
3 changes: 2 additions & 1 deletion backend/src/interface/persistent_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod model;
use crate::interface::persistent_data::model::{ApiKey, Canteen, Image, Line, Meal, Side};
use crate::util::{Additive, Allergen, Date, FoodType, NutritionData, Price, ReportReason, Uuid};
use async_trait::async_trait;
use model::ExtendedImage;
use sqlx::migrate::MigrateError;
use std::num::TryFromIntError;
use thiserror::Error;
Expand Down Expand Up @@ -174,7 +175,7 @@ pub trait MealplanManagementDataAccess: Send + Sync {
/// An interface for api actions. The Command component uses this interface for database access.
pub trait CommandDataAccess: Sync + Send {
/// Returns the ImageInfo struct of image.
async fn get_image_info(&self, image_id: Uuid) -> Result<Image>;
async fn get_image_info(&self, image_id: Uuid) -> Result<ExtendedImage>;
/// Marks an image as hidden. Hidden images cant be seen by users.
async fn hide_image(&self, image_id: Uuid) -> Result<()>;
/// Saves an image report
Expand Down
13 changes: 12 additions & 1 deletion backend/src/interface/persistent_data/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub struct Side {
pub price: Price,
}

#[derive(Debug, Default, PartialEq, Clone)]
/// This structure is used for database operations. This image structure is based on the database entity 'image'.
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Image {
/// Database-identification of the image.
pub id: Uuid,
Expand All @@ -96,6 +96,17 @@ pub struct Image {
pub meal_id: Uuid,
}

/// This structure contains all information of an image necessary to file a report.
#[derive(Debug, Default, PartialEq, Clone)]
pub struct ExtendedImage {
/// Information that can be directly queried from the image and is also useful elsewhere, see [Image].
pub image: Image,
/// Name of the meal this image belongs to.
pub meal_name: String,
/// List of urls of other images of the same meal.
pub other_image_urls: Vec<String>,
}

/// This struct contains all environmental information. co2 in grams, water in litres
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnvironmentInfo {
Expand Down
73 changes: 49 additions & 24 deletions backend/src/layer/data/database/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use async_trait::async_trait;
use sqlx::{Pool, Postgres};

use crate::{
interface::persistent_data::{model::Image, CommandDataAccess, Result},
interface::persistent_data::{
model::{ExtendedImage, Image},
CommandDataAccess, Result,
},
null_error,
util::{ReportReason, Uuid},
util::{image_id_to_url, ReportReason, Uuid},
};

/// Class implementing all database requests arising from graphql manipulations.
Expand All @@ -17,12 +20,12 @@ pub struct PersistentCommandData {
#[async_trait]
#[allow(clippy::missing_panics_doc)] // necessary because sqlx macro sometimes create unreachable panics?
impl CommandDataAccess for PersistentCommandData {
async fn get_image_info(&self, image_id: Uuid) -> Result<Image> {
async fn get_image_info(&self, image_id: Uuid) -> Result<ExtendedImage> {
let record = sqlx::query!(
r#"
SELECT approved, link_date as upload_date, report_count,
upvotes, downvotes, image_id, rank, food_id
FROM image_detail
upvotes, downvotes, image_id, rank, food_id, f.name as meal_name
FROM image_detail JOIN food f USING (food_id)
WHERE image_id = $1
ORDER BY image_id
"#,
Expand All @@ -31,15 +34,33 @@ impl CommandDataAccess for PersistentCommandData {
.fetch_one(&self.pool)
.await?;

Ok(Image {
approved: null_error!(record.approved),
rank: null_error!(record.rank),
report_count: u32::try_from(null_error!(record.report_count))?,
upload_date: null_error!(record.upload_date),
downvotes: u32::try_from(null_error!(record.downvotes))?,
upvotes: u32::try_from(null_error!(record.upvotes))?,
id: null_error!(record.image_id),
meal_id: null_error!(record.food_id),
let other_image_urls = sqlx::query_scalar!(
"
SELECT image_id FROM image_detail
WHERE currently_visible AND food_id = $1
ORDER BY rank DESC
",
record.food_id
)
.fetch_all(&self.pool)
.await?
.iter()
.map(|i| Ok(image_id_to_url(null_error!(i))))
.collect::<Result<Vec<_>>>()?;

Ok(ExtendedImage {
image: Image {
approved: null_error!(record.approved),
rank: null_error!(record.rank),
report_count: u32::try_from(null_error!(record.report_count))?,
upload_date: null_error!(record.upload_date),
downvotes: u32::try_from(null_error!(record.downvotes))?,
upvotes: u32::try_from(null_error!(record.upvotes))?,
id: null_error!(record.image_id),
meal_id: null_error!(record.food_id),
},
meal_name: record.meal_name,
other_image_urls,
})
}

Expand Down Expand Up @@ -182,16 +203,20 @@ mod test {
assert!(command.get_image_info(WRONG_UUID).await.is_err());
}

fn provide_dummy_image() -> Image {
Image {
id: Uuid::parse_str("76b904fe-d0f1-4122-8832-d0e21acab86d").unwrap(),
rank: 0.5,
downvotes: 0,
upvotes: 0,
approved: false,
upload_date: Local::now().date_naive(),
report_count: 0,
meal_id: Uuid::default(),
fn provide_dummy_image() -> ExtendedImage {
ExtendedImage {
image: Image {
id: Uuid::parse_str("76b904fe-d0f1-4122-8832-d0e21acab86d").unwrap(),
rank: 0.5,
downvotes: 0,
upvotes: 0,
approved: false,
upload_date: Local::now().date_naive(),
report_count: 0,
meal_id: Uuid::default(),
},
meal_name: "Happy Meal".into(),
other_image_urls: vec!["https://picsum.photos/500/300".into()],
}
}

Expand Down
11 changes: 8 additions & 3 deletions backend/src/layer/data/mail/mail_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ mod test {
"the template must not contain any formatting"
);
assert!(
report.contains(info.image_link.as_str()),
report.contains(info.image_url.as_str()),
"the template must contain all of the information from the report info."
);
assert!(
Expand Down Expand Up @@ -235,7 +235,7 @@ mod test {
reason: crate::util::ReportReason::Advert,
image_got_hidden: true,
image_id: Uuid::default(),
image_link: String::from("https://picsum.photos/200/300"),
image_url: String::from("https://picsum.photos/500/330"),
report_count: 1,
positive_rating_count: 10,
negative_rating_count: 20,
Expand All @@ -246,7 +246,12 @@ mod test {
meal_id: Uuid::default(),
meal_name: "Happy Meal".into(),
report_date: Local::now().date_naive(),
other_image_urls: vec!["https://picsum.photos/200/300".into()],
other_image_urls: vec![
"https://picsum.photos/500/300".into(),
"https://picsum.photos/500/350".into(),
"https://picsum.photos/400/350".into(),
"https://picsum.photos/300/350".into(),
],
}
}

Expand Down
Loading

0 comments on commit 13747a5

Please sign in to comment.