-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
104 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Check
|
||
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters