Refactoring

This commit is contained in:
Andreas 2024-06-25 11:32:06 +02:00
parent 1e8a12789d
commit a492bf4ac0
7 changed files with 36 additions and 45 deletions

View File

@ -3,15 +3,10 @@
import { revalidatePath, revalidateTag } from 'next/cache' import { revalidatePath, revalidateTag } from 'next/cache'
import { Bucket, Post, PostBucket, User, dbSync } from "@/model/Models"; import { Bucket, Post, PostBucket, User, dbSync } from "@/model/Models";
import { Attributes, where } from "@sequelize/core"; import { Attributes, where } from "@sequelize/core";
import { cookies } from 'next/headers';
import { getCookieAuth, userIsAdmin } from '../actions'; import { getCookieAuth, userIsAdmin } from '../actions';
import { ActionResult } from '../ActionResult'; import { ActionResult } from '../ActionResult';
import { PostAttributesWithBuckets } from '@/model/Post'; import { PostAttributesWithBuckets } from '@/model/Post';
export async function deletePost(postID: number): Promise<ActionResult<boolean>> { export async function deletePost(postID: number): Promise<ActionResult<boolean>> {
await dbSync; await dbSync;
// revalidatePath("/admin/man-post","page") // revalidatePath("/admin/man-post","page")
@ -20,16 +15,14 @@ export async function deletePost(postID: number): Promise<ActionResult<boolean>>
return {result: true}; return {result: true};
} }
export async function getPostsWithBucketsAndProject(): Promise<ActionResult<PostAttributesWithBuckets[]>> {
export async function getPostsWithBuckets(): Promise<ActionResult<PostAttributesWithBuckets[]>> {
await dbSync; await dbSync;
if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."} if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."}
const posts = await Post.findAll({include: {association: Post.associations.buckets, include: Bucket.associations.attachments}},); const posts = await Post.findAll({include: [{association: Post.associations.buckets, include: Bucket.associations.attachments}, {association: Post.associations.project}], nest: false});
console.dir(JSON.parse(JSON.stringify(posts)),{depth: 10, colors: true})
return {result:JSON.parse(JSON.stringify(posts))}; return {result:JSON.parse(JSON.stringify(posts))};
} }
export async function getPosts(): Promise<ActionResult<Attributes<Post>[]>> { export async function getPosts(): Promise<ActionResult<Attributes<Post>[]>> {
await dbSync; await dbSync;
if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."} if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."}
@ -37,7 +30,6 @@ export async function getPosts(): Promise<ActionResult<Attributes<Post>[]>> {
return {result:JSON.parse(JSON.stringify(posts))}; return {result:JSON.parse(JSON.stringify(posts))};
} }
export async function updatePost(postAttributes: Partial<Attributes<Post>>): Promise<ActionResult<Attributes<Post>[]>> { export async function updatePost(postAttributes: Partial<Attributes<Post>>): Promise<ActionResult<Attributes<Post>[]>> {
await dbSync; await dbSync;
if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."} if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."}

View File

@ -9,6 +9,7 @@ type TableGenOptions = {
type Props = { type Props = {
headings:Array<string>; headings:Array<string>;
children:ReactNode; children:ReactNode;
entityName:string;
options?:TableGenOptions options?:TableGenOptions
} }
@ -16,6 +17,7 @@ export default function EntityManagementTable(props:Props){
const options = props.options ? props.options : {editButton: true, deleteButton: true} const options = props.options ? props.options : {editButton: true, deleteButton: true}
console.log(props.headings) console.log(props.headings)
return <> return <>
<span className="flex flex-row flex-grow w-[100%] pl-2 pr-2"><h1 className="p-2 inline-block">{props.entityName} Management</h1><section className="flex-grow"></section><button className='btn btn-success h-12 mt-auto mb-auto self-end'>New</button></span>
<table className="table overflow-scroll"> <table className="table overflow-scroll">
<thead> <thead>
<tr> <tr>

View File

@ -20,7 +20,7 @@ import { useState } from "react";
import { Project, Post, Bucket } from "@/model/Models"; import { Project, Post, Bucket } from "@/model/Models";
import { ActionResult } from "@/app/lib/actions/ActionResult"; import { ActionResult } from "@/app/lib/actions/ActionResult";
import { handleActionResult } from "@/app/lib/actions/clientActionHandler"; import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
import { getPostsWithBuckets } from "@/app/lib/actions/entityManagement/postActions"; import { getPostsWithBucketsAndProject } from "@/app/lib/actions/entityManagement/postActions";
import { PostAttributesWithBuckets } from "@/model/Post"; import { PostAttributesWithBuckets } from "@/model/Post";
type Actions = { type Actions = {
@ -100,7 +100,7 @@ export default function PostTable(props: Props) {
project_id: e.project_id project_id: e.project_id
}) })
.then(res => handleActionResult(res)) .then(res => handleActionResult(res))
.then(getPostsWithBuckets) .then(getPostsWithBucketsAndProject)
.then(res => { .then(res => {
if (!res.error && res.result) setPosts(res.result); if (!res.error && res.result) setPosts(res.result);
}) })
@ -169,7 +169,7 @@ export default function PostTable(props: Props) {
} }
return <> return <>
<EntityManagementTable key="der-table" headings={props.headings}> <EntityManagementTable entityName="Post" key="der-table" headings={props.headings}>
{posts.map((postData: Partial<PostAttributesWithBuckets>) => <TableRow headings={props.headings} {posts.map((postData: Partial<PostAttributesWithBuckets>) => <TableRow headings={props.headings}
postData={postData} postData={postData}
key={postData.id}/> )} key={postData.id}/> )}

View File

@ -5,7 +5,7 @@ import { constructAPIUrl } from "@/util/Utils";
import { ReactNode, useEffect } from "react"; import { ReactNode, useEffect } from "react";
import EntityManagementTable from "../../../client/EntityManagementTable"; import EntityManagementTable from "../../../client/EntityManagementTable";
import PostTable from "@/components/client/admin/PostTable"; import PostTable from "@/components/client/admin/PostTable";
import { deletePost, getPostsWithBuckets, updatePost } from "@/app/lib/actions/entityManagement/postActions"; import { deletePost, getPostsWithBucketsAndProject, updatePost } from "@/app/lib/actions/entityManagement/postActions";
import { getProjects } from "@/app/lib/actions/entityManagement/projectActions"; import { getProjects } from "@/app/lib/actions/entityManagement/projectActions";
import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models"; import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
import { tryCreateAttachment } from "@/app/api/attachment/route"; import { tryCreateAttachment } from "@/app/api/attachment/route";
@ -13,22 +13,20 @@ import { tryCreateAttachment } from "@/app/api/attachment/route";
type Props = { type Props = {
children?:ReactNode children?:ReactNode
} }
async function getHeadings(){
let headings:string[] = ['#', 'Title', 'Content', 'Project', 'CreatedAt', 'UpdatedAt']
return headings
}
export default async function PostView(props:Props){ export default async function PostView(props:Props){
await dbSync; const sync = await dbSync;
const headings = await getHeadings();
const headings = [
'#',
'Title',
'Content',
'Project',
'Date Created',
'Date Modified',
]
const actions = { const actions = {
deletePost: deletePost, deletePost: deletePost,
getPosts:getPostsWithBuckets, getPosts:getPostsWithBucketsAndProject,
getProjects:getProjects, getProjects:getProjects,
savePost:updatePost, savePost:updatePost,
// uploadAttachment:tryCreateAttachment // uploadAttachment:tryCreateAttachment
@ -39,7 +37,6 @@ export default async function PostView(props:Props){
return ( return (
<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll"> <div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
<span className="flex flex-row flex-grow w-[100%] pl-2 pr-2"><h1 className="p-2 inline-block">Post Management</h1><section className="flex-grow"></section><button className='btn btn-success h-12 mt-auto mb-auto self-end'>New</button></span>
<div className="w-[100%] m-auto"> <div className="w-[100%] m-auto">
<PostTable data={posts} <PostTable data={posts}
projects={projects} projects={projects}

View File

@ -5,7 +5,7 @@ import { constructAPIUrl } from "@/util/Utils";
import { ReactNode, useEffect } from "react"; import { ReactNode, useEffect } from "react";
import EntityManagementTable from "../../../client/EntityManagementTable"; import EntityManagementTable from "../../../client/EntityManagementTable";
import PostTable from "@/components/client/admin/PostTable"; import PostTable from "@/components/client/admin/PostTable";
import { deletePost, getPostsWithBuckets, updatePost } from "@/app/lib/actions/entityManagement/postActions"; import { deletePost, getPostsWithBucketsAndProject, updatePost } from "@/app/lib/actions/entityManagement/postActions";
import { getProjects } from "@/app/lib/actions/entityManagement/projectActions"; import { getProjects } from "@/app/lib/actions/entityManagement/projectActions";
import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models"; import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
import { tryCreateAttachment } from "@/app/api/attachment/route"; import { tryCreateAttachment } from "@/app/api/attachment/route";
@ -16,9 +16,17 @@ type Props = {
children?:ReactNode children?:ReactNode
} }
async function getHeadings(){
let headings:string[] = []
for (const key in Project.getAttributes())
{
headings.push(key)
}
return headings
}
export default async function ProjectView(props:Props){ export default async function ProjectView(props:Props){
await dbSync; const sync = await dbSync;
// const headings = [ // const headings = [
// '#', // '#',
@ -30,7 +38,6 @@ export default async function ProjectView(props:Props){
// 'Edit', // 'Edit',
// 'Delete', // 'Delete',
// ] // ]
const headings = Project.getAttributes();
// const actions = { // const actions = {
// deletePost: deletePost, // deletePost: deletePost,
// getPosts:getPostsWithBuckets, // getPosts:getPostsWithBuckets,
@ -41,19 +48,11 @@ export default async function ProjectView(props:Props){
const posts:Post[] = await Post.findAll({include: {model: Bucket, include: {model: Attachment}}}).then(posts=>posts.map((e)=>JSON.parse(JSON.stringify(e)))); const posts:Post[] = await Post.findAll({include: {model: Bucket, include: {model: Attachment}}}).then(posts=>posts.map((e)=>JSON.parse(JSON.stringify(e))));
const projects = await Project.findAll().then(projects=>projects.map((e)=>JSON.parse(JSON.stringify(e)))); const projects = await Project.findAll().then(projects=>projects.map((e)=>JSON.parse(JSON.stringify(e))));
const headings = await getHeadings();
return <> return <>
<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll"> <div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
<div className="w-[100%] m-auto"> <div className="w-[100%] m-auto">
{(()=>{ <EntityManagementTable entityName="Project" headings={headings}><></></EntityManagementTable>
let a:string[] = []
for (const key in headings)
{
a.push(key)
}
// return a.map((e)=> <div key={e}>{e}</div> );
return <EntityManagementTable headings={a}><></></EntityManagementTable>
})()}
{/* <PostTable data={posts} {/* <PostTable data={posts}
projects={projects} projects={projects}
headings={headings} headings={headings}

View File

@ -2,7 +2,7 @@ import { Auth } from './Auth';
import { Attachment } from './Attachment'; import { Attachment } from './Attachment';
import { Bucket } from './Bucket'; import { Bucket } from './Bucket';
import { DBState } from './DBState'; import { DBState } from './DBState';
import { Post, PostBucket } from './Post'; import { addPostScopes, Post, PostBucket } from './Post';
import { PostTag } from './PostTag'; import { PostTag } from './PostTag';
import { Project } from './Project'; import { Project } from './Project';
import { Tag } from './Tag'; import { Tag } from './Tag';
@ -23,6 +23,7 @@ const sequelize = new Sequelize({
const dbSync = (async ()=> await sequelize.sync())().then(()=>{ const dbSync = (async ()=> await sequelize.sync())().then(()=>{
addUserScopes(); addUserScopes();
addUserPermsScopes(); addUserPermsScopes();
addPostScopes();
}); });

View File

@ -95,8 +95,8 @@ export class PostBucket extends Model<InferAttributes<PostBucket>, InferCreation
declare post?:NonAttribute<Post> declare post?:NonAttribute<Post>
declare bucket?:NonAttribute<Bucket> declare bucket?:NonAttribute<Bucket>
}
export function addPostScopes(){
Post.addScope('withProject', {include: [{association: Post.associations.project}]})
}
}