| 18 | } |
| 19 | |
| 20 | pub async fn update_post_by_id( |
| 21 | db: &DbConn, |
| 22 | id: i32, |
| 23 | form_data: post::Model, |
| 24 | ) -> Result<post::Model, DbErr> { |
| 25 | let post: post::ActiveModel = Post::find_by_id(id) |
| 26 | .one(db) |
| 27 | .await? |
| 28 | .ok_or(DbErr::Custom("Cannot find post.".to_owned())) |
| 29 | .map(Into::into)?; |
| 30 | |
| 31 | post::ActiveModel { |
| 32 | id: post.id, |
| 33 | title: Set(form_data.title.to_owned()), |
| 34 | text: Set(form_data.text.to_owned()), |
| 35 | } |
| 36 | .update(db) |
| 37 | .await |
| 38 | } |
| 39 | |
| 40 | pub async fn delete_post(db: &DbConn, id: i32) -> Result<DeleteResult, DbErr> { |
| 41 | let post: post::ActiveModel = Post::find_by_id(id) |