Skip to content

Commit

Permalink
renamed file handler interface to image storage
Browse files Browse the repository at this point in the history
  • Loading branch information
worldofjoni committed Sep 29, 2023
1 parent ec7474a commit 7d1d68e
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ use thiserror::Error;

use crate::util::{ImageResource, Uuid};

pub type Result<T> = std::result::Result<T, FileError>;
pub type Result<T> = std::result::Result<T, ImageError>;

/// This interface allows to store images as file.
#[async_trait]
pub trait FileHandler: Send + Sync {
pub trait ImageStorage: Send + Sync {
/// Permanently saves an image with the given id.
async fn save_image(&self, id: Uuid, image: ImageResource) -> Result<()>;
}

/// Enum describing possible ways an file operation can go wrong.
#[derive(Debug, Error)]
pub enum FileError {
// TODO
pub enum ImageError {
/// An error in the image processing library occurred.
#[error("Error while image operation: {0}")]
ImageError(#[from] image::ImageError),
}
2 changes: 1 addition & 1 deletion backend/src/interface/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Module bundle for interface models
pub mod admin_notification;
pub mod api_command;
pub mod file_handler;
pub mod image_storage;
pub mod image_validation;
pub mod mealplan_management;
pub mod mensa_parser;
Expand Down
1 change: 1 addition & 0 deletions backend/src/layer/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! In this layer all interactions with the outside world and the required conversions of data take place.

pub mod database;
pub mod file_handler;
pub mod mail;
pub mod swka_parser;
26 changes: 13 additions & 13 deletions backend/src/layer/logic/api_command/command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
interface::{
admin_notification::{AdminNotification, ImageReportInfo},
api_command::{AuthInfo, Command, CommandError, Result},
file_handler::FileHandler,
image_storage::ImageStorage,
image_validation::ImageValidation,
persistent_data::{model::Image, CommandDataAccess},
},
Expand All @@ -20,12 +20,12 @@ pub struct CommandHandler<DataAccess, Notify, File, Validation>
where
DataAccess: CommandDataAccess,
Notify: AdminNotification,
File: FileHandler,
File: ImageStorage,
Validation: ImageValidation,
{
command_data: DataAccess,
admin_notification: Notify,
file_handler: File,
image_storage: File,
image_validation: Validation,
auth: Authenticator,
}
Expand All @@ -34,7 +34,7 @@ impl<DataAccess, Notify, File, Validation> CommandHandler<DataAccess, Notify, Fi
where
DataAccess: CommandDataAccess,
Notify: AdminNotification,
File: FileHandler,
File: ImageStorage,
Validation: ImageValidation,
{
/// A function that creates a new [`CommandHandler`]
Expand All @@ -44,7 +44,7 @@ where
pub async fn new(
command_data: DataAccess,
admin_notification: Notify,
file_handler: File,
image_storage: File,
image_validation: Validation,
) -> Result<Self> {
let keys: Vec<String> = command_data
Expand All @@ -56,7 +56,7 @@ where
Ok(Self {
command_data,
admin_notification,
file_handler,
image_storage,
image_validation,
auth: Authenticator::new(keys),
})
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<DataAccess, Notify, File, Image> Command for CommandHandler<DataAccess, Not
where
DataAccess: CommandDataAccess,
Notify: AdminNotification,
File: FileHandler,
File: ImageStorage,
Image: ImageValidation,
{
async fn report_image(
Expand Down Expand Up @@ -182,7 +182,7 @@ where
};
self.auth.authn_command(&auth_info, &command_type)?;

_ = &self.file_handler;
_ = &self.image_storage;
_ = &self.image_validation;
todo!() // todo
}
Expand All @@ -206,7 +206,7 @@ mod test {
use crate::interface::api_command::{Command, InnerAuthInfo, Result};
use crate::interface::persistent_data::model::Image;
use crate::layer::logic::api_command::test::mocks::{
CommandFileHandlerMock, CommandImageValidationMock, IMAGE_ID_TO_FAIL, INVALID_URL,
CommandImageStorageMock, CommandImageValidationMock, IMAGE_ID_TO_FAIL, INVALID_URL,
MEAL_ID_TO_FAIL,
};
use crate::layer::logic::api_command::{
Expand Down Expand Up @@ -418,18 +418,18 @@ mod test {
CommandHandler<
CommandDatabaseMock,
CommandAdminNotificationMock,
CommandFileHandlerMock,
CommandImageStorageMock,
CommandImageValidationMock,
>,
> {
let command_data = CommandDatabaseMock;
let admin_notification = CommandAdminNotificationMock;
let file_handler = CommandFileHandlerMock;
let image_storage = CommandImageStorageMock;
let image_validation = CommandImageValidationMock;
CommandHandler::new(
command_data,
admin_notification,
file_handler,
image_storage,
image_validation,
)
.await
Expand All @@ -445,7 +445,7 @@ mod test {
assert!(CommandHandler::<
CommandDatabaseMock,
CommandAdminNotificationMock,
CommandFileHandlerMock,
CommandImageStorageMock,
CommandImageValidationMock,
>::will_be_hidden(&image));
}
Expand Down
10 changes: 5 additions & 5 deletions backend/src/layer/logic/api_command/test/mocks.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! This crate contains mocks of [`CommandDataAccess`], [`FileHandler`], [`ImageValidation`] and [`AdminNotification`] for testing.
//! This crate contains mocks of [`CommandDataAccess`], [`ImageStorage`], [`ImageValidation`] and [`AdminNotification`] for testing.

use async_trait::async_trait;

use crate::{
interface::{
admin_notification::{AdminNotification, ImageReportInfo},
file_handler::FileHandler,
image_storage::ImageStorage,
image_validation::ImageValidation,
persistent_data::{
model::{ApiKey, Image},
Expand Down Expand Up @@ -161,15 +161,15 @@ impl ImageValidation for CommandImageValidationMock {
}

#[derive(Default)]
pub struct CommandFileHandlerMock;
pub struct CommandImageStorageMock;

#[async_trait]
impl FileHandler for CommandFileHandlerMock {
impl ImageStorage for CommandImageStorageMock {
async fn save_image(
&self,
_id: Uuid,
_image: ImageResource,
) -> crate::interface::file_handler::Result<()> {
) -> crate::interface::image_storage::Result<()> {
Ok(())
}
}
4 changes: 2 additions & 2 deletions backend/src/layer/trigger/graphql/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use mensa_app_backend::layer::{
logic::api_command::{
command_handler::CommandHandler,
test::mocks::{
CommandAdminNotificationMock, CommandDatabaseMock, CommandFileHandlerMock,
CommandAdminNotificationMock, CommandDatabaseMock, CommandImageStorageMock,
CommandImageValidationMock,
},
},
Expand All @@ -29,7 +29,7 @@ async fn main() {
CommandHandler::new(
CommandDatabaseMock,
CommandAdminNotificationMock,
CommandFileHandlerMock,
CommandImageStorageMock,
CommandImageValidationMock,
)
.await
Expand Down
4 changes: 2 additions & 2 deletions backend/src/startup/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
logic::{
api_command::{
command_handler::CommandHandler,
test::mocks::{CommandFileHandlerMock, CommandImageValidationMock},
test::mocks::{CommandImageStorageMock, CommandImageValidationMock},
},
mealplan_management::meal_plan_manager::MealPlanManager,
},
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Server {

let mail = MailSender::new(config.read_mail_info()?)?;
let parser = SwKaParseManager::new(config.read_swka_info()?)?;
let file_handler = CommandFileHandlerMock; // todo
let file_handler = CommandImageStorageMock; // todo
let google_vision = CommandImageValidationMock; // todo

// logic layer
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use mensa_app_backend::{
data::{database::factory::DataAccessFactory, mail::mail_sender::MailSender},
logic::api_command::{
command_handler::CommandHandler,
test::mocks::{CommandFileHandlerMock, CommandImageValidationMock},
test::mocks::{CommandImageStorageMock, CommandImageValidationMock},
},
},
startup::config::ConfigReader,
Expand Down Expand Up @@ -118,7 +118,7 @@ async fn setup_cmd() -> impl Command {
let reader = ConfigReader::default();

let mail = MailSender::new(reader.read_mail_info().unwrap()).unwrap();
let file_handler = CommandFileHandlerMock; // todo
let file_handler = CommandImageStorageMock; // todo
let image_validation = CommandImageValidationMock; // todo

let factory = DataAccessFactory::new(reader.read_database_info().unwrap(), true)
Expand Down

0 comments on commit 7d1d68e

Please sign in to comment.