From 0dd56c4934addcf9f031c6fdeb56b11e23b6dd11 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sun, 28 Nov 2021 10:02:26 -0600 Subject: [PATCH 1/3] GraphQL readme --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index b87cb00..2801e50 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,30 @@ This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Database + +Set the `DATABASE_URL` environment variable for your PostgreSQL DB. (e.g. `postgres://bumi@127.0.0.1:5432/bolt_fun_dev`) + +### `prisma studio` + +prisma studio runs an UI for the DB + +### `prisma migrate dev` + +Create a migration from the schema.prisma file + +### `prisma migrate deploy` + +Apply pending migrations to the database + + +## GraphQL + +GraphQL endpoint is available as netlify function on: `.netlify/functions/graphql` + +Use the Apollo GraphQL Studio to to inspect the GraphQL API: [https://studio.apollographql.com/sandbox/explorer](https://studio.apollographql.com/sandbox/explorer) + ## Available Scripts In the project directory, you can run: From 0d0e57baaff4780901042dc9583dd9c049f2c4cf Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sun, 28 Nov 2021 10:55:04 -0600 Subject: [PATCH 2/3] update database for votes --- .../migration.sql | 20 +++++++++++++++++-- schema.prisma | 16 +++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) rename migrations/{20211128154719_init => 20211128165357_init}/migration.sql (54%) diff --git a/migrations/20211128154719_init/migration.sql b/migrations/20211128165357_init/migration.sql similarity index 54% rename from migrations/20211128154719_init/migration.sql rename to migrations/20211128165357_init/migration.sql index ad940ad..dc37055 100644 --- a/migrations/20211128154719_init/migration.sql +++ b/migrations/20211128165357_init/migration.sql @@ -12,13 +12,29 @@ CREATE TABLE "Project" ( "title" TEXT NOT NULL, "description" TEXT NOT NULL, "website" TEXT NOT NULL, - "thumbnail_image" TEXT NOT NULL, - "cover_image" TEXT NOT NULL, + "thumbnail_image" TEXT, + "cover_image" TEXT, "category_id" INTEGER NOT NULL, "votes_count" INTEGER NOT NULL DEFAULT 0, CONSTRAINT "Project_pkey" PRIMARY KEY ("id") ); +-- CreateTable +CREATE TABLE "Vote" ( + "id" SERIAL NOT NULL, + "project_id" INTEGER NOT NULL, + "amount_in_sat" INTEGER NOT NULL, + "payment_request" TEXT, + "payment_hash" TEXT, + "preimage" TEXT, + "paid" BOOLEAN NOT NULL DEFAULT false, + + CONSTRAINT "Vote_pkey" PRIMARY KEY ("id") +); + -- AddForeignKey ALTER TABLE "Project" ADD CONSTRAINT "Project_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "Category"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Vote" ADD CONSTRAINT "Vote_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/schema.prisma b/schema.prisma index def2a6a..7448aed 100644 --- a/schema.prisma +++ b/schema.prisma @@ -18,9 +18,21 @@ model Project { title String description String website String - thumbnail_image String - cover_image String + thumbnail_image String? + cover_image String? category Category @relation(fields: [category_id], references: [id]) category_id Int votes_count Int @default(0) + Vote Vote[] +} + +model Vote { + id Int @id @default(autoincrement()) + project Project @relation(fields: [project_id], references: [id]) + project_id Int + amount_in_sat Int + payment_request String? + payment_hash String? + preimage String? + paid Boolean @default(false) } From b4532cd1bda4d20b5683eb4a461174eb6b4e80df Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Sun, 28 Nov 2021 11:13:14 -0600 Subject: [PATCH 3/3] voting resolve and prisma seed and cleanup --- functions/graphql/resolvers.js | 15 ++++++++-- functions/graphql/typeDefs.js | 11 ++++++++ package.json | 3 ++ .../20211128171210_init}/migration.sql | 0 .../migrations}/migration_lock.toml | 0 schema.prisma => prisma/schema.prisma | 0 prisma/seed.js | 28 +++++++++++++++++++ 7 files changed, 54 insertions(+), 3 deletions(-) rename {migrations/20211128165357_init => prisma/migrations/20211128171210_init}/migration.sql (100%) rename {migrations => prisma/migrations}/migration_lock.toml (100%) rename schema.prisma => prisma/schema.prisma (100%) create mode 100644 prisma/seed.js diff --git a/functions/graphql/resolvers.js b/functions/graphql/resolvers.js index 0b8c746..8cddfcd 100644 --- a/functions/graphql/resolvers.js +++ b/functions/graphql/resolvers.js @@ -14,7 +14,16 @@ module.exports = { }); }, }, - //Mutation: { - // vote: async (_source, args, context) => {}, - //}, + Mutation: { + vote: async (_source, args, context) => { + const project = await context.prisma.project.findUnique({where: { id: args.project_id }}); + console.log(project) + return context.prisma.vote.create({ + data: { + project_id: project.id, + amount_in_sat: args.amount_in_sat, + } + }); + }, + }, }; diff --git a/functions/graphql/typeDefs.js b/functions/graphql/typeDefs.js index ad3c646..6d23fc6 100644 --- a/functions/graphql/typeDefs.js +++ b/functions/graphql/typeDefs.js @@ -16,9 +16,20 @@ module.exports = gql` title: String! } + type Vote { + id: Int! + project: Project! + amount_in_sat: Int! + payment_request: String! + paid: Boolean! + } + type Query { allProjects: [Project]! getProject(id: Int!): Project allCategories: [Category]! } + type Mutation { + vote (project_id: Int!, amount_in_sat: Int!): Vote! + } `; diff --git a/package.json b/package.json index c0b825d..5caa02b 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,9 @@ "storybook": "start-storybook -p 6006 -s public", "build-storybook": "build-storybook -s public" }, + "prisma": { + "seed": "node prisma/seed.js" + }, "eslintConfig": { "extends": [ "react-app", diff --git a/migrations/20211128165357_init/migration.sql b/prisma/migrations/20211128171210_init/migration.sql similarity index 100% rename from migrations/20211128165357_init/migration.sql rename to prisma/migrations/20211128171210_init/migration.sql diff --git a/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml similarity index 100% rename from migrations/migration_lock.toml rename to prisma/migrations/migration_lock.toml diff --git a/schema.prisma b/prisma/schema.prisma similarity index 100% rename from schema.prisma rename to prisma/schema.prisma diff --git a/prisma/seed.js b/prisma/seed.js new file mode 100644 index 0000000..b8f95e5 --- /dev/null +++ b/prisma/seed.js @@ -0,0 +1,28 @@ +const { PrismaClient } = require('@prisma/client') +const prisma = new PrismaClient() + +async function main() { + const category = await prisma.category.create({ + data: { + title: 'El Salvador', + }, + }); + + const project = await prisma.project.create({ + data: { + title: "Captain Morgan", + description: "HQ on a VULCANO lake", + website: "https://github.com/peakshift", + category_id: category.id, + } + }); +} + +main() + .catch((e) => { + console.error(e) + process.exit(1) + }) + .finally(async () => { + await prisma.$disconnect() + }) \ No newline at end of file