2024-12-10 14:06:22 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use crate::utils::rss;
|
2024-09-24 20:26:15 -04:00
|
|
|
use crate::{datasources::posts::PostsDatasource, AppState};
|
2024-12-10 14:06:22 -05:00
|
|
|
use axum::http::{HeaderMap, HeaderValue};
|
2024-09-25 18:29:12 -04:00
|
|
|
use axum::{
|
2024-09-26 13:11:55 -04:00
|
|
|
extract::{Path, State},
|
2024-12-10 14:06:22 -05:00
|
|
|
http::{header, StatusCode},
|
2024-09-26 13:11:55 -04:00
|
|
|
response::IntoResponse,
|
2024-09-25 18:29:12 -04:00
|
|
|
routing::get,
|
|
|
|
Json, Router,
|
|
|
|
};
|
|
|
|
use chrono::Utc;
|
2024-09-26 13:11:55 -04:00
|
|
|
use serde::{Deserialize, Serialize, Serializer};
|
|
|
|
use sqlx::{Pool, Postgres};
|
2024-09-25 18:29:12 -04:00
|
|
|
|
2024-12-10 14:06:22 -05:00
|
|
|
#[derive(sqlx::FromRow, Serialize, Debug, Clone)]
|
2024-09-25 18:29:12 -04:00
|
|
|
pub struct Post {
|
|
|
|
pub post_id: i32,
|
2024-12-02 18:29:23 -05:00
|
|
|
pub author_id: Option<i32>,
|
2024-09-26 13:11:55 -04:00
|
|
|
pub first_name: Option<String>,
|
|
|
|
pub last_name: Option<String>,
|
2024-09-25 18:29:12 -04:00
|
|
|
pub title: String,
|
|
|
|
pub body: String,
|
|
|
|
#[serde(serialize_with = "serialize_datetime")]
|
2024-09-26 13:11:55 -04:00
|
|
|
pub created_at: Option<chrono::DateTime<Utc>>,
|
|
|
|
}
|
|
|
|
|
2024-12-02 18:29:23 -05:00
|
|
|
#[derive(sqlx::FromRow, Serialize, Debug)]
|
|
|
|
pub struct PostFeaturedVariant {
|
|
|
|
pub post_id: i32,
|
|
|
|
pub author_id: Option<i32>,
|
|
|
|
pub first_name: Option<String>,
|
|
|
|
pub last_name: Option<String>,
|
|
|
|
pub title: String,
|
|
|
|
pub body: String,
|
|
|
|
#[serde(serialize_with = "serialize_datetime")]
|
|
|
|
pub created_at: Option<chrono::DateTime<Utc>>,
|
|
|
|
pub is_featured: Option<bool>,
|
|
|
|
}
|
|
|
|
|
2024-09-26 13:11:55 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct PostGetOneParams {
|
|
|
|
pub id: i32,
|
2024-09-25 18:29:12 -04:00
|
|
|
}
|
2024-09-24 20:26:15 -04:00
|
|
|
|
2024-09-22 22:38:35 -04:00
|
|
|
pub struct PostsRoute;
|
2024-09-24 20:26:15 -04:00
|
|
|
impl PostsRoute {
|
|
|
|
pub fn routes(app_state: &AppState) -> Router {
|
|
|
|
// add more post routes here!
|
|
|
|
Router::new()
|
2024-09-25 18:29:12 -04:00
|
|
|
.route("/all", get(PostsRoute::get_all))
|
2024-09-26 13:11:55 -04:00
|
|
|
.route("/:id", get(PostsRoute::get_one))
|
2024-12-02 18:29:23 -05:00
|
|
|
.route("/recent", get(PostsRoute::get_recent_posts))
|
2024-09-26 13:11:55 -04:00
|
|
|
.route("/popular", get(PostsRoute::get_popular_posts))
|
2024-09-26 23:53:39 -04:00
|
|
|
.route("/hot", get(PostsRoute::get_hot_posts))
|
2024-12-02 18:29:23 -05:00
|
|
|
.route("/featured", get(PostsRoute::get_featured_posts))
|
2024-12-10 14:06:22 -05:00
|
|
|
.route("/rss", get(PostsRoute::get_rss_posts))
|
2024-09-25 18:29:12 -04:00
|
|
|
.with_state(app_state.db.clone())
|
2024-09-24 20:26:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get all posts
|
2024-09-25 18:29:12 -04:00
|
|
|
async fn get_all(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
|
|
|
match PostsDatasource::get_all(&pool).await {
|
|
|
|
Ok(posts) => Ok(Json(posts)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
2024-09-24 20:26:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// get one post
|
2024-09-26 13:11:55 -04:00
|
|
|
async fn get_one(
|
|
|
|
State(pool): State<Pool<Postgres>>,
|
|
|
|
Path(params): Path<PostGetOneParams>,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
match PostsDatasource::get_one(&pool, params.id).await {
|
|
|
|
Ok(post) => Ok(Json(post)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
2024-09-24 20:26:15 -04:00
|
|
|
|
2024-12-02 18:29:23 -05:00
|
|
|
// get recent posts
|
|
|
|
async fn get_recent_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
|
|
|
match PostsDatasource::get_recent(&pool).await {
|
|
|
|
Ok(posts) => Ok(Json(posts)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the top three posts with the highest view count
|
2024-09-26 13:11:55 -04:00
|
|
|
async fn get_popular_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
|
|
|
match PostsDatasource::get_popular(&pool).await {
|
|
|
|
Ok(posts) => Ok(Json(posts)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-02 18:29:23 -05:00
|
|
|
// get the top three posts with the most comments
|
2024-09-26 23:53:39 -04:00
|
|
|
async fn get_hot_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
|
|
|
match PostsDatasource::get_hot(&pool).await {
|
|
|
|
Ok(posts) => Ok(Json(posts)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
2024-12-02 18:29:23 -05:00
|
|
|
|
|
|
|
// get posts that are featured
|
|
|
|
async fn get_featured_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
|
|
|
match PostsDatasource::get_featured(&pool).await {
|
|
|
|
Ok(posts) => Ok(Json(posts)),
|
|
|
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
|
|
}
|
|
|
|
}
|
2024-12-10 14:06:22 -05:00
|
|
|
|
|
|
|
// get rss posts
|
|
|
|
async fn get_rss_posts(State(pool): State<Pool<Postgres>>) -> impl IntoResponse {
|
|
|
|
match PostsDatasource::get_all(&pool).await {
|
|
|
|
Ok(posts) => {
|
|
|
|
let web_url = std::env::var("BASE_URI_WEB").expect("No environment variable found");
|
|
|
|
let mapped_posts: HashMap<String, Post> = posts
|
|
|
|
.into_iter()
|
|
|
|
.map(|post| (post.post_id.to_string(), post))
|
|
|
|
.collect();
|
|
|
|
let xml: String = rss::generate_rss(
|
|
|
|
"Wyatt's blog",
|
|
|
|
"Wyatt and friends",
|
|
|
|
web_url.as_str(),
|
|
|
|
&mapped_posts,
|
|
|
|
);
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(
|
|
|
|
header::CONTENT_DISPOSITION,
|
|
|
|
HeaderValue::from_str(r#"attachment; filename="posts.xml""#).unwrap(),
|
|
|
|
);
|
|
|
|
headers.insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
HeaderValue::from_static("application/xml"),
|
|
|
|
);
|
|
|
|
(headers, xml)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert("Content-Type", HeaderValue::from_static("text/plain"));
|
|
|
|
(headers, e.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-25 18:29:12 -04:00
|
|
|
}
|
|
|
|
|
2024-09-27 23:17:33 -04:00
|
|
|
pub fn serialize_datetime<S>(
|
2024-09-26 13:11:55 -04:00
|
|
|
date: &Option<chrono::DateTime<Utc>>,
|
|
|
|
serializer: S,
|
|
|
|
) -> Result<S::Ok, S::Error>
|
2024-09-25 18:29:12 -04:00
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
2024-09-26 13:11:55 -04:00
|
|
|
serializer.serialize_str(&date.unwrap().to_rfc3339())
|
2024-09-24 20:26:15 -04:00
|
|
|
}
|