maybe this works now
This commit is contained in:
parent
f3a61bfa99
commit
5f1410fb2f
15 changed files with 472 additions and 15 deletions
|
@ -2,7 +2,8 @@ use crate::auth::AuthenticatedUser;
|
|||
use crate::{
|
||||
error::AppError,
|
||||
models::{
|
||||
AuthResponse, Claims, CreateLink, Link, LoginRequest, RegisterRequest, User, UserResponse,
|
||||
AuthResponse, Claims, ClickStats, CreateLink, Link, LoginRequest, RegisterRequest,
|
||||
SourceStats, User, UserResponse,
|
||||
},
|
||||
AppState,
|
||||
};
|
||||
|
@ -305,3 +306,85 @@ pub async fn delete_link(
|
|||
|
||||
Ok(HttpResponse::NoContent().finish())
|
||||
}
|
||||
|
||||
pub async fn get_link_clicks(
|
||||
state: web::Data<AppState>,
|
||||
user: AuthenticatedUser,
|
||||
path: web::Path<i32>,
|
||||
) -> Result<impl Responder, AppError> {
|
||||
let link_id = path.into_inner();
|
||||
|
||||
// Verify the link belongs to the user
|
||||
let link = sqlx::query!(
|
||||
"SELECT id FROM links WHERE id = $1 AND user_id = $2",
|
||||
link_id,
|
||||
user.user_id
|
||||
)
|
||||
.fetch_optional(&state.db)
|
||||
.await?;
|
||||
|
||||
if link.is_none() {
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let clicks = sqlx::query_as!(
|
||||
ClickStats,
|
||||
r#"
|
||||
SELECT
|
||||
DATE(created_at)::date as "date!",
|
||||
COUNT(*)::bigint as "clicks!"
|
||||
FROM clicks
|
||||
WHERE link_id = $1
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY DATE(created_at) ASC -- Changed from DESC to ASC
|
||||
LIMIT 30
|
||||
"#,
|
||||
link_id
|
||||
)
|
||||
.fetch_all(&state.db)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(clicks))
|
||||
}
|
||||
|
||||
pub async fn get_link_sources(
|
||||
state: web::Data<AppState>,
|
||||
user: AuthenticatedUser,
|
||||
path: web::Path<i32>,
|
||||
) -> Result<impl Responder, AppError> {
|
||||
let link_id = path.into_inner();
|
||||
|
||||
// Verify the link belongs to the user
|
||||
let link = sqlx::query!(
|
||||
"SELECT id FROM links WHERE id = $1 AND user_id = $2",
|
||||
link_id,
|
||||
user.user_id
|
||||
)
|
||||
.fetch_optional(&state.db)
|
||||
.await?;
|
||||
|
||||
if link.is_none() {
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
|
||||
let sources = sqlx::query_as!(
|
||||
SourceStats,
|
||||
r#"
|
||||
SELECT
|
||||
query_source as "source!",
|
||||
COUNT(*)::bigint as "count!"
|
||||
FROM clicks
|
||||
WHERE link_id = $1
|
||||
AND query_source IS NOT NULL
|
||||
AND query_source != ''
|
||||
GROUP BY query_source
|
||||
ORDER BY COUNT(*) DESC
|
||||
LIMIT 10
|
||||
"#,
|
||||
link_id
|
||||
)
|
||||
.fetch_all(&state.db)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(sources))
|
||||
}
|
||||
|
|
|
@ -1,10 +1,15 @@
|
|||
use actix_cors::Cors;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_files::Files;
|
||||
use actix_web::{middleware::DefaultHeaders, web, App, HttpServer};
|
||||
use anyhow::Result;
|
||||
use simple_link::{handlers, AppState};
|
||||
use simplelink::{handlers, AppState};
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use tracing::info;
|
||||
|
||||
async fn index() -> Result<actix_files::NamedFile, actix_web::Error> {
|
||||
Ok(actix_files::NamedFile::open("./static/index.html")?)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Load environment variables from .env file
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::FromRow;
|
||||
|
||||
|
@ -14,12 +15,10 @@ impl Claims {
|
|||
let exp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs() as usize + 24 * 60 * 60; // 24 hours from now
|
||||
|
||||
Self {
|
||||
sub: user_id,
|
||||
exp,
|
||||
}
|
||||
.as_secs() as usize
|
||||
+ 24 * 60 * 60; // 24 hours from now
|
||||
|
||||
Self { sub: user_id, exp }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,3 +69,15 @@ pub struct User {
|
|||
pub email: String,
|
||||
pub password_hash: String,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow, Serialize)]
|
||||
pub struct ClickStats {
|
||||
pub date: NaiveDate,
|
||||
pub clicks: i64,
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow, Serialize)]
|
||||
pub struct SourceStats {
|
||||
pub source: String,
|
||||
pub count: i64,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue