Updated database, adapted backend. Reading data on an apicall.

This commit is contained in:
Yohan Boujon 2024-03-13 10:38:27 +01:00
parent 36f967998b
commit ac32ff7624
5 changed files with 52 additions and 14 deletions

View file

@ -9,7 +9,7 @@ CREATE TABLE "authors" (
"name" text NOT NULL, "name" text NOT NULL,
"username" text UNIQUE NOT NULL, "username" text UNIQUE NOT NULL,
"password" text NOT NULL, "password" text NOT NULL,
"created_at" time DEFAULT (now()), "created_at" date DEFAULT (now()),
"profile_picture" text, "profile_picture" text,
"bio" text "bio" text
); );
@ -27,7 +27,7 @@ CREATE TABLE "post" (
"lang_id" integer, "lang_id" integer,
"title" varchar, "title" varchar,
"picture" text, "picture" text,
"date" time DEFAULT (now()), "date" date DEFAULT (now()),
"body" text, "body" text,
"views" integer DEFAULT 0, "views" integer DEFAULT 0,
"likes" integer DEFAULT 0, "likes" integer DEFAULT 0,
@ -69,7 +69,7 @@ CREATE TABLE "comment" (
"id" integer UNIQUE PRIMARY KEY NOT NULL, "id" integer UNIQUE PRIMARY KEY NOT NULL,
"post_id" integer NOT NULL, "post_id" integer NOT NULL,
"comment_id" integer, "comment_id" integer,
"date" time DEFAULT (now()), "date" date DEFAULT (now()),
"user" text, "user" text,
"body" text "body" text
); );

View file

@ -3,17 +3,23 @@
#include "crow.h" #include "crow.h"
#include <pqxx/pqxx> #include <pqxx/pqxx>
#include "crow/http_response.h"
#include "dotenv.hpp" #include "dotenv.hpp"
#include "json.hpp" #include "json.hpp"
pqxx::connection* globalConnection(nullptr); pqxx::connection* globalConnection(nullptr);
crow::response test(void); crow::response test(void);
crow::response getSimplePosts(void);
// Json Handler with a precise data type
Etheryo::JsonHandler category; Etheryo::JsonHandler category;
Etheryo::JsonHandler post_info;
int main() int main()
{ {
// Init Json Objects // Init Json Objects
category.add({ "id", "slug", "icon", "type_icon" }); category.add({ "id", "slug", "icon", "type_icon" });
post_info.add({"slug","author","title", "date"});
// Init Postgresql + DotEnv // Init Postgresql + DotEnv
Etheryo::DotEnv dotenvParser; Etheryo::DotEnv dotenvParser;
@ -23,8 +29,8 @@ int main()
// Init Crow App // Init Crow App
crow::SimpleApp app; crow::SimpleApp app;
CROW_ROUTE(app, "/") CROW_ROUTE(app, "/")(test);
(test); CROW_ROUTE(app, "/get_simple_posts")(getSimplePosts);
app.port(8000).multithreaded().run(); app.port(8000).multithreaded().run();
globalConnection->close(); globalConnection->close();
@ -46,4 +52,23 @@ crow::response test(void)
response.add_header("Access-Control-Allow-Origin", "*"); response.add_header("Access-Control-Allow-Origin", "*");
response.add_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); response.add_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
return response; return response;
}
crow::response getSimplePosts()
{
post_info.clear();
pqxx::work worker { *globalConnection };
auto result = worker.query<std::string, int, std::string, std::string, std::string>("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;
} }

View file

@ -0,0 +1,6 @@
.articles-minpost {
box-shadow: #00000040 0px 8px 18px -1px;
padding: 1rem;
margin-left: 2rem;
width: fit-content;
}

View file

@ -1,6 +1,6 @@
export async function load_post() { export async function load(url) {
try { 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) { if (!response.ok) {
return {status: -1}; return {status: -1};

View file

@ -1,15 +1,17 @@
<script> <script>
import '$lib/css/base.css';
import '$lib/css/article.css';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { load_post } from '$lib/js/apicall.js'; import { load } from '$lib/js/apicall.js';
$: isLoaded = false; $: isLoaded = false;
$: hasLoadFailed = false; $: hasLoadFailed = false;
let data; let post_min; // slug, author, title, date
onMount(async () => { onMount(async () => {
const loaded_post = await load_post(); const loaded_post = await load("get_simple_posts");
if (loaded_post.status == 200) { if (loaded_post.status == 200) {
data = loaded_post.data; post_min = loaded_post.data;
isLoaded = true; isLoaded = true;
} else hasLoadFailed = true; } else hasLoadFailed = true;
}); });
@ -18,9 +20,14 @@
<div> <div>
<h1>Etheryo Blog</h1> <h1>Etheryo Blog</h1>
{#if isLoaded} {#if isLoaded}
{#each data as d} <div class="articles-minpost">
<p>id: {d.id}</p> {#each post_min as p}
{/each} <h2>{p.title}</h2>
<p>{p.date}</p>
<p>{p.author}</p>
{/each}
</div>
<!-- debug purpose -->
<div style="height: 100rem;"></div> <div style="height: 100rem;"></div>
{:else if hasLoadFailed} {:else if hasLoadFailed}
<p>Loading failed :c</p> <p>Loading failed :c</p>