diff --git a/backend/sql/etheryo.sql b/backend/sql/etheryo.sql index 10fb359..9d4d310 100644 --- a/backend/sql/etheryo.sql +++ b/backend/sql/etheryo.sql @@ -9,7 +9,7 @@ CREATE TABLE "authors" ( "name" text NOT NULL, "username" text UNIQUE NOT NULL, "password" text NOT NULL, - "created_at" time DEFAULT (now()), + "created_at" date DEFAULT (now()), "profile_picture" text, "bio" text ); @@ -27,7 +27,7 @@ CREATE TABLE "post" ( "lang_id" integer, "title" varchar, "picture" text, - "date" time DEFAULT (now()), + "date" date DEFAULT (now()), "body" text, "views" integer DEFAULT 0, "likes" integer DEFAULT 0, @@ -69,7 +69,7 @@ CREATE TABLE "comment" ( "id" integer UNIQUE PRIMARY KEY NOT NULL, "post_id" integer NOT NULL, "comment_id" integer, - "date" time DEFAULT (now()), + "date" date DEFAULT (now()), "user" text, "body" text ); diff --git a/backend/src/main.cpp b/backend/src/main.cpp index fec20c0..3897d69 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -3,17 +3,23 @@ #include "crow.h" #include +#include "crow/http_response.h" #include "dotenv.hpp" #include "json.hpp" pqxx::connection* globalConnection(nullptr); crow::response test(void); +crow::response getSimplePosts(void); + +// Json Handler with a precise data type Etheryo::JsonHandler category; +Etheryo::JsonHandler post_info; int main() { // Init Json Objects category.add({ "id", "slug", "icon", "type_icon" }); + post_info.add({"slug","author","title", "date"}); // Init Postgresql + DotEnv Etheryo::DotEnv dotenvParser; @@ -23,8 +29,8 @@ int main() // Init Crow App crow::SimpleApp app; - CROW_ROUTE(app, "/") - (test); + CROW_ROUTE(app, "/")(test); + CROW_ROUTE(app, "/get_simple_posts")(getSimplePosts); app.port(8000).multithreaded().run(); globalConnection->close(); @@ -46,4 +52,23 @@ crow::response test(void) response.add_header("Access-Control-Allow-Origin", "*"); response.add_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); return response; +} + +crow::response getSimplePosts() +{ + post_info.clear(); + pqxx::work worker { *globalConnection }; + auto result = worker.query("select p.slug, p.author_id, a.\"name\", p.title, p.\"date\" from post p join authors a on p.author_id = a.id order by p.\"date\";"); + for(auto [slug, author_id, author_name, title, date] : result) + { + post_info["slug"] = slug; + post_info["author"] = author_name; + post_info["title"] = title; + post_info["date"] = date; + post_info.push(); + } + auto response = crow::response { "application/json", post_info.to_str() }; + response.add_header("Access-Control-Allow-Origin", "*"); + response.add_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + return response; } \ No newline at end of file diff --git a/frontend/src/lib/css/article.css b/frontend/src/lib/css/article.css new file mode 100644 index 0000000..685a95d --- /dev/null +++ b/frontend/src/lib/css/article.css @@ -0,0 +1,6 @@ +.articles-minpost { + box-shadow: #00000040 0px 8px 18px -1px; + padding: 1rem; + margin-left: 2rem; + width: fit-content; +} \ No newline at end of file diff --git a/frontend/src/lib/js/apicall.js b/frontend/src/lib/js/apicall.js index 79919b3..876f219 100644 --- a/frontend/src/lib/js/apicall.js +++ b/frontend/src/lib/js/apicall.js @@ -1,6 +1,6 @@ -export async function load_post() { +export async function load(url) { try { - const response = await fetch('http://127.0.0.1:8000'); + const response = await fetch('http://127.0.0.1:8000/'+url); if (!response.ok) { return {status: -1}; diff --git a/frontend/src/routes/articles/+page.svelte b/frontend/src/routes/articles/+page.svelte index f476360..ab2cc1b 100644 --- a/frontend/src/routes/articles/+page.svelte +++ b/frontend/src/routes/articles/+page.svelte @@ -1,15 +1,17 @@