27 lines
773 B
Rust
27 lines
773 B
Rust
use actix_web::{web::{ServiceConfig, Data, scope}, HttpResponse, Responder, get};
|
|
use diesel::RunQueryDsl;
|
|
|
|
use crate::database::models::post::Post;
|
|
use crate::schema::posts::dsl::*;
|
|
use crate::State;
|
|
|
|
#[get("")]
|
|
async fn get(state: Data<State>) -> impl Responder {
|
|
let mut conn = match state.db.get() {
|
|
Ok(conn) => conn,
|
|
Err(why) => return HttpResponse
|
|
::InternalServerError()
|
|
.body(format!("database connection error: {}", why)),
|
|
};
|
|
|
|
match posts.load::<Post>(&mut conn) {
|
|
Ok(data) => HttpResponse::Ok().json(data),
|
|
Err(why) => HttpResponse::InternalServerError().body(why.to_string()),
|
|
}
|
|
}
|
|
|
|
pub fn init_routes(cfg: &mut ServiceConfig) {
|
|
cfg.service(scope("/posts")
|
|
.service(get)
|
|
);
|
|
} |