From de148eaf193cecc457cca6e5a33b49636d0c516c Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 28 Jun 2024 09:49:39 +0200 Subject: [PATCH] moving stuff and refactoring --- src/app/api/attachment/route.ts | 11 ++--------- .../attachment/attachmentActions.ts | 13 +++++++++++++ .../actions/entityManagement/attachmentActions.ts | 0 .../entityManagement/{ => post}/postActions.ts | 4 ++-- .../entityManagement/{ => post}/projectActions.ts | 4 ++-- src/components/client/admin/PostEditor.tsx | 2 +- src/components/client/admin/PostTable.tsx | 2 +- src/views/admin/ClientPostView.tsx | 2 +- src/views/admin/PostView.tsx | 4 ++-- src/views/admin/ProjectView.tsx | 4 ++-- 10 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/app/lib/actions/entityManagement/attachment/attachmentActions.ts delete mode 100644 src/app/lib/actions/entityManagement/attachmentActions.ts rename src/app/lib/actions/entityManagement/{ => post}/postActions.ts (96%) rename src/app/lib/actions/entityManagement/{ => post}/projectActions.ts (79%) diff --git a/src/app/api/attachment/route.ts b/src/app/api/attachment/route.ts index 03463ee..666dc26 100644 --- a/src/app/api/attachment/route.ts +++ b/src/app/api/attachment/route.ts @@ -4,9 +4,10 @@ import { APIError, attemptAPIAction } from "@/util/api/error"; import { sequelize, Bucket, Auth, Post, PostTag, Tag, User, dbSync } from "@/models"; import { cookies } from "next/headers"; import { Attachment } from "@/models"; -import { UUID, randomUUID } from "crypto"; +import { UUID } from "crypto"; import { mkdir, mkdirSync, writeFile } from "fs"; import { where } from "@sequelize/core"; +import { addToNewBucketForPost } from "../../lib/actions/entityManagement/attachment/attachmentActions"; async function writeFilesToFS(uuid: UUID, files: any[]): Promise<{ success: boolean, filePaths: string[] }> { @@ -49,14 +50,6 @@ async function writeFilesToFS(uuid: UUID, files: any[]): Promise<{ success: bool } } -async function addToNewBucketForPost(postid: number): Promise { - Post.sync(); - const post = await Post.findOne({ where: { id: postid } }); - - if (!post) throw new APIError({ status: 500, responseText: "invalid postid" }); - const bucket = await post.createBucket({ id: randomUUID() }); - return bucket; -} async function addToExistingBucket(bucketid: number): Promise { const bucket = await Bucket.findOne({ where: { id: bucketid }, include: { association: Bucket.associations.posts } diff --git a/src/app/lib/actions/entityManagement/attachment/attachmentActions.ts b/src/app/lib/actions/entityManagement/attachment/attachmentActions.ts new file mode 100644 index 0000000..e91a90a --- /dev/null +++ b/src/app/lib/actions/entityManagement/attachment/attachmentActions.ts @@ -0,0 +1,13 @@ +'use server'; +import { APIError } from "@/util/api/error"; +import { Bucket, Post } from "@/models"; +import { randomUUID } from "crypto"; + +export async function addToNewBucketForPost(postid: number): Promise { + Post.sync(); + const post = await Post.findOne({ where: { id: postid } }); + + if (!post) throw new APIError({ status: 500, responseText: "invalid postid" }); + const bucket = await post.createBucket({ id: randomUUID() }); + return bucket; +} diff --git a/src/app/lib/actions/entityManagement/attachmentActions.ts b/src/app/lib/actions/entityManagement/attachmentActions.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/lib/actions/entityManagement/postActions.ts b/src/app/lib/actions/entityManagement/post/postActions.ts similarity index 96% rename from src/app/lib/actions/entityManagement/postActions.ts rename to src/app/lib/actions/entityManagement/post/postActions.ts index ed881fb..f2cba7d 100644 --- a/src/app/lib/actions/entityManagement/postActions.ts +++ b/src/app/lib/actions/entityManagement/post/postActions.ts @@ -3,8 +3,8 @@ import { revalidatePath, revalidateTag } from 'next/cache' import { Bucket, Post, PostBucket, Project, User, dbSync } from "@/models"; import { Attributes, where } from "@sequelize/core"; -import { getCookieAuth, userIsAdmin } from '../actions'; -import { ActionResult } from '../ActionResult'; +import { getCookieAuth, userIsAdmin } from '../../actions'; +import { ActionResult } from '../../ActionResult'; import { PostAttributesWithBuckets } from '@/models'; export async function deletePost(postID: number): Promise> { diff --git a/src/app/lib/actions/entityManagement/projectActions.ts b/src/app/lib/actions/entityManagement/post/projectActions.ts similarity index 79% rename from src/app/lib/actions/entityManagement/projectActions.ts rename to src/app/lib/actions/entityManagement/post/projectActions.ts index ce34097..01dff89 100644 --- a/src/app/lib/actions/entityManagement/projectActions.ts +++ b/src/app/lib/actions/entityManagement/post/projectActions.ts @@ -1,8 +1,8 @@ 'use server'; import { Project } from "@/models"; -import { ActionResult } from "../ActionResult"; +import { ActionResult } from "../../ActionResult"; import { Attributes } from "@sequelize/core"; -import { userIsAdmin } from "../actions"; +import { userIsAdmin } from "../../actions"; export async function getProjects(): Promise[]>> { if (! await userIsAdmin()) return { error: 'Unauthorized, not fetching Projects' } diff --git a/src/components/client/admin/PostEditor.tsx b/src/components/client/admin/PostEditor.tsx index 3e27c66..6591e51 100644 --- a/src/components/client/admin/PostEditor.tsx +++ b/src/components/client/admin/PostEditor.tsx @@ -1,4 +1,4 @@ -import { GetPostsAttributes } from "@/app/lib/actions/entityManagement/postActions"; +import { GetPostsAttributes } from "@/app/lib/actions/entityManagement/post/postActions"; import { Post, Project, Bucket, PostBucket, Attachment } from "@/models"; import { Attributes } from "@sequelize/core"; import { UUID } from "crypto"; diff --git a/src/components/client/admin/PostTable.tsx b/src/components/client/admin/PostTable.tsx index 7d858c6..d411c32 100644 --- a/src/components/client/admin/PostTable.tsx +++ b/src/components/client/admin/PostTable.tsx @@ -11,7 +11,7 @@ import { getPostsWithBucketsAndProject, GetPostsAttributes, PostServerActions, -} from "@/app/lib/actions/entityManagement/postActions"; +} from "@/app/lib/actions/entityManagement/post/postActions"; import { PostViewProps } from "@/views/admin/ClientPostView"; import { aifa } from "@/util/Utils"; import { StateHook } from "@/util/state/stateUtils"; diff --git a/src/views/admin/ClientPostView.tsx b/src/views/admin/ClientPostView.tsx index 42eb2f5..789c3fd 100644 --- a/src/views/admin/ClientPostView.tsx +++ b/src/views/admin/ClientPostView.tsx @@ -6,7 +6,7 @@ import PostTable, { import { GetPostsAttributes, PostServerActions, -} from "@/app/lib/actions/entityManagement/postActions"; +} from "@/app/lib/actions/entityManagement/post/postActions"; import { EditorState } from "@/components/client/admin/PostEditor"; import { Project } from "@/models"; import { Dispatch, ReactNode, SetStateAction, useState } from "react"; diff --git a/src/views/admin/PostView.tsx b/src/views/admin/PostView.tsx index 080407e..8e7b6e8 100644 --- a/src/views/admin/PostView.tsx +++ b/src/views/admin/PostView.tsx @@ -9,8 +9,8 @@ import { updatePost, GetPostsAttributes, PostServerActions, -} from "@/app/lib/actions/entityManagement/postActions"; -import { getProjects } from "@/app/lib/actions/entityManagement/projectActions"; +} from "@/app/lib/actions/entityManagement/post/postActions"; +import { getProjects } from "@/app/lib/actions/entityManagement/post/projectActions"; import { Bucket, Project, Post, dbSync, Attachment } from "@/models"; import { handleActionResult } from "../../app/lib/actions/clientActionHandler"; import { ClientPostView } from "./ClientPostView"; diff --git a/src/views/admin/ProjectView.tsx b/src/views/admin/ProjectView.tsx index b150399..7230d4c 100644 --- a/src/views/admin/ProjectView.tsx +++ b/src/views/admin/ProjectView.tsx @@ -9,8 +9,8 @@ import { deletePost, getPostsWithBucketsAndProject, updatePost, -} from "@/app/lib/actions/entityManagement/postActions"; -import { getProjects } from "@/app/lib/actions/entityManagement/projectActions"; +} from "@/app/lib/actions/entityManagement/post/postActions"; +import { getProjects } from "@/app/lib/actions/entityManagement/post/projectActions"; import { Bucket, Project, Post, dbSync, Attachment } from "@/models"; import { tryCreateAttachment } from "@/app/api/attachment/route"; import { Attributes } from "@sequelize/core";