Skip to content

Commit

Permalink
add the fetch site API
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcheng committed Jan 17, 2024
1 parent 7ff9b39 commit 903ccb5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ async fn main() -> Result<(), rocket::Error> {
.mount("/api/sites", routes![site::all])
.mount(
"/api/site",
routes![site::add, site::update, site::delete, site::analytics],
routes![
site::get,
site::add,
site::update,
site::delete,
site::analytics
],
)
.mount("/api/upload", routes![upload])
.mount(upload_url, FileServer::from(upload_dir))
Expand Down
16 changes: 16 additions & 0 deletions src/handlers/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::errors::ServiceError;
use crate::models::category::Category;
use crate::models::site::Site;
use crate::request::site::{CreateSite, UpdateSite};
use crate::response::site::Site as SiteResponse;
use crate::response::site::SiteWithCategory;
use crate::response::WithTotal;
use crate::MySQLDb;
Expand Down Expand Up @@ -259,3 +260,18 @@ pub async fn analytics(id: &str, db: &mut Connection<MySQLDb>) -> Result<(), Ser

Ok(())
}

pub async fn get_site(id: i64, db: &mut Connection<MySQLDb>) -> Result<SiteResponse, ServiceError> {
let record = query_as::<_, Site>(
r#"SELECT id, name, url, description, icon, sort_order, visit_count, created_at, updated_at FROM site WHERE id = ?"#,
)
.bind(id)
.fetch_one(&mut ***db)
.await
.map_err(|e| match e {
sqlx::Error::RowNotFound => ServiceError::BadRequest(String::from("Site not found")),
_ => ServiceError::DatabaseError(e),
})?;

Ok(record.into())
}
15 changes: 15 additions & 0 deletions src/response/site.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use sqlx::FromRow;

use crate::models::site::Site as SiteModel;

#[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct Site {
pub id: i64,
Expand All @@ -21,3 +23,16 @@ pub struct SiteWithCategory {
pub category: String,
pub visit_count: i64,
}

impl From<SiteModel> for Site {
fn from(site: SiteModel) -> Self {
Self {
id: site.id,
name: site.name,
url: site.url,
description: site.description,
icon: site.icon,
visit_count: site.visit_count,
}
}
}
27 changes: 26 additions & 1 deletion src/routes/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,36 @@ use crate::guards::jwt::Middleware;
use crate::handlers::site;
use crate::handlers::site::get_sites;
use crate::request::site::{CreateSite, UpdateSite};
use crate::response::site::SiteWithCategory;
use crate::response::site::{Site, SiteWithCategory};
use crate::response::WithTotal;
use crate::utils::standardize_url;
use crate::MySQLDb;

#[get("/<id>")]
pub async fn get(
id: i64,
config: &State<Config>,
mut db: Connection<MySQLDb>,
) -> Result<Json<Site>, Status> {
let mut site = site::get_site(id, &mut db).await.map_err(|e| {
error!("{}", e);

e.status()
})?;

let icon = site.icon.clone();

let icon = if icon.starts_with("http") || icon.starts_with("https") {
icon
} else {
format!("{}/{}", config.upload_url, icon)
};

site.icon = icon;

Ok(Json(site))
}

#[get("/?<page>&<size>&<search>")]
pub async fn all(
page: Option<i64>,
Expand Down

0 comments on commit 903ccb5

Please sign in to comment.