-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from starknet-id/ayush/unique-page-visit
feat: add unique page visit endpoint
- Loading branch information
Showing
3 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ chrono = "0.4.19" | |
lazy_static = "1.4.0" | ||
regex = "1.10.0" | ||
ctor = "0.2.6" | ||
axum-client-ip = "0.4.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::{ | ||
models::{AppState}, | ||
utils::get_error, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use axum_auto_routes::route; | ||
use mongodb::bson::doc; | ||
use serde::Deserialize; | ||
use std::sync::Arc; | ||
use chrono::Utc; | ||
use mongodb::Collection; | ||
use mongodb::options::UpdateOptions; | ||
use serde_json::json; | ||
use crate::models::UniquePageVisit; | ||
use axum_client_ip::InsecureClientIp; | ||
|
||
#[derive(Deserialize)] | ||
pub struct GetQuestsQuery { | ||
page_id: String, | ||
} | ||
|
||
#[route(get, "/unique_page_visit", crate::endpoints::unique_page_visit)] | ||
pub async fn handler( | ||
insecure_ip: InsecureClientIp, | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<GetQuestsQuery>, | ||
) -> impl IntoResponse { | ||
let addr = insecure_ip.0; | ||
let id = query.page_id; | ||
let unique_viewers_collection: Collection<UniquePageVisit> = | ||
state.db.collection("unique_viewers"); | ||
let created_at = Utc::now().timestamp_millis(); | ||
let filter = doc! { "viewer_ip": addr.to_string(), "viewed_page_id": &id }; | ||
let update = doc! { "$setOnInsert": { "viewer_ip": addr.to_string(), "viewed_page_id": &id,"timestamp":created_at } }; | ||
let options = UpdateOptions::builder().upsert(true).build(); | ||
|
||
match unique_viewers_collection.update_one(filter, update, options) | ||
.await { | ||
Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(), | ||
Err(_) => get_error("unable to detect page visit status".to_string()), | ||
} | ||
} |