Skip to content

Commit

Permalink
Migrate page link methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmiegit committed Oct 15, 2023
1 parent 6fdda43 commit e72c379
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 80 deletions.
18 changes: 5 additions & 13 deletions deepwell/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ async fn build_module(app_state: ServerState) -> anyhow::Result<RpcModule<Server
register!("page_revision_range", not_implemented);

// Page links
register!("page_get_links_from", not_implemented);
register!("page_get_links_to", not_implemented);
register!("page_get_links_to_missing", not_implemented);
register!("page_get_urls_from", not_implemented);
register!("page_get_urls_to", not_implemented);
register!("page_get_links_from", page_links_from_get);
register!("page_get_links_to", page_links_to_get);
register!("page_get_links_to_missing", page_links_to_missing_get);
register!("page_get_urls_from", page_links_external_from);
register!("page_get_urls_to", page_links_external_to);

// Page parents
register!("parent_set", parent_set);
Expand Down Expand Up @@ -320,14 +320,6 @@ pub fn tide_build_server(state: ServerState) -> tide::Server<ServerState> {
}

fn tide_build_routes(mut app: tide::Server<ServerState>) -> tide::Server<ServerState> {
// Page links
app.at("/page/links/from").put(page_links_from_retrieve);
app.at("/page/links/to").put(page_links_to_retrieve);
app.at("/page/links/to/missing")
.put(page_links_to_missing_retrieve);
app.at("/page/urls/from").put(page_links_external_from);
app.at("/page/urls/to").put(page_links_external_to);

// Files
app.at("/file").post(file_edit).delete(file_delete);
app.at("/file/get").put(file_retrieve);
Expand Down
88 changes: 33 additions & 55 deletions deepwell/src/endpoints/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,95 +20,73 @@

use super::prelude::*;
use crate::services::link::{
GetLinksExternalFrom, GetLinksExternalTo, GetLinksFrom, GetLinksTo, GetLinksToMissing,
GetLinksExternalFrom, GetLinksExternalFromOutput, GetLinksExternalTo,
GetLinksExternalToOutput, GetLinksFrom, GetLinksFromOutput, GetLinksTo,
GetLinksToMissing, GetLinksToMissingOutput, GetLinksToOutput,
};

pub async fn page_links_from_retrieve(mut req: ApiRequest) -> ApiResponse {
let txn = req.database().begin().await?;
let ctx = ServiceContext::from_req(&req, &txn);

pub async fn page_links_from_get(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<GetLinksFromOutput> {
let GetLinksFrom {
site_id,
page: reference,
} = req.body_json().await?;
} = params.parse()?;

tide::log::info!("Getting page links for page {reference:?} in site ID {site_id}");

let page_id = PageService::get_id(&ctx, site_id, reference).await?;
let output = LinkService::get_from(&ctx, page_id).await?;
let body = Body::from_json(&output)?;
txn.commit().await?;

Ok(body.into())
LinkService::get_from(&ctx, page_id).await
}

pub async fn page_links_to_retrieve(mut req: ApiRequest) -> ApiResponse {
let txn = req.database().begin().await?;
let ctx = ServiceContext::from_req(&req, &txn);

pub async fn page_links_to_get(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<GetLinksToOutput> {
let GetLinksTo {
site_id,
page: reference,
} = req.body_json().await?;
} = params.parse()?;

tide::log::info!("Getting page links from page {reference:?} in site ID {site_id}");

let page_id = PageService::get_id(&ctx, site_id, reference).await?;
let output = LinkService::get_to(&ctx, page_id, None).await?;

let body = Body::from_json(&output)?;
txn.commit().await?;
Ok(body.into())
LinkService::get_to(&ctx, page_id, None).await
}

pub async fn page_links_to_missing_retrieve(mut req: ApiRequest) -> ApiResponse {
let txn = req.database().begin().await?;
let ctx = ServiceContext::from_req(&req, &txn);

let GetLinksToMissing { site_id, page_slug } = req.body_json().await?;
pub async fn page_links_to_missing_get(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<GetLinksToMissingOutput> {
let GetLinksToMissing { site_id, page_slug } = params.parse()?;
tide::log::info!(
"Getting missing page links from page slug {page_slug} in site ID {site_id}",
);

let output = LinkService::get_to_missing(&ctx, site_id, &page_slug, None).await?;

let body = Body::from_json(&output)?;
txn.commit().await?;
Ok(body.into())
LinkService::get_to_missing(&ctx, site_id, &page_slug, None).await
}

pub async fn page_links_external_from(mut req: ApiRequest) -> ApiResponse {
let txn = req.database().begin().await?;
let ctx = ServiceContext::from_req(&req, &txn);

pub async fn page_links_external_from(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<GetLinksExternalFromOutput> {
let GetLinksExternalFrom {
site_id,
page: reference,
} = req.body_json().await?;
} = params.parse()?;

tide::log::info!(
"Getting external links from page {reference:?} in site ID {site_id}",
);

let page_id = PageService::get_id(&ctx, site_id, reference).await?;

let output = LinkService::get_external_from(&ctx, page_id).await?;

let body = Body::from_json(&output)?;
txn.commit().await?;
Ok(body.into())
LinkService::get_external_from(&ctx, page_id).await
}

pub async fn page_links_external_to(mut req: ApiRequest) -> ApiResponse {
let txn = req.database().begin().await?;
let ctx = ServiceContext::from_req(&req, &txn);

let GetLinksExternalTo { site_id, url } = req.body_json().await?;
pub async fn page_links_external_to(
ctx: &ServiceContext<'_>,
params: Params<'static>,
) -> Result<GetLinksExternalToOutput> {
let GetLinksExternalTo { site_id, url } = params.parse()?;
tide::log::info!("Getting external links to URL {url} in site ID {site_id}");

let output = LinkService::get_external_to(&ctx, site_id, &url).await?;

let body = Body::from_json(&output)?;
txn.commit().await?;
Ok(body.into())
LinkService::get_external_to(&ctx, site_id, &url).await
}
24 changes: 12 additions & 12 deletions deepwell/src/services/link/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,70 @@ use crate::models::page_link::Model as PageLinkModel;
use crate::web::Reference;
use time::OffsetDateTime;

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct GetLinksFrom<'a> {
pub site_id: i64,
pub page: Reference<'a>,
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub struct GetLinksFromOutput {
pub present: Vec<PageConnectionModel>,
pub absent: Vec<PageConnectionMissingModel>,
pub external: Vec<PageLinkModel>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct GetLinksTo<'a> {
pub site_id: i64,
pub page: Reference<'a>,
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub struct GetLinksToOutput {
pub connections: Vec<PageConnectionModel>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct GetLinksToMissing {
pub site_id: i64,
pub page_slug: String,
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub struct GetLinksToMissingOutput {
pub connections: Vec<PageConnectionMissingModel>,
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub struct GetConnectionsFromOutput {
pub present: Vec<PageConnectionModel>,
pub absent: Vec<PageConnectionMissingModel>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct GetLinksExternalFrom<'a> {
pub site_id: i64,
pub page: Reference<'a>,
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub struct GetLinksExternalFromOutput {
pub links: Vec<PageLinkModel>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub struct GetLinksExternalTo {
pub site_id: i64,
pub url: String,
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub struct GetLinksExternalToOutput {
pub links: Vec<ToExternalLink>,
}

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Clone)]
pub struct ToExternalLink {
pub created_at: OffsetDateTime,
pub updated_at: Option<OffsetDateTime>,
Expand Down

0 comments on commit e72c379

Please sign in to comment.