diff --git a/justfile b/justfile index 693ee5b..ff58177 100644 --- a/justfile +++ b/justfile @@ -2,3 +2,5 @@ fake: POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/gen_fake.ts fetch_post: POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/post_db.ts +create_schema: + POSTGRES_URL="postgres://test:test@192.168.50.14:5432/posts" bun run ./tools/create_schema.ts diff --git a/src/app/new_post/route.ts b/src/app/new_post/route.ts new file mode 100644 index 0000000..39e193a --- /dev/null +++ b/src/app/new_post/route.ts @@ -0,0 +1,15 @@ +import { insert_post, NewPost } from "@/db"; +import { Blob } from "buffer"; +import type { NextRequest } from "next/server"; + +export async function POST(req: NextRequest) { + let form = await req.formData(); + let text = form.get("content")?.toString() + if (!text) { + return Response.error() + } + let request: NewPost = { content: text, image: [] }; + let [id, hash] = await insert_post(request); + + return Response.json({ id: id, hash: hash }) +} diff --git a/src/app/new_user/route.ts b/src/app/new_user/route.ts new file mode 100644 index 0000000..d72f3d5 --- /dev/null +++ b/src/app/new_user/route.ts @@ -0,0 +1,6 @@ +import { NextRequest } from "next/server"; + +export function Post(req: NextRequest) { + + +} diff --git a/src/db.ts b/src/db.ts index af1b730..cd8bb1a 100644 --- a/src/db.ts +++ b/src/db.ts @@ -1,4 +1,4 @@ -import { env, ReservedSQL, sql } from 'bun' +import { env, ReservedSQL, CryptoHasher, sql } from 'bun' import { MIMEType } from 'util'; export enum MultiMediaType { @@ -39,20 +39,40 @@ const SQLPostCast2Post = (obj: SQLPostCast) => { return x } -export interface Cursor { - hash: string, - post_time: string, + +export interface NewPost { + image: Blob[] + content: string, } +export async function insert_post(post: NewPost): Promise<[number, string]> { + let post_hash = new CryptoHasher("sha256"); + post_hash.update(post.content); + post_hash.update(Date.now().toString()) + let populated = post_hash.digest("base64"); + let [{ id, hash }] = await sql`INSERT INTO niming.posts (post, hash) VALUES (${post.content}, ${populated}) RETURNING id, hash` + return [id, hash] + +} + +export async function init_db() { + await sql.begin(async sql => { + await sql`CREATE SCHEMA niming` + await sql`CREATE TABLE niming.posts (id SERIAL PRIMARY KEY, hash char(44) UNIQUE, post VARCHAR(500), post_time TIMESTAMPTZ DEFAULT now())` + await sql`CREATE TABLE niming.images (id INTEGER PRIMARY KEY REFERENCES niming.posts (id), fileid integer[] )` + }) +} export class PostFetcher { conn: Promise; + constructor() { this.conn = sql.reserve(); } + async init() { await this.conn.then(async e => { - await e`DECLARE post_ptr CURSOR WITH HOLD FOR SELECT niming.posts.*, niming.images.file_sequence + await e`DECLARE post_ptr CURSOR WITH HOLD FOR SELECT niming.posts.*, niming.images.fileid AS images FROM niming.posts LEFT JOIN niming.images ON niming.images.hash=niming.posts.hash ORDER BY niming.posts.post_time DESC, niming.posts.hash ASC` }).catch(err => { diff --git a/tools/create_schema.ts b/tools/create_schema.ts new file mode 100644 index 0000000..9ba13ed --- /dev/null +++ b/tools/create_schema.ts @@ -0,0 +1,3 @@ +import { init_db } from "@/db"; + +init_db() diff --git a/tools/gen_fake.ts b/tools/gen_fake.ts index 61af1d6..51b4224 100644 --- a/tools/gen_fake.ts +++ b/tools/gen_fake.ts @@ -1,15 +1,13 @@ -import { CryptoHasher, sql } from "bun"; -import { retrieve_post, setup_func } from "@/db"; +import { insert_post, NewPost } from '@/db'; import { faker } from '@faker-js/faker'; -for (let i = 0; i < 100; i++) { + +for (let i = 0; i < 100; i++) { let text = faker.string.alpha(20); - let hash = new CryptoHasher("sha256") - hash.update(text) - hash.update(Date.now().toString()) - let dg = hash.digest("hex") - console.log(dg) - const _ = await sql`INSERT INTO niming.posts (hash, post,post_time) VALUES (${dg},${text}, now())` + let x: NewPost = { image: [], content: text }; + let [p_id, p_hash] = await insert_post(x) + console.log(p_id, p_hash) + }