Skip to content

Commit

Permalink
add the handler for fetching all sites
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcheng committed Nov 28, 2023
1 parent fee74b9 commit 1fbe1b5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use dotenvy::dotenv;
use rocket::{self, routes};
use rocket_db_pools::Database;
use sqlx::MySqlPool;

use startpage::routes::upload::upload;
use startpage::routes::{auth, category, user};
use startpage::routes::{auth, category, site, user};
use startpage::state::AppState;
use startpage::utils::calculate_expires;
use startpage::Db;
Expand Down Expand Up @@ -75,6 +74,7 @@ async fn main() -> Result<(), rocket::Error> {
category::delete_site,
],
)
.mount("/api/sites", routes![site::all])
.mount("/api/upload", routes![upload])
.launch()
.await?;
Expand Down
1 change: 1 addition & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod auth;
pub mod category;
pub mod site;
pub mod user;
2 changes: 1 addition & 1 deletion src/handlers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::response;
use crate::response::WithTotal;
use crate::Db;

pub async fn get_all_categories(
pub async fn get_categories(
page: i64,
size: i64,
db: &mut Connection<Db>,
Expand Down
27 changes: 27 additions & 0 deletions src/handlers/site.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use rocket_db_pools::Connection;
use sqlx::{query, query_as, Row};

use crate::errors::ServiceError;
use crate::response::site::SiteWithCategory;
use crate::response::WithTotal;
use crate::Db;

pub async fn get_sites(
page: i64,
size: i64,
db: &mut Connection<Db>,
) -> Result<WithTotal<SiteWithCategory>, ServiceError> {
let count = query(r#"SELECT COUNT(id) AS count FROM site"#)
.fetch_one(&mut ***db)
.await?
.get::<i64, &str>("count");

let sites = query_as::<_, SiteWithCategory>(
r#"SELECT site.id AS id, site.name AS name, site.url AS url, site.icon AS icon, site.description AS description, category.name AS category FROM site INNER JOIN category INNER JOIN category_site ON site.id = category_site.site_id AND category.id = category_site.category_id LIMIT ? OFFSET ?"#,
).bind(size).bind(page * size).fetch_all(&mut ***db).await?;

Ok(WithTotal {
total: count,
data: sites,
})
}
10 changes: 10 additions & 0 deletions src/response/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ pub struct Site {
pub description: String,
pub icon: String,
}

#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct SiteWithCategory {
pub id: i64,
pub name: String,
pub url: String,
pub description: String,
pub icon: String,
pub category: String,
}
1 change: 1 addition & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod auth;
pub mod category;
pub mod site;
pub mod upload;
pub mod user;
7 changes: 3 additions & 4 deletions src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use rocket::{delete, get, post, put};
use rocket_db_pools::Connection;

use crate::handlers::category::{
self, add_category, delete_category, get_all_categories, modify_site, update_category,
self, add_category, delete_category, get_categories, modify_site, update_category,
};
use crate::middlewares::JwtMiddleware;
use crate::request::category::{CreateCategory, UpdateCategory};
use crate::request::site::{CreateSite, UpdateSite};
use crate::response::category::Category;
use crate::response::site::Site;
use crate::response::WithTotal;
use crate::state::AppState;
use crate::Db;

#[get("/?<page>&<size>")]
Expand All @@ -28,13 +27,13 @@ pub async fn all(

let size = size.unwrap_or(10);

let categories = get_all_categories(page, size, &mut db).await.map_err(|e| {
let result = get_categories(page, size, &mut db).await.map_err(|e| {
error!("{}", e);

e.into()
})?;

Ok(Json(categories))
Ok(Json(result))
}

#[put("/<id>", format = "json", data = "<category>")]
Expand Down
29 changes: 29 additions & 0 deletions src/routes/site.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use log::error;
use rocket::get;
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket_db_pools::Connection;

use crate::handlers::site::get_sites;
use crate::response::site::SiteWithCategory;
use crate::response::WithTotal;
use crate::Db;

#[get("/?<page>&<size>")]
pub async fn all(
page: Option<i64>,
size: Option<i64>,
mut db: Connection<Db>,
) -> Result<Json<WithTotal<SiteWithCategory>>, Status> {
let page = page.unwrap_or(0);

let size = size.unwrap_or(10);

let result = get_sites(page, size, &mut db).await.map_err(|e| {
error!("{}", e);

e.into()
})?;

Ok(Json(result))
}

0 comments on commit 1fbe1b5

Please sign in to comment.