summaryrefslogtreecommitdiffstats
path: root/src/routes/artworks.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/artworks.rs')
-rw-r--r--src/routes/artworks.rs45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/routes/artworks.rs b/src/routes/artworks.rs
index 556265f..fae7a10 100644
--- a/src/routes/artworks.rs
+++ b/src/routes/artworks.rs
@@ -1,10 +1,49 @@
-use poem::{handler, web::Path};
+use poem::web::{Data, Multipart, Redirect};
+use poem::IntoResponse;
+use poem::{handler, session::Session, web::Path, Response};
+
+use crate::artist::Artist;
+use crate::db::artworks::Artwork;
+use crate::error::Error;
+use crate::file::File;
+use crate::{Critch, Result};
pub mod comments;
#[handler]
-pub async fn post() {
- todo!()
+pub async fn post(
+ session: &Session,
+ mut multipart: Multipart,
+ critch: Data<&Critch>,
+) -> Result<Response> {
+ if let Some(true) = session.get("is_admin") {
+ let (mut title, mut handle, mut url_source, mut description, mut files) =
+ (None, None, None, None, Vec::new());
+ while let Some(field) = multipart.next_field().await? {
+ match field.name() {
+ Some("title") => title = Some(field.text().await?),
+ Some("artist") => handle = Some(field.text().await?),
+ Some("url") => url_source = Some(field.text().await?),
+ Some("description") => description = Some(field.text().await?),
+ Some("file") => {
+ let content_type = field.content_type().ok_or(Error::BadRequest)?.to_owned();
+ let file_data = field.bytes().await?;
+ let file = File::new(&content_type)?;
+ let file_name = format!("{}.{}", file.file_id(), file.extension());
+ critch.save_file(&file_name, file_data).await?;
+ files.push(file);
+ }
+ _ => return Err(Error::BadRequest),
+ }
+ }
+ let artist = Artist::new(handle.ok_or(Error::BadRequest)?);
+ let artwork = Artwork::new(title, description, url_source, artist, files);
+ critch.db.artworks().create(artwork).await?;
+ println!("saved file");
+ return Ok(Redirect::see_other("/admin").into_response());
+ } else {
+ return Err(Error::Unauthorized);
+ }
}
#[handler]