| 61 | } |
| 62 | |
| 63 | async fn handler_generate( |
| 64 | State(state): State<CFEnv>, |
| 65 | ) -> Result<impl IntoResponse, (StatusCode, String)> { |
| 66 | let env = state.env.clone(); |
| 67 | let db = crate::orm::init_db(env).await.map_err(|err| { |
| 68 | console_log!("Failed to connect to database: {:?}", err); |
| 69 | ( |
| 70 | StatusCode::INTERNAL_SERVER_ERROR, |
| 71 | "Failed to connect to database".to_string(), |
| 72 | ) |
| 73 | })?; |
| 74 | |
| 75 | let ret = crate::entity::ActiveModel { |
| 76 | id: NotSet, |
| 77 | title: Set(chrono::Utc::now().to_rfc3339()), |
| 78 | text: Set(uuid::Uuid::new_v4().to_string()), |
| 79 | }; |
| 80 | |
| 81 | let ret = ret.insert(&db).await.map_err(|err| { |
| 82 | console_log!("Failed to insert into database: {:?}", err); |
| 83 | ( |
| 84 | StatusCode::INTERNAL_SERVER_ERROR, |
| 85 | "Failed to insert into database".to_string(), |
| 86 | ) |
| 87 | })?; |
| 88 | |
| 89 | Ok(format!("Inserted: {:?}", ret).into_response()) |
| 90 | } |