Individual post api endpoint

This commit is contained in:
Imbus 2023-10-27 16:05:12 +02:00
parent 6bef5ada4d
commit 9c60ae2000
3 changed files with 21 additions and 3 deletions

View file

@ -1,8 +1,8 @@
use crate::db::{db_get_latest_posts, db_new_post};
use crate::db::{db_get_latest_posts, db_get_post, db_new_post};
use crate::jwt::validate_token;
use crate::ServerState;
use actix_web::web::{Data, Query};
use actix_web::web::{Data, Path, Query};
use actix_web::{get, post, web::Json, HttpResponse, Responder, Result};
use log::info;
use serde::{Deserialize, Serialize};
@ -94,3 +94,12 @@ pub async fn new_post(new_post: Json<NewPost>, state: Data<ServerState>) -> Resu
None => Ok(HttpResponse::InternalServerError().json("Error")),
};
}
#[get("posts/{id}")]
pub async fn post_by_id(path: Path<i64>, state: Data<ServerState>) -> Result<impl Responder> {
let id = path.into_inner();
match db_get_post(id, &state.pool).await {
Some(post) => Ok(HttpResponse::Ok().json(post)),
None => Ok(HttpResponse::NotFound().json("Error")),
}
}