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) -> 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::(&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) ); }