Skip to main content

systemprompt_cli/commands/core/content/link/
show.rs

1use crate::cli_settings::CliConfig;
2use crate::commands::core::content::types::LinkDetailOutput;
3use crate::shared::CommandResult;
4use anyhow::{anyhow, Result};
5use clap::Args;
6use systemprompt_content::services::LinkGenerationService;
7use systemprompt_runtime::AppContext;
8
9const DEFAULT_BASE_URL: &str = "https://systemprompt.io";
10
11#[derive(Debug, Args)]
12pub struct ShowArgs {
13    #[arg(help = "Short code")]
14    pub short_code: String,
15}
16
17pub async fn execute(
18    args: ShowArgs,
19    _config: &CliConfig,
20) -> Result<CommandResult<LinkDetailOutput>> {
21    let ctx = AppContext::new().await?;
22    let service = LinkGenerationService::new(ctx.db_pool())?;
23
24    let link = service
25        .get_link_by_short_code(&args.short_code)
26        .await?
27        .ok_or_else(|| anyhow!("Link not found: {}", args.short_code))?;
28
29    let full_url = link.get_full_url();
30    let short_url = format!("{}/r/{}", DEFAULT_BASE_URL, link.short_code);
31
32    let output = LinkDetailOutput {
33        id: link.id,
34        short_code: link.short_code,
35        target_url: link.target_url,
36        full_url: if full_url == short_url {
37            short_url
38        } else {
39            full_url
40        },
41        link_type: link.link_type,
42        campaign_id: link.campaign_id,
43        campaign_name: link.campaign_name,
44        source_content_id: link.source_content_id,
45        click_count: link.click_count.unwrap_or(0),
46        unique_click_count: link.unique_click_count.unwrap_or(0),
47        conversion_count: link.conversion_count.unwrap_or(0),
48        is_active: link.is_active.unwrap_or(true),
49        created_at: link.created_at,
50    };
51
52    Ok(CommandResult::card(output).with_title("Link Details"))
53}