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,
"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
);

View file

@ -3,17 +3,23 @@
#include "crow.h"
#include <pqxx/pqxx>
#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();
@ -47,3 +53,22 @@ crow::response test(void)
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<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 {
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};

View file

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