diff --git a/src/app/api/attachment/route.ts b/src/app/api/attachment/route.ts index 462c17a..4464253 100644 --- a/src/app/api/attachment/route.ts +++ b/src/app/api/attachment/route.ts @@ -141,19 +141,9 @@ export async function tryCreateAttachment(request: Request) { export async function tryFetchAttachments(request: Request) { await Post.sync(); - const foundPosts = await Post.findAll({ - include: [ - { - association: Post.associations.user, - attributes: { exclude: ["password", "createdAt", "updatedAt"] }, - }, - { - association: Post.associations.postTags, - }, - ], - }); + const foundAttachments = await Attachment.findAll(); - return new Response(JSON.stringify(foundPosts), { status: 200 }); + return new Response(JSON.stringify(foundAttachments), { status: 200 }); } export async function GET(request: Request) { diff --git a/src/app/api/setupDB/route.ts b/src/app/api/setupDB/route.ts index 4b51a4b..98618d7 100644 --- a/src/app/api/setupDB/route.ts +++ b/src/app/api/setupDB/route.ts @@ -4,24 +4,24 @@ import { cookies } from "next/headers"; import { APIError } from "@/util/api/error"; import { - Attachment, - Auth, - Bucket, - DBState, - Post, - PostTag, - Project, - Tag, - User, - UserPerms, - dbSync, - sequelize, + Attachment, + Auth, + Bucket, + DBState, + Post, + PostTag, + Project, + Tag, + User, + UserPerms, + dbSync, + sequelize, } from "@/models"; import Sequelize, { CreationAttributes, DataTypes } from "@sequelize/core"; import { - SqliteColumnsDescription, - SqliteDialect, - SqliteQueryInterface, + SqliteColumnsDescription, + SqliteDialect, + SqliteQueryInterface, } from "@sequelize/sqlite3"; import { hashpassword } from "@/util/auth"; import { copyFile, readFileSync } from "fs"; @@ -30,160 +30,158 @@ import { UUID } from "crypto"; import { runApiAction } from "@/util/api"; async function seedUsers(qif: SqliteQueryInterface) { - const fp = path.resolve("./db/seed/users.json"); - const json: { users: CreationAttributes[] } = JSON.parse( - Buffer.from(readFileSync(fp).valueOf()).toString() - ); + const fp = path.resolve("./db/seed/users.json"); + const json: { users: CreationAttributes[] } = JSON.parse( + Buffer.from(readFileSync(fp).valueOf()).toString() + ); - const users = json.users.map(async (user) => { - user.password = await hashpassword(user.password); - return user; - }); + const users = json.users.map(async (user) => { + user.password = await hashpassword(user.password); + return user; + }); - const dbUsers = await User.bulkCreate(await Promise.all(users), { - include: User.associations.perms, - }); + const dbUsers = await User.bulkCreate(await Promise.all(users), { + include: User.associations.perms, + }); } async function seedPosts(qif: SqliteQueryInterface) { - const fp = path.resolve("./db/seed/posts.json"); - const json: { users: CreationAttributes[] } = JSON.parse( - Buffer.from(readFileSync(fp).valueOf()).toString() - ); - const projects = [ - { - name: "Blog", - readableIdentifier: "blog", - posts: [ - { - title: "Test Post", - content: - "# Hello \nthis is some **test** markdown and we make some edits\n![](/attachment/788dfc19-55ba-482c-8124-277702296dfb/FB_IMG_1716665756868.jpg)", - description: "A new post to test the blog system", - project_id: 1, - user_id: 1, - buckets: [ - { - id: "788dfc19-55ba-482c-8124-277702296dfb", - attachments: [ - { - filename: "FB_IMG_1716665756868.jpg", - }, - ], - }, - ], - }, - ], - }, - ]; - projects.map((project) => { - Project.create({ - name: project.name, - readableIdentifier: project.readableIdentifier, - }); - project.posts.map(async (post) => { - const pst = await Post.create({ - title: post.title, - content: post.content, - description: post.description, - project_id: post.project_id, - user_id: post.user_id, - }); - post.buckets.map(async (bucket) => { - pst.createBucket({ id: bucket.id as UUID }); - bucket.attachments.map((attachment) => { - Attachment.create({ - bucket_id: bucket.id as UUID, - filename: attachment.filename, - }).then((a) => { - copyFile( - path.resolve(".", "db", "seed", "post", a.filename), - path.resolve(".", "bucket", bucket.id, a.filename), - () => {} - ); - }); - }); - }); - }); - }); + const fp = path.resolve("./db/seed/posts.json"); + const json: { users: CreationAttributes[] } = JSON.parse( + Buffer.from(readFileSync(fp).valueOf()).toString() + ); + const projects = [ + { + name: "Blog", + readableIdentifier: "blog", + posts: [ + { + title: "Test Post", + content: + "# Hello \nthis is some **test** markdown and we make some edits\n![](/attachment/788dfc19-55ba-482c-8124-277702296dfb/FB_IMG_1716665756868.jpg)", + description: "A new post to test the blog system", + project_id: 1, + user_id: 1, + buckets: [ + { + id: "788dfc19-55ba-482c-8124-277702296dfb", + attachments: [ + { + filename: "FB_IMG_1716665756868.jpg", + }, + ], + }, + ], + }, + ], + }, + ]; + projects.map((project) => { + Project.create({ + name: project.name, + readableIdentifier: project.readableIdentifier, + }); + project.posts.map(async (post) => { + const pst = await Post.create({ + title: post.title, + content: post.content, + description: post.description, + project_id: post.project_id, + user_id: post.user_id, + }); + post.buckets.map(async (bucket) => { + pst.createBucket({ id: bucket.id as UUID }); + bucket.attachments.map((attachment) => { + Attachment.create({ + bucket_id: bucket.id as UUID, + filename: attachment.filename, + }).then((a) => { + copyFile( + path.resolve(".", "db", "seed", "post", a.filename), + path.resolve(".", "bucket", bucket.id, a.filename), + () => {} + ); + }); + }); + }); + }); + }); - const project = await Project.findOne({ - where: { - readableIdentifier: "blog", - }, - }).then((e) => - e - ? e - : Project.create({ - name: "General Blog", - readableIdentifier: "blog", - }) - ); + const project = await Project.findOne({ + where: { + readableIdentifier: "blog", + }, + }).then( + (project) => + project || + Project.create({ + name: "General Blog", + readableIdentifier: "blog", + }) + ); } async function seedDatabase(qif: SqliteQueryInterface) { - await seedUsers(qif); - await seedPosts(qif); + await seedUsers(qif); + await seedPosts(qif); - // await Post.create({ - // title: 'Test Post', - // content: ` - // # Hello - // this is some **test** markdown - // `, - // project_id: project.id, - // user_id: user.id - // }) - // await Post.create({ - // title: 'Test Post 2', - // content: ` - // # Hello - // this is amother post with some **test** markdown - // `, - // project_id: project.id, - // user_id: user.id - // }) + // await Post.create({ + // title: 'Test Post', + // content: ` + // # Hello + // this is some **test** markdown + // `, + // project_id: project.id, + // user_id: user.id + // }) + // await Post.create({ + // title: 'Test Post 2', + // content: ` + // # Hello + // this is amother post with some **test** markdown + // `, + // project_id: project.id, + // user_id: user.id + // }) } export async function GET(request: Request) { - runApiAction(async (request) => { - await dbSync; + runApiAction(async (request) => { + await dbSync; - const queryInterface = sequelize.queryInterface; + const queryInterface = sequelize.queryInterface; - const version = (await DBState.findAll()) - .sort((a, b) => (a.version > b.version ? 1 : -1)) - .map((a) => a.version)[0]; + const version = (await DBState.findAll()) + .sort((a, b) => (a.version > b.version ? 1 : -1)) + .map((a) => a.version)[0]; - switch (version) { - case 1: - break; - default: - seedDatabase(queryInterface); - const postsRows: SqliteColumnsDescription = await queryInterface - .describeTable(Post.table.tableName) - .then((t) => t); - if (!postsRows["project_id"]) - queryInterface.addColumn("Posts", "project_id", { - type: DataTypes.INTEGER, - acceptsNull: () => false, - defaultValue: 1, - }); - break; - } + switch (version) { + case 1: + break; + default: + seedDatabase(queryInterface); + const postsRows: SqliteColumnsDescription = await queryInterface + .describeTable(Post.table.tableName) + .then((t) => t); + if (!postsRows["project_id"]) + queryInterface.addColumn("Posts", "project_id", { + type: DataTypes.INTEGER, + acceptsNull: () => false, + defaultValue: 1, + }); + break; + } - return new Response( - JSON.stringify({ - test: await queryInterface - .describeTable("Posts") - .then((t) => t), - }), - { - status: 200, - headers: { - "Content-Type": "text/JSON", - }, - } - ); - }, request); + return new Response( + JSON.stringify({ + test: await queryInterface.describeTable("Posts").then((t) => t), + }), + { + status: 200, + headers: { + "Content-Type": "text/JSON", + }, + } + ); + }, request); }