Skip to content

Commit

Permalink
add vote table (IT-Academy-BCN#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolsastre authored Apr 28, 2023
1 parent e628f99 commit 540ebc5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
16 changes: 16 additions & 0 deletions back/prisma/migrations/20230428132132_add_vote/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- CreateTable
CREATE TABLE "vote" (
"user_id" TEXT NOT NULL,
"resource_id" TEXT NOT NULL,
"vote" SMALLINT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "vote_pkey" PRIMARY KEY ("user_id","resource_id")
);

-- AddForeignKey
ALTER TABLE "vote" ADD CONSTRAINT "vote_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "vote" ADD CONSTRAINT "vote_resource_id_fkey" FOREIGN KEY ("resource_id") REFERENCES "resource"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
53 changes: 34 additions & 19 deletions back/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ datasource db {
}

model User {
id String @id @default(cuid())
email String @unique
dni String @unique @db.VarChar(10)
id String @id @default(cuid())
email String @unique
dni String @unique @db.VarChar(10)
password String
name String
status USER_STATUS @default(INACTIVE)
role USER_ROLE @default(REGISTERED)
status USER_STATUS @default(INACTIVE)
role USER_ROLE @default(REGISTERED)
resources Resource[]
media Media[]
favorite Favorites[]
viewed ViewedResource[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
vote Vote[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@map("user")
}
Expand All @@ -44,6 +45,7 @@ model Resource {
topics TopicsOnResources[]
favorites Favorites[]
viewed ViewedResource[]
vote Vote[]
user User @relation(fields: [userId], references: [id])
userId String @map("user_id")
createdAt DateTime @default(now()) @map("created_at")
Expand Down Expand Up @@ -103,24 +105,37 @@ model TopicsOnResources {
}

model Favorites {
user User @relation(fields: [userId], references: [id])
userId String @map("user_id")
resource Resource @relation(fields: [resourceId], references: [id])
resourceId String @map("resource_id")
createdAt DateTime @default(now()) @map("created_at")
user User @relation(fields: [userId], references: [id])
userId String @map("user_id")
resource Resource @relation(fields: [resourceId], references: [id])
resourceId String @map("resource_id")
createdAt DateTime @default(now()) @map("created_at")
@@id([userId, resourceId])
@@map("favorites")
}

model ViewedResource {
user User @relation(fields: [userId], references: [id])
userId String @map("user_id")
resource Resource @relation(fields: [resourceId], references: [id])
resourceId String @map("resource_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id])
userId String @map("user_id")
resource Resource @relation(fields: [resourceId], references: [id])
resourceId String @map("resource_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@id([userId, resourceId])
@@map("viewed_resource")
}
}

model Vote {
user User @relation(fields: [userId], references: [id])
userId String @map("user_id")
resource Resource @relation(fields: [resourceId], references: [id])
resourceId String @map("resource_id")
vote Int @db.SmallInt // 1 upvote, -1 downvote
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@id([userId, resourceId])
@@map("vote")
}

0 comments on commit 540ebc5

Please sign in to comment.