Make the sample javascript first register the hit, then fetch the count.

This commit is contained in:
Joe Ardent 2024-03-28 22:16:18 -07:00
parent bc05fe87b3
commit aad60a5752
3 changed files with 15 additions and 14 deletions

View File

@ -7,20 +7,18 @@
</head> </head>
<body> <body>
<img src=http://localhost:5000/hit/index.html style="visibility:hidden"> <p>There have been <span id="allhits">no</span> views of this page</p>
<p id="allhits"></p>
<p><a href="http://localhost:3000/user">user</a></p> <p><a href="http://localhost:3000/user">user</a></p>
<script>
<script defer>
const hits = document.getElementById('allhits'); const hits = document.getElementById('allhits');
fetch('http://localhost:5000/hits/index.html').then((resp) => { fetch('http://localhost:5000/hit/index.html').then((resp) => {
return resp.text(); return resp.text();
}).then((data) => { }).then((data) => {
hits.innerHTML = data; hits.innerHTML = data;
}); });
</script> </script>
</body> </body>
</html> </html>

View File

@ -83,7 +83,12 @@ async fn register_hit(
tx.commit().await.unwrap_or_default(); tx.commit().await.unwrap_or_default();
} }
"".to_string() let hits = sqlx::query_scalar!("select count(*) from hits where page = ?", slug)
.fetch_one(&db)
.await
.unwrap_or(1);
format!("{hits}")
} }
#[axum::debug_handler] #[axum::debug_handler]

View File

@ -7,19 +7,17 @@
</head> </head>
<body> <body>
<img src=http://localhost:5000/hit/user style="visibility:hidden"> <p>oy, guv, there've been <span id="allhits">no</span> hits, mate.</p>
<p id="allhits"></p>
<script> <script defer>
const hits = document.getElementById('allhits'); const hits = document.getElementById('allhits');
fetch('http://localhost:5000/hits/user').then((resp) => { fetch('http://localhost:5000/hit/user').then((resp) => {
return resp.text(); return resp.text();
}).then((data) => { }).then((data) => {
hits.innerHTML = data; hits.innerHTML = data;
}); });
</script> </script>
</body> </body>
</html> </html>