finally got the associations to work through black magic

This commit is contained in:
2024-06-13 07:03:38 +02:00
parent c72ac5e67f
commit 21e31d0ee4
16 changed files with 98 additions and 144 deletions
+12 -14
View File
@@ -1,14 +1,12 @@
'use server'
import { APIError, attemptAPIAction } from "@/util/api/error";
import { Auth, Post, PostTag, Tag, User } from "@/model/Models";
import { sequelize, Bucket, Auth, Post, PostTag, Tag, User, dbSync } from "@/model/Models";
import { cookies } from "next/headers";
import { Attachment } from "@/model/Attachment";
import { UUID, randomUUID } from "crypto";
import { mkdir, mkdirSync, writeFile } from "fs";
import { Bucket } from "@/model/Bucket";
import { where } from "@sequelize/core";
import { PostBucket } from "@/model/Post";
@@ -37,28 +35,28 @@ async function writeFilesToBucket(uuid: UUID, files:any[]) {
async function addToPost(postid:number):Promise<UUID>
{
const post = await Post.findOne({where: {id:postid}, include: {association: Post.associations.postBuckets}});
if (!post) throw new APIError({ status: 500, responseText: "invalid postid" });
const bucket = await Bucket.create({id:randomUUID()});
const bucketPost = await PostBucket.create({bucketId: bucket.id, postId: postid})
Post.sync();
const post = await Post.findOne({where: {id:postid}});
console.log(bucketPost);
return bucket.id
if (!post) throw new APIError({ status: 500, responseText: "invalid postid" });
const bucket = await post.createBucket({id:randomUUID()});
// const bucketPost = await Post.associations.postBuckets.create({bucketId: bucket.id, postId: postid})
// console.log(bucketPost);
return bucket.id;
}
async function addToBucket(bucketid:number):Promise<UUID> {
const bucket = await Bucket.findOne({where: {id: bucketid}});
if (!bucket) throw new APIError({ status: 500, responseText: "invalid bucketid" });
return bucket.id
return bucket.id;
}
async function tryCreateAttachment(request: Request) {
// Make sure the DB is ready
await Attachment.sync();
await Bucket.sync();
await Post.sync();
await dbSync;
// Prepare data
const formData = await request.formData();
const requestData:string | Object | undefined = formData.get('data')?.valueOf();