Skip to main content

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

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