added nested routes, added separate config struct, among other items
This commit is contained in:
@ -1,2 +1,39 @@
|
||||
use crate::{datasources::comments::CommentsDatasource, AppState};
|
||||
use axum::{
|
||||
extract::{Form, State},
|
||||
routing::{get, post},
|
||||
Json,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct CommentInput {
|
||||
name: String,
|
||||
body: String,
|
||||
post_id: i32,
|
||||
}
|
||||
|
||||
pub struct CommentsRoute;
|
||||
impl CommentsRoute {}
|
||||
impl CommentsRoute {
|
||||
pub fn routes(app_state: &AppState) -> axum::Router {
|
||||
// add more comment routes here!
|
||||
axum::Router::new()
|
||||
.route("/post/:id", get(CommentsRoute::get_post_comments))
|
||||
.route("/add", post(CommentsRoute::insert_comment))
|
||||
.with_state(app_state.db)
|
||||
}
|
||||
|
||||
async fn get_post_comments(State(pool): State<PgPool>) -> Json<()> {
|
||||
let results = CommentsDatasource::get_posts_comments(pool).await;
|
||||
Json {}
|
||||
}
|
||||
|
||||
async fn insert_comment(
|
||||
State(pool): State<PgPool>,
|
||||
Form(comment_input): Form<CommentInput>,
|
||||
) -> bool {
|
||||
let results = CommentsDatasource::insert_comment(pool, comment_input).await;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,32 @@
|
||||
use crate::{datasources::posts::PostsDatasource, AppState};
|
||||
use axum::{extract::State, routing::get, Json, Router};
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub struct PostsRoute;
|
||||
impl PostsRoute {}
|
||||
impl PostsRoute {
|
||||
pub fn routes(app_state: &AppState) -> Router {
|
||||
// add more post routes here!
|
||||
Router::new()
|
||||
.route("/", get(PostsRoute::get_all))
|
||||
.route("/:id", get(PostsRoute::get_one))
|
||||
.with_state(app_state.db)
|
||||
}
|
||||
|
||||
// get all posts
|
||||
async fn get_all(State(pool): State<PgPool>) -> Json<()> {
|
||||
let results = PostsDatasource::get_all(pool).await;
|
||||
Json {}
|
||||
}
|
||||
|
||||
// get one post
|
||||
async fn get_one(State(pool): State<PgPool>) -> Json<()> {
|
||||
let results = PostsDatasource::get_one(pool).await;
|
||||
Json {}
|
||||
}
|
||||
|
||||
// get the top three posts with the highest view count
|
||||
async fn get_popular_posts(State(pool): State<PgPool>) -> Json<()> {}
|
||||
|
||||
// get the top three posts with the most comments
|
||||
async fn get_hot_posts(State(pool): State<PgPool>) -> Json<()> {}
|
||||
}
|
||||
|
@ -1,15 +1,23 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
||||
pub struct RootRoute;
|
||||
impl RootRoute {
|
||||
pub async fn root() -> Html<&'static str> {
|
||||
pub fn routes() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(RootRoute::root))
|
||||
.fallback(RootRoute::not_found)
|
||||
}
|
||||
|
||||
async fn root() -> Html<&'static str> {
|
||||
Html("<p>Copyright Wyatt J. Miller 2024</p>")
|
||||
}
|
||||
|
||||
pub async fn not_found() -> impl IntoResponse {
|
||||
async fn not_found() -> impl IntoResponse {
|
||||
(StatusCode::NOT_FOUND, "¯\\_(ツ)_/¯")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user