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 { Bucket, Post, PostBucket, User, dbSync } from "@/model/Models";
import { Attributes, where } from "@sequelize/core";
import { cookies } from 'next/headers';
import { getCookieAuth, userIsAdmin } from '../actions';
import { ActionResult } from '../ActionResult';
import { PostAttributesWithBuckets } from '@/model/Post';
export async function deletePost(postID: number): Promise<ActionResult<boolean>> {
await dbSync;
// revalidatePath("/admin/man-post","page")
@ -20,16 +15,14 @@ export async function deletePost(postID: number): Promise<ActionResult<boolean>>
return {result: true};
}
export async function getPostsWithBuckets(): Promise<ActionResult<PostAttributesWithBuckets[]>> {
export async function getPostsWithBucketsAndProject(): Promise<ActionResult<PostAttributesWithBuckets[]>> {
await dbSync;
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))};
}
export async function getPosts(): Promise<ActionResult<Attributes<Post>[]>> {
await dbSync;
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))};
}
export async function updatePost(postAttributes: Partial<Attributes<Post>>): Promise<ActionResult<Attributes<Post>[]>> {
await dbSync;
if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."}

View File

@ -9,6 +9,7 @@ type TableGenOptions = {
type Props = {
headings:Array<string>;
children:ReactNode;
entityName:string;
options?:TableGenOptions
}
@ -16,6 +17,7 @@ export default function EntityManagementTable(props:Props){
const options = props.options ? props.options : {editButton: true, deleteButton: true}
console.log(props.headings)
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">
<thead>
<tr>

View File

@ -20,7 +20,7 @@ import { useState } from "react";
import { Project, Post, Bucket } from "@/model/Models";
import { ActionResult } from "@/app/lib/actions/ActionResult";
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";
type Actions = {
@ -100,7 +100,7 @@ export default function PostTable(props: Props) {
project_id: e.project_id
})
.then(res => handleActionResult(res))
.then(getPostsWithBuckets)
.then(getPostsWithBucketsAndProject)
.then(res => {
if (!res.error && res.result) setPosts(res.result);
})
@ -169,7 +169,7 @@ export default function PostTable(props: Props) {
}
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}
postData={postData}
key={postData.id}/> )}

View File

@ -5,7 +5,7 @@ import { constructAPIUrl } from "@/util/Utils";
import { ReactNode, useEffect } from "react";
import EntityManagementTable from "../../../client/EntityManagementTable";
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 { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
import { tryCreateAttachment } from "@/app/api/attachment/route";
@ -13,22 +13,20 @@ import { tryCreateAttachment } from "@/app/api/attachment/route";
type Props = {
children?:ReactNode
}
async function getHeadings(){
let headings:string[] = ['#', 'Title', 'Content', 'Project', 'CreatedAt', 'UpdatedAt']
return headings
}
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 = {
deletePost: deletePost,
getPosts:getPostsWithBuckets,
getPosts:getPostsWithBucketsAndProject,
getProjects:getProjects,
savePost:updatePost,
// uploadAttachment:tryCreateAttachment
@ -39,7 +37,6 @@ export default async function PostView(props:Props){
return (
<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">
<PostTable data={posts}
projects={projects}

View File

@ -5,7 +5,7 @@ import { constructAPIUrl } from "@/util/Utils";
import { ReactNode, useEffect } from "react";
import EntityManagementTable from "../../../client/EntityManagementTable";
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 { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
import { tryCreateAttachment } from "@/app/api/attachment/route";
@ -16,9 +16,17 @@ type Props = {
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){
await dbSync;
const sync = await dbSync;
// const headings = [
// '#',
@ -30,7 +38,6 @@ export default async function ProjectView(props:Props){
// 'Edit',
// 'Delete',
// ]
const headings = Project.getAttributes();
// const actions = {
// deletePost: deletePost,
// 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 projects = await Project.findAll().then(projects=>projects.map((e)=>JSON.parse(JSON.stringify(e))));
const headings = await getHeadings();
return <>
<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
<div className="w-[100%] m-auto">
{(()=>{
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>
})()}
<EntityManagementTable entityName="Project" headings={headings}><></></EntityManagementTable>
{/* <PostTable data={posts}
projects={projects}
headings={headings}

View File

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

View File

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