diff --git a/app.js b/app.js index 40452c6..1e0ff03 100644 --- a/app.js +++ b/app.js @@ -3,7 +3,7 @@ import { PrismaClient } from "@prisma/client"; import createError from "http-errors" import jeux from './route/jeux.js' - +import groupe from './route/groupe.js' const app = express() app.use(express.json()) @@ -13,7 +13,7 @@ const port = 3500 app.use('/',jeux) - +app.use('/',groupe) // run the server app.listen(port, () => { diff --git a/groupeValidator.js b/groupeValidator.js new file mode 100644 index 0000000..8521e3a --- /dev/null +++ b/groupeValidator.js @@ -0,0 +1,9 @@ +import { optional, string, z } from "zod"; + +const groupeValidation = z.object({ + name : z.string(), + logo : z.string(), + +}); + +export default groupeValidation; \ No newline at end of file diff --git a/index.html b/index.html deleted file mode 100644 index e69de29..0000000 diff --git a/jeuxValidator.js b/jeuxValidator.js index cfdb034..4ada8ba 100644 --- a/jeuxValidator.js +++ b/jeuxValidator.js @@ -4,7 +4,8 @@ const jeuxValidation = z.object({ Title : z.string(), Description : z.string(), Picture: z.string(), - Worker : z.string() + lien : z.string(), + groupe_id : z.number() }); diff --git a/prisma/migrations/20230206134947_ajout_de_l_url/migration.sql b/prisma/migrations/20230206134947_ajout_de_l_url/migration.sql new file mode 100644 index 0000000..c585fe7 --- /dev/null +++ b/prisma/migrations/20230206134947_ajout_de_l_url/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - Added the required column `url` to the `Jeux` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE `Jeux` ADD COLUMN `url` VARCHAR(191) NOT NULL; diff --git a/prisma/migrations/20230206135125_lien/migration.sql b/prisma/migrations/20230206135125_lien/migration.sql new file mode 100644 index 0000000..eaf11f3 --- /dev/null +++ b/prisma/migrations/20230206135125_lien/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - You are about to drop the column `url` on the `Jeux` table. All the data in the column will be lost. + - Added the required column `lien` to the `Jeux` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE `Jeux` DROP COLUMN `url`, + ADD COLUMN `lien` VARCHAR(191) NOT NULL; diff --git a/prisma/migrations/20230206142200_user/migration.sql b/prisma/migrations/20230206142200_user/migration.sql new file mode 100644 index 0000000..f8eed60 --- /dev/null +++ b/prisma/migrations/20230206142200_user/migration.sql @@ -0,0 +1,22 @@ +/* + Warnings: + + - You are about to drop the column `Workers` on the `Jeux` table. All the data in the column will be lost. + - Added the required column `groupe_id` to the `Jeux` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE `Jeux` DROP COLUMN `Workers`, + ADD COLUMN `groupe_id` INTEGER NOT NULL; + +-- CreateTable +CREATE TABLE `Groupe` ( + `id` INTEGER NOT NULL AUTO_INCREMENT, + `name` VARCHAR(191) NOT NULL, + `logo` VARCHAR(255) NOT NULL, + + PRIMARY KEY (`id`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `Jeux` ADD CONSTRAINT `Jeux_groupe_id_fkey` FOREIGN KEY (`groupe_id`) REFERENCES `Groupe`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b60222a..9fb9825 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -10,10 +10,19 @@ datasource db { url = env("DATABASE_URL") } +model Groupe{ + id Int @id @default(autoincrement()) + name String + logo String @db.VarChar(255) + jeux Jeux [] +} model Jeux{ id Int @id @default(autoincrement()) Title String Description String Picture String @db.VarChar(255) - Workers String + lien String + groupe_id Int + groupe Groupe @relation(fields: [groupe_id], references: [id]) + } diff --git a/route/groupe.js b/route/groupe.js new file mode 100644 index 0000000..c2f1444 --- /dev/null +++ b/route/groupe.js @@ -0,0 +1,27 @@ +import express from "express"; +import { PrismaClient } from "@prisma/client"; +import createError from "http-errors" +import groupeValidation from "../groupeValidator.js"; + +const router = express.Router() +const prisma = new PrismaClient(); + + + +router.post('/groupe', async(req,res)=>{ + let groupData; + groupData = groupeValidation.parse(req.body) + let group = await prisma.groupe.create({ + data:{ + name : groupData.name, + logo : groupData.logo, + + } + }) + res.json(group) +}) + + + + +export default router; \ No newline at end of file diff --git a/route/jeux.js b/route/jeux.js index f167d75..d4f979a 100644 --- a/route/jeux.js +++ b/route/jeux.js @@ -11,15 +11,24 @@ const prisma = new PrismaClient(); router.post('/jeux',async (req,res,next)=>{ let jeuxData; jeuxData = jeuxValidation.parse(req.body) - const jeux = await prisma.jeux.create({ + let jeux = await prisma.jeux.create({ data:{ Title : jeuxData.Title, Description : jeuxData.Description, Picture : jeuxData.Picture, - Workers : jeuxData.Worker + lien : jeuxData.lien, + groupe:{ + connect:{ + id : jeuxData.groupe_id + + } + } } + }) res.json(jeux) + console.log(jeux) + })