Skip to content

Commit

Permalink
implement post_media
Browse files Browse the repository at this point in the history
  • Loading branch information
thebino committed Sep 19, 2023
1 parent 742ec89 commit 61ccdd8
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ serde_with.workspace = true
time.workspace = true
tracing.workspace = true
uuid = { workspace = true, features = ["serde"] }

[dev-dependencies]
testdir.workspace = true
3 changes: 2 additions & 1 deletion crates/common/src/config/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

//! This defines the app configuration
use std::{fmt, fs};
use std::{fmt, fs, path::PathBuf};

Check failure on line 19 in crates/common/src/config/configuration.rs

View workflow job for this annotation

GitHub Actions / Lint checks

unused import: `path::PathBuf`

Check warning on line 19 in crates/common/src/config/configuration.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `path::PathBuf`

Check warning on line 19 in crates/common/src/config/configuration.rs

View workflow job for this annotation

GitHub Actions / Tests

unused import: `path::PathBuf`

use serde::Deserialize;
use tracing::info;
Expand All @@ -43,6 +43,7 @@ impl Configuration {
Some(config)
}

/// Use this for tests
pub fn empty() -> Self {
Configuration {
internal_url: "".into(),
Expand Down
8 changes: 7 additions & 1 deletion crates/media/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ doctest = false

[dependencies]
common.workspace = true
database = { path = "../database" }
database.workspace = true
# database = { path = "../database" }

time.workspace = true

tracing.workspace = true
# serialization
Expand All @@ -34,9 +37,12 @@ uuid = { workspace = true, features = ["serde"] }
sqlx.workspace = true
rand.workspace = true


[dev-dependencies]
# testing
mockall.workspace = true
rstest.workspace = true
tokio.workspace = true
tower = { workspace = true, features = ["util"] }
hyper = { workspace = true, features = ["full"] }
testdir.workspace = true
2 changes: 1 addition & 1 deletion crates/media/src/api/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl MediaApi {
S: Send + Sync + Clone,
{
let media_repository: MediaRepository<SqliteDatabase> =
MediaRepository::new(state.database.clone()).await;
MediaRepository::new(state.database.clone(), state.config.clone()).await;
let repository_state: MediaRepositoryState = Arc::new(media_repository);

Router::new()
Expand Down
67 changes: 59 additions & 8 deletions crates/media/src/api/routes/post_media.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,70 @@
//! Creates a new media item to aggregate related files for current user
//!
use axum::{extract::Multipart, http::StatusCode};
use axum::{
extract::{Multipart, State},
http::StatusCode,
};
use common::auth::user::User;
use tracing::{debug, error};

Check warning on line 8 in crates/media/src/api/routes/post_media.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `error`

Check warning on line 8 in crates/media/src/api/routes/post_media.rs

View workflow job for this annotation

GitHub Actions / Tests

unused import: `error`
use uuid::Uuid;

use crate::{data::error::DataAccessError, repository::MediaRepositoryState};

pub(crate) async fn post_media(
State(repo): State<MediaRepositoryState>,
user: User,
mut _multipart: Multipart,
mut multipart: Multipart,
) -> std::result::Result<String, StatusCode> {
error!("POST /media user={}", user);
let mut name = None;
let mut date_taken = None;

while let Some(field) = multipart.next_field().await.unwrap() {
if let Some(field_name) = field.name() {
match field_name {
"name" => {
name = Some(field.text().await.unwrap());
// debug!("name={}", field.text().await.unwrap());
}
"date_taken" => {
date_taken = Some(field.text().await.unwrap());
// debug!("date_taken={}", field.text().await.unwrap());
}
_ => continue,
}
}
}

if name.is_none() || date_taken.is_none() {
return Err(StatusCode::BAD_REQUEST);
}

let result = repo.create_media_item_for_user(
Uuid::parse_str(user.uuid.as_str()).unwrap(),
name.clone().unwrap(),
date_taken.clone().unwrap(),
);

let id = uuid::Uuid::new_v4();
debug!("add media with id {} into database", id);
// TODO: check if media already exists for user
match result {
Ok(uuid) => {
debug!(
"name={}, taken={} => id={}",
name.unwrap(),
date_taken.unwrap(),
uuid.clone().hyphenated().to_string()
);

// TODO: add item in database for user
Err(StatusCode::NOT_IMPLEMENTED)
Ok(uuid.hyphenated().to_string())
}
Err(error) => {
match error {
DataAccessError::AlreadyExist => {
// TODO: use Redirect::permanent to add a Location header to the already existing item
return Err(StatusCode::SEE_OTHER);
}
_ => {
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
}
}
}
}
4 changes: 2 additions & 2 deletions crates/media/src/api/routes/post_media_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub(crate) async fn post_media_id(
while let Some(mut field) = multipart.next_field().await.unwrap() {
if let Some(field_name) = field.name() {
match field_name {
"description" => {
debug!("description={}", field.text().await.unwrap());
"name" => {
debug!("name={}", field.text().await.unwrap());
}
"file" => {
// TODO: wrap bytes and write to persistency
Expand Down
2 changes: 2 additions & 0 deletions crates/media/src/data/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
pub enum DataAccessError {
NotFound,
#[allow(dead_code)]
InvalidDateFormat,
AlreadyExist,
TechnicalError,
#[allow(dead_code)]
OtherError,
Expand Down
28 changes: 25 additions & 3 deletions crates/media/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/

use axum::async_trait;
use common::config::configuration::Configuration;
use std::sync::Arc;
use std::time::Instant;
use time::OffsetDateTime;

Check warning on line 22 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `time::OffsetDateTime`

Check warning on line 22 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Tests

unused import: `time::OffsetDateTime`
use tracing::info;
use uuid::Uuid;

Expand All @@ -27,6 +29,7 @@ use crate::data::media_item::MediaItem;
#[allow(dead_code)]
pub struct MediaRepository<D> {
pub(crate) database: D,
pub(crate) config: Configuration,
}

pub type MediaRepositoryState = Arc<dyn MediaRepositoryTrait + Send + Sync>;
Expand All @@ -37,11 +40,19 @@ pub type MediaRepositoryState = Arc<dyn MediaRepositoryTrait + Send + Sync>;
pub trait MediaRepositoryTrait {
// Gets a list of media items from the DB filted by user_id
fn get_media_items_for_user(&self, user_id: Uuid) -> Result<Vec<MediaItem>, DataAccessError>;

/// Create a new media item for the given user
fn create_media_item_for_user(
&self,
user_id: Uuid,
name: String,
date_taken: String,
) -> Result<Uuid, DataAccessError>;
}

impl<D> MediaRepository<D> {
pub async fn new(database: D) -> Self {
Self { database }
pub async fn new(database: D, config: Configuration) -> Self {
Self { database, config }
}
}

Expand All @@ -63,6 +74,17 @@ impl<D> MediaRepositoryTrait for MediaRepository<D> {
references: None,
}])
}

/// inside impl
fn create_media_item_for_user(
&self,
user_id: Uuid,

Check warning on line 81 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `user_id`

Check warning on line 81 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Tests

unused variable: `user_id`
name: String,

Check warning on line 82 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `name`

Check warning on line 82 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Tests

unused variable: `name`
date_taken: String,

Check warning on line 83 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `date_taken`

Check warning on line 83 in crates/media/src/repository.rs

View workflow job for this annotation

GitHub Actions / Tests

unused variable: `date_taken`
) -> Result<Uuid, DataAccessError> {
// Err(DataAccessError::AlreadyExist)
Ok(Uuid::new_v4())
}
}

#[allow(unused_imports)]
Expand Down Expand Up @@ -91,7 +113,7 @@ mod tests {

let db = SqliteDatabase::new("target/sqlx/test-dbs/media/repository/tests/test_new.sqlite")
.await;
let repository = MediaRepository::new(db).await;
let repository = MediaRepository::new(db, Configuration::empty()).await;

// when
let result = repository.get_media_items_for_user(Uuid::new_v4());
Expand Down

0 comments on commit 61ccdd8

Please sign in to comment.