-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the handler for fetching all sites
- Loading branch information
1 parent
fee74b9
commit 1fbe1b5
Showing
8 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |