2024-06-25 11:32:06 +02:00

38 lines
1.8 KiB
TypeScript

'use server';
import { revalidatePath, revalidateTag } from 'next/cache'
import { Bucket, Post, PostBucket, User, dbSync } from "@/model/Models";
import { Attributes, where } from "@sequelize/core";
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")
if(! await userIsAdmin()) return {error:"Unauthorized, not deleting Post", result: false}
const destroy = await Post.destroy({ where: { id: postID } });
return {result: true};
}
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}, {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."}
const posts = await Post.findAll();
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."}
const post = await Post.update(postAttributes, {where:{id:postAttributes.id}});
return {result:JSON.parse(JSON.stringify(post))};
}