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( session: &Session, mut multipart: Multipart, critch: Data<&Critch>, ) -> Result { 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] pub async fn get() { todo!() } #[handler] pub async fn put() { todo!() } #[handler] pub async fn delete() { todo!() }