@@ -5,11 +5,13 @@ import fastifyFormbody from "@fastify/formbody";
55
66import { Liquid } from "liquidjs" ;
77import webpack from "webpack" ;
8+ import { JSONFilePreset } from "lowdb/node" ;
89
910import fs from "node:fs" ;
1011import path from "node:path" ;
1112import url from "node:url" ;
1213import childProcess from "node:child_process" ;
14+ import { randomUUID } from "node:crypto" ;
1315
1416const __filename = url . fileURLToPath ( import . meta. url ) ;
1517const __dirname = path . dirname ( __filename ) ;
@@ -18,6 +20,11 @@ const viewsPath = path.join(__dirname, "views");
1820const staticsPath = path . join ( __dirname , "statics" ) ;
1921const publicPath = path . join ( __dirname , "public" ) ;
2022
23+ const db = await JSONFilePreset ( path . join ( __dirname , "data" , "data.json" ) , {
24+ create : [ ] ,
25+ consume : [ ] ,
26+ } ) ;
27+
2128function buildClientsideAssets ( ) {
2229 if ( ! fs . existsSync ( publicPath ) ) {
2330 fs . mkdirSync ( publicPath ) ;
@@ -81,15 +88,7 @@ app.register(fastifyView, {
8188app . register ( fastifyFormbody ) ;
8289
8390app . get ( getRoutePath ( ) , ( req , reply ) => {
84- // TODO: Replace mock data with content from database
85- let links = {
86- create : [
87- { id : "1" , description : "Foobar (create)" , url : "https://example.com" } ,
88- ] ,
89- consume : [
90- { id : "2" , description : "Foobar (consume)" , url : "https://example.com" } ,
91- ] ,
92- } ;
91+ const links = db . data ;
9392
9493 return reply . view ( "./views/index.liquid" , {
9594 links,
@@ -98,22 +97,51 @@ app.get(getRoutePath(), (req, reply) => {
9897} ) ;
9998
10099app . get ( getRoutePath ( "links" ) , ( req , reply ) => {
101- // get all links
100+ const links = db . data ;
101+
102+ return reply . send ( links ) ;
102103} ) ;
103104
104- app . post ( getRoutePath ( "create" ) , ( req , reply ) => {
105- // Create a new link
105+ app . post ( getRoutePath ( "create" ) , async ( req , reply ) => {
106106 const { type, url, description } = req . body ;
107107
108+ const uuid = randomUUID ( ) ;
109+
110+ db . data [ type ] . push ( {
111+ id : uuid ,
112+ url,
113+ description,
114+ } ) ;
115+ await db . write ( ) ;
116+
108117 reply . redirect ( getRoutePath ( ) ) ;
109118} ) ;
110119
111- app . post ( getRoutePath ( "delete" ) , ( req , reply ) => {
112- // Delete a link
120+ app . post ( getRoutePath ( "delete" ) , async ( req , reply ) => {
121+ console . log ( req . body . id ) ;
122+ let inCreateIdx = db . data . create . findIndex ( ( { id } ) => req . body . id === id ) ;
123+ console . log ( { inCreateIdx } ) ;
124+ if ( inCreateIdx !== - 1 ) {
125+ db . data . create . splice ( inCreateIdx , 1 ) ;
126+
127+ await db . write ( ) ;
128+ return reply . send ( { message : "link deleted" } ) ;
129+ }
130+
131+ let inConsumeIdx = db . data . consume . findIndex ( ( { id } ) => req . body . id === id ) ;
132+ console . log ( { inConsumeIdx } ) ;
133+ if ( inConsumeIdx !== - 1 ) {
134+ db . data . consume . splice ( inConsumeIdx , 1 ) ;
135+ await db . write ( ) ;
136+ return reply . send ( { message : "link deleted" } ) ;
137+ }
113138} ) ;
114139
115- app . post ( getRoutePath ( "delete-all" ) , ( req , reply ) => {
116- // Delete all links
140+ app . post ( getRoutePath ( "delete-all" ) , async ( req , reply ) => {
141+ db . data . create = [ ] ;
142+ db . data . consume = [ ] ;
143+ await db . write ( ) ;
144+ return reply . send ( { message : "Successfully deleted all posts" } ) ;
117145} ) ;
118146
119147app . listen ( { port : 3007 , host : "0.0.0.0" } , ( err , address ) => {
0 commit comments