Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I need you to write me a PostgreSQL query, here is the model:
- generator client {
- provider = "prisma-client"
- output = "../src/generated/"
- previewFeatures = ["driverAdapters", "queryCompiler", "relationJoins", "typedSql"]
- }
- datasource db {
- provider = "postgresql"
- url = env("DATABASE_URL")
- directUrl = env("DIRECT_URL")
- }
- model User {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- email String @unique
- password String?
- name String
- role UserRole @default(USER)
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- referalCode String? @unique
- referalCodeUsed Boolean @default(false)
- authenticationMethod AuthenticationMethod @default(EMAIL)
- answers Answer[]
- submissions Submission[]
- allowedProblemSets UserOnProblemSet[]
- }
- model ProblemSet {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- name String @unique
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- availableFrom DateTime
- availableTo DateTime
- type ProblemSetType @default(REAL)
- scenarios Scenario[]
- submissions Submission[]
- allowedUsers UserOnProblemSet[]
- }
- model UserOnProblemSet {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- userId String
- problemSetId String
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- problemSet ProblemSet @relation(fields: [problemSetId], references: [id], onDelete: Cascade)
- user User @relation(fields: [userId], references: [id], onDelete: Cascade)
- @@unique([userId, problemSetId])
- }
- model Scenario {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- type ScenarioType
- text String?
- videoUrl String?
- videoTranscription String?
- desc String?
- problemSetId String
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- videoMinutes Int? @db.SmallInt
- videoSeconds Int? @db.SmallInt
- number Int @default(1) @db.SmallInt
- questions Question[]
- problemSet ProblemSet @relation(fields: [problemSetId], references: [id], onDelete: Cascade)
- ScenarioReport ScenarioReport[]
- @@index([problemSetId, number])
- }
- model Question {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- number Int @default(1) @db.SmallInt
- statement String
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- scenarioId String
- scenario Scenario @relation(fields: [scenarioId], references: [id], onDelete: Cascade)
- answer Answer[]
- QuestionReport QuestionReport[]
- @@index([scenarioId, number])
- }
- model Submission {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- createdAt DateTime @default(now())
- problemSetId String
- updatedAt DateTime @default(now()) @updatedAt
- userId String?
- answers Answer[]
- problemSet ProblemSet @relation(fields: [problemSetId], references: [id], onDelete: Cascade)
- user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
- }
- model Answer {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- answer String
- userId String?
- questionId String
- submissionId String
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- question Question @relation(fields: [questionId], references: [id], onDelete: Cascade)
- submission Submission @relation(fields: [submissionId], references: [id], onDelete: Cascade)
- user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
- likes Int @default(0)
- answerCategoryId String?
- AnswerCategory AnswerCategory? @relation(fields: [answerCategoryId], references: [id])
- @@index([submissionId, questionId])
- }
- model ScenarioReport {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- scenarioId String
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- scenario Scenario @relation(fields: [scenarioId], references: [id], onDelete: Cascade)
- QuestionReport QuestionReport[]
- @@unique([scenarioId])
- }
- model QuestionReport {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- scenarioReportId String
- questionId String
- question Question @relation(fields: [questionId], references: [id], onDelete: Cascade)
- scenarioReport ScenarioReport @relation(fields: [scenarioReportId], references: [id], onDelete: Cascade)
- AnswerCategory AnswerCategory[]
- }
- model AnswerCategory {
- id String @id @default(dbgenerated("(gen_random_uuid())::text"))
- createdAt DateTime @default(now())
- updatedAt DateTime @default(now()) @updatedAt
- title String @unique
- questionReportId String
- QuestionReport QuestionReport @relation(fields: [questionReportId], references: [id])
- Answer Answer[]
- }
- enum ScenarioType {
- TEXT
- VIDEO
- }
- enum UserRole {
- ADMIN
- USER
- }
- enum ProblemSetType {
- DEMO
- REAL
- }
- enum AuthenticationMethod {
- EMAIL
- GOOGLE
- }
- Here is the code: You don't necessarily need to use all of it, you can make some changes if you think it will help, this code just provides the starting ground on where to begin.
- First, initialize the answerIdsAlreadyIncluded and answersToInclude with the format "{"question1Id":[]}" for all questionids in the scenarioQuestionIds array.
- Then, it will find the last ScenarioReport for the given scenarioId.
- If there wasn't a ScenarioReport for the scenarioId then:
- - include all answers with the right questionIds in the answersToInclude variable, with the format "{"question1Id":[{"answerId": , "answer": }]}".
- However, if there was a ScenarioReport for the scenarioId then:
- - get all answers that was included in the last ScenarioReport for the given scenarioId, and store them in the answerIdsAlreadyIncluded variable in the format "{"question1Id":["answerId1", "answerId2"]}". You can get the answers of the ScenarioReport by looking at the QuestionReport and then its AnswerCategory.
- - only include answers that are not already included in the last ScenarioReport for the given scenarioId, that is in the answerIdsAlreadyIncluded variable, and add them to the answersToInclude variable in the same format as above.
- BUT, answerIdsAlreadyIncluded is optional, you don't have to use this variable and populate it if you don't need it. If you can keep track of the answers to include without it, you can skip that part.
- You must write PostgreSQL code that is correct, performant as possible and highly optimized, and readable.
- You can optionally use indexes, just make sure to tell me which indexes you are using and why they are necessary as well as try to make your query index-friendly if possible if you decide to use indexes.
- Finally, return the answersToInclude variable.
- -- @param {String} $1:scenarioId
- DO $$
- DECLARE
- scenarioQuestionIds TEXT[];
- prevScenarioReportId TEXT;
- answerIdsAlreadyIncluded JSONB;
- answersToInclude JSONB;
- BEGIN
- SELECT array_agg(q.id) INTO scenarioQuestionIds
- FROM "question" AS q
- JOIN "Scenario" AS s ON q."scenarioId" = s.id
- WHERE s.id = $1;
- IF NOT FOUND THEN
- RAISE EXCEPTION 'No questions found for scenario with id %', $1;
- END IF;
- SELECT sr.id INTO prevScenarioReportId
- FROM "ScenarioReport" AS sr
- WHERE sr."scenarioId" = $1
- ORDER BY sr."createdAt" DESC
- LIMIT 1;
- IF NOT FOUND THEN
- ELSE
- END $$;
Advertisement
Add Comment
Please, Sign In to add comment