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>
<body>
<img src=http://localhost:5000/hit/index.html style="visibility:hidden">
<p id="allhits"></p>
<p>There have been <span id="allhits">no</span> views of this page</p>
<p><a href="http://localhost:3000/user">user</a></p>
<script>
<script defer>
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();
}).then((data) => {
hits.innerHTML = data;
});
</script>
</body>
</html>

View File

@ -83,7 +83,12 @@ async fn register_hit(
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]

View File

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