Skip to content

Commit

Permalink
Bind data to view.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbyers committed Apr 1, 2024
1 parent 07fcfc6 commit e542497
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 30 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
watch:
cargo watch -x check -x test -x run
cargo watch -x "run --bin zero2prod"

lint:
cargo clippy
Expand Down
28 changes: 25 additions & 3 deletions src/routes/index.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
use actix_web::{get, HttpResponse};
use actix_web::{get, web, HttpResponse};
use tera::{Context, Tera};

use crate::{models::sm_scrape, routes::scrapes};

#[get("/")]
async fn home() -> HttpResponse {
let tera_context = Context::new();
async fn home(conn: web::Data<libsql::Connection>) -> HttpResponse {
let mut tera_context = Context::new();

let select_page = sm_scrape::select_with_pagination(
"id, url, arrival, lot_size, bag_size, score, packaging, cultivar_detail, spro_rec",
"score != ''",
"score",
"DESC",
200,
0,
);
let rows = match conn.get_ref().query(&select_page, ()).await {
Ok(rows) => rows,
Err(e) => {
eprintln!("Error: {}", e);
return HttpResponse::InternalServerError().body("Error querying the database");
}
};
let res = scrapes::rows_to_response(rows).await;
tera_context.insert("scrapes", &res.scrapes);

let rendered = match Tera::one_off(include_str!("../templates/index.html"), &tera_context, true)
{
Ok(rendered) => rendered,
Expand All @@ -12,5 +33,6 @@ async fn home() -> HttpResponse {
return HttpResponse::InternalServerError().finish();
}
};

HttpResponse::Ok().body(rendered)
}
14 changes: 7 additions & 7 deletions src/routes/scrapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ async fn get_scrapes(
HttpResponse::Ok().json(response)
}

#[derive(Serialize)]
struct Response {
scrapes: Vec<Scrape>,
total: u32,
#[derive(Serialize, Debug)]
pub struct Response {
pub scrapes: Vec<Scrape>,
pub total: u32,
}

#[derive(Serialize, Clone)]
struct Scrape {
#[derive(Serialize, Clone, Debug)]
pub struct Scrape {
id: String,
url: String,
arrival: String,
Expand Down Expand Up @@ -106,7 +106,7 @@ macro_rules! real_row_to_string {
};
}

async fn rows_to_response(mut rows: Rows) -> Response {
pub async fn rows_to_response(mut rows: Rows) -> Response {
let mut scrapes = Vec::new();
while let Ok(Some(row)) = rows.next().await {
let scrape = Scrape {
Expand Down
55 changes: 36 additions & 19 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Table</title>
<title>Sweet Maria's Data Explorer</title>
<link href="https://cdn.jsdelivr.net/npm/daisyui@4.9.0/dist/full.min.css" rel="stylesheet" type="text/css" />
<style>
.center-screen {
Expand All @@ -15,24 +15,41 @@
</style>
</head>
<body class="center-screen">
<table class="table-auto">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Example 1</td>
<td>This is an example</td>
</tr>
<tr>
<td>Example 2</td>
<td>This is another example</td>
</tr>
</tbody>
</table>
<div class="px-4 py2 overflow-x-auto max-h-full">
<table class="table table-auto">
<thead>
<tr>
<th>Score</th>
<th>URL</th>
<th>Arrival</th>
<th>Lot Size</th>
<th>Bag Size</th>
<th>Packaging</th>
<th>Cultivar Details</th>
<th>Spro Rec</th>
</tr>
</thead>
<tbody>
{% for scrape in scrapes %}
<tr>
<td>{{ scrape.score }}</td>
<td><a href="{{ scrape.url }}" target="_blank">{{ scrape.url }}</a></td>
<td>{{ scrape.arrival }}</td>
<td>{{ scrape.lot_size }}</td>
<td>{{ scrape.bag_size }}</td>
<td>{{ scrape.packaging }}</td>
<td>{{ scrape.cultivar_detail }}</td>
<td>{{ scrape.spro_rec }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<script src="https://cdn.tailwindcss.com"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
window.scrapes = {{ scrapes | json_encode() | safe }};
});
</script>
</body>
</html>

0 comments on commit e542497

Please sign in to comment.