Skip to content

Commit

Permalink
wip: implement data structure for answer collection creation and editing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjschlapbach committed Jan 1, 2025
1 parent d73c976 commit df43512
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
-- CreateEnum
CREATE TYPE "CollectionAccess" AS ENUM ('PUBLIC', 'PRIVATE', 'RESTRICTED');

-- CreateTable
CREATE TABLE "AnswerCollection" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"version" INTEGER NOT NULL DEFAULT 1,
"access" "CollectionAccess" NOT NULL DEFAULT 'PRIVATE',
"ownerId" UUID,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "AnswerCollection_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "AnswerCollectionEntry" (
"id" SERIAL NOT NULL,
"value" TEXT NOT NULL,
"collectionId" INTEGER NOT NULL,

CONSTRAINT "AnswerCollectionEntry_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "_answerCollectionAccessRequested" (
"A" INTEGER NOT NULL,
"B" UUID NOT NULL,

CONSTRAINT "_answerCollectionAccessRequested_AB_pkey" PRIMARY KEY ("A","B")
);

-- CreateTable
CREATE TABLE "_answerCollectionShared" (
"A" INTEGER NOT NULL,
"B" UUID NOT NULL,

CONSTRAINT "_answerCollectionShared_AB_pkey" PRIMARY KEY ("A","B")
);

-- CreateIndex
CREATE UNIQUE INDEX "AnswerCollection_ownerId_name_key" ON "AnswerCollection"("ownerId", "name");

-- CreateIndex
CREATE UNIQUE INDEX "AnswerCollectionEntry_collectionId_value_key" ON "AnswerCollectionEntry"("collectionId", "value");

-- CreateIndex
CREATE INDEX "_answerCollectionAccessRequested_B_index" ON "_answerCollectionAccessRequested"("B");

-- CreateIndex
CREATE INDEX "_answerCollectionShared_B_index" ON "_answerCollectionShared"("B");

-- AddForeignKey
ALTER TABLE "AnswerCollection" ADD CONSTRAINT "AnswerCollection_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE SET NULL;

-- AddForeignKey
ALTER TABLE "AnswerCollectionEntry" ADD CONSTRAINT "AnswerCollectionEntry_collectionId_fkey" FOREIGN KEY ("collectionId") REFERENCES "AnswerCollection"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_answerCollectionAccessRequested" ADD CONSTRAINT "_answerCollectionAccessRequested_A_fkey" FOREIGN KEY ("A") REFERENCES "AnswerCollection"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_answerCollectionAccessRequested" ADD CONSTRAINT "_answerCollectionAccessRequested_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_answerCollectionShared" ADD CONSTRAINT "_answerCollectionShared_A_fkey" FOREIGN KEY ("A") REFERENCES "AnswerCollection"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_answerCollectionShared" ADD CONSTRAINT "_answerCollectionShared_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
40 changes: 40 additions & 0 deletions packages/prisma/src/prisma/schema/element.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,46 @@ model Tag {

// #endregion

// ----- ANSWER COLLECTIONS -----
// #region
enum CollectionAccess {
PUBLIC
PRIVATE
RESTRICTED
}

model AnswerCollection {
id Int @id @default(autoincrement())
name String
description String?
version Int @default(1)
entries AnswerCollectionEntry[]
access CollectionAccess @default(PRIVATE)
accessRequested User[] @relation("answerCollectionAccessRequested")
accessGranted User[] @relation("answerCollectionShared")
owner User? @relation(name: "userAnswerCollections", fields: [ownerId], references: [id], onDelete: SetNull, onUpdate: SetNull)
ownerId String? @db.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([ownerId, name])
}

model AnswerCollectionEntry {
id Int @id @default(autoincrement())
value String
collection AnswerCollection @relation(fields: [collectionId], references: [id], onDelete: Cascade, onUpdate: Cascade)
collectionId Int
@@unique([collectionId, value])
}

// #endregion

// ----- MEDIA LIBRARY -----
// #region
model MediaFile {
Expand Down
4 changes: 4 additions & 0 deletions packages/prisma/src/prisma/schema/user.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ model User {
liveQuizzes LiveQuiz[]
competencyTrees CompetencyTree[]
answerCollections AnswerCollection[] @relation("userAnswerCollections")
requestedCollections AnswerCollection[] @relation("answerCollectionAccessRequested")
sharedCollections AnswerCollection[] @relation("answerCollectionShared")
firstLogin Boolean @default(true)
createdAt DateTime @default(now())
Expand Down

0 comments on commit df43512

Please sign in to comment.