Skip to content

Commit

Permalink
add template
Browse files Browse the repository at this point in the history
  • Loading branch information
DaviRain-Su committed May 31, 2024
1 parent adfd014 commit cde44c9
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 3 deletions.
76 changes: 75 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ reqwest = { version = "0.12", features = ["json"] }
aion-types = { version = "0.1.0", path = "./crates/aion-types" }
aion-parse = { version = "0.1.0", path = "./crates/parse" }
is-url = "1.0.4"
askama = "0.12.1"
askama_actix = "0.14.0"

[dependencies.sqlx]
version = "0.7"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod configuration;
pub mod routes;
pub mod startup;
pub mod telemetry;
pub mod templates;
11 changes: 10 additions & 1 deletion src/routes/rebase/query_all.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::templates::all_news::AllNewsTemplate;
use actix_web::web;
use actix_web::HttpResponse;
use aion_types::rebase::response::{ListAllItemsResponse, SimpleDisplay};
use askama_actix::TemplateToResponse;
use sqlx::query_as;
use sqlx::PgPool;

Expand Down Expand Up @@ -56,7 +58,14 @@ pub async fn list_all(pool: web::Data<PgPool>) -> HttpResponse {
.await;

match result {
Ok(items) => HttpResponse::Ok().json(items),
Ok(items) => {
let items = items
.into_iter()
.map(SimpleDisplay::from)
.collect::<Vec<_>>();
let template = AllNewsTemplate { items };
template.to_response()
}
Err(e) => {
tracing::error!("Failed to execute query: {:?}", e);
HttpResponse::InternalServerError().finish()
Expand Down
12 changes: 11 additions & 1 deletion src/routes/rebase/query_latest_news.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::templates::latest_news::LatestNewsTemplate;
use actix_web::web;
use actix_web::HttpResponse;
use aion_types::rebase::response::ListAllItemsResponse;
use aion_types::rebase::response::SimpleDisplay;
use askama_actix::TemplateToResponse;
use sqlx::query_as;
use sqlx::PgPool;

Expand All @@ -14,7 +17,14 @@ pub async fn list_latest_news(conn_pool: web::Data<PgPool>) -> HttpResponse {
.await;

match tags_result {
Ok(items) => HttpResponse::Ok().json(items),
Ok(items) => {
let items = items
.into_iter()
.map(SimpleDisplay::from)
.collect::<Vec<_>>();
let template = LatestNewsTemplate { items };
template.to_response()
}
Err(e) => {
tracing::error!("Failed to execute query: {:?}", e);
HttpResponse::InternalServerError().finish()
Expand Down
9 changes: 9 additions & 0 deletions src/templates/all_news.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use aion_types::rebase::response::SimpleDisplay;
use askama_actix::Template;

#[derive(Template)] // this will generate the code...
#[template(path = "all_news.html")] // using the template in this path, relative
// to the `templates` dir in the crate root
pub struct AllNewsTemplate {
pub items: Vec<SimpleDisplay>,
}
10 changes: 10 additions & 0 deletions src/templates/latest_news.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use aion_types::rebase::response::SimpleDisplay;
//use askama::Template;
use askama_actix::Template;

#[derive(Template)] // this will generate the code...
#[template(path = "latest_news.html")] // using the template in this path, relative
// to the `templates` dir in the crate root
pub struct LatestNewsTemplate {
pub items: Vec<SimpleDisplay>,
}
2 changes: 2 additions & 0 deletions src/templates/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod all_news;
pub mod latest_news;
54 changes: 54 additions & 0 deletions templates/all_news.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Rebase All News</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
margin: 0;
padding: 20px;
}
h1 {
color: #5a5a5a;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #fff;
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
a {
color: #0066cc;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
strong {
color: #333;
}
</style>
</head>
<body>
<h1>All News</h1>
<ul>
{% for item in items %}
<li>
<strong>Title:</strong> {{ item.title }}<br />
<strong>Introduce:</strong> {{ item.introduce }}<br />
<strong>URL:</strong>
<a href="{{ item.url }}">{{ item.url }}</a>
</li>
{% endfor %}
</ul>
</body>
</html>
1 change: 1 addition & 0 deletions templates/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, {{ name }}!
54 changes: 54 additions & 0 deletions templates/latest_news.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Rebase Latest News</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
margin: 0;
padding: 20px;
}
h1 {
color: #5a5a5a;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #fff;
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
a {
color: #0066cc;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
strong {
color: #333;
}
</style>
</head>
<body>
<h1>Latest News</h1>
<ul>
{% for item in items %}
<li>
<strong>Title:</strong> {{ item.title }}<br />
<strong>Introduce:</strong> {{ item.introduce }}<br />
<strong>URL:</strong>
<a href="{{ item.url }}">{{ item.url }}</a>
</li>
{% endfor %}
</ul>
</body>
</html>

0 comments on commit cde44c9

Please sign in to comment.