From 87da41d4d1403b49d68f3cff4cfdbbbcb0a86976 Mon Sep 17 00:00:00 2001 From: zobaidulkazi64 Date: Sat, 20 Apr 2024 23:46:32 +0600 Subject: [PATCH] create pet controller --- .../20240420164021_init/migration.sql | 21 ----- .../20240420173748_init_y/migration.sql | 30 ++++++++ .../services/adoption/prisma/schema.prisma | 76 +++++++++---------- .../adoption/src/controllers/createPet.ts | 24 +++++- .../services/adoption/src/utils/schemas.ts | 11 +-- 5 files changed, 93 insertions(+), 69 deletions(-) delete mode 100644 packages/services/adoption/prisma/migrations/20240420164021_init/migration.sql create mode 100644 packages/services/adoption/prisma/migrations/20240420173748_init_y/migration.sql diff --git a/packages/services/adoption/prisma/migrations/20240420164021_init/migration.sql b/packages/services/adoption/prisma/migrations/20240420164021_init/migration.sql deleted file mode 100644 index c463681..0000000 --- a/packages/services/adoption/prisma/migrations/20240420164021_init/migration.sql +++ /dev/null @@ -1,21 +0,0 @@ --- CreateTable -CREATE TABLE "Pet" ( - "id" TEXT NOT NULL, - "userId" TEXT NOT NULL, - "name" TEXT NOT NULL, - "breed" TEXT NOT NULL, - "age" TEXT NOT NULL, - "color" TEXT NOT NULL, - "size" TEXT NOT NULL, - "description" TEXT NOT NULL, - "images" TEXT, - "category" TEXT NOT NULL, - "gender" TEXT NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - - CONSTRAINT "Pet_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "Pet_breed_key" ON "Pet"("breed"); diff --git a/packages/services/adoption/prisma/migrations/20240420173748_init_y/migration.sql b/packages/services/adoption/prisma/migrations/20240420173748_init_y/migration.sql new file mode 100644 index 0000000..fa9e15a --- /dev/null +++ b/packages/services/adoption/prisma/migrations/20240420173748_init_y/migration.sql @@ -0,0 +1,30 @@ +-- CreateEnum +CREATE TYPE "PetGender" AS ENUM ('MALE', 'FEMALE'); + +-- CreateEnum +CREATE TYPE "PetCategory" AS ENUM ('DOG', 'CAT', 'BIRD'); + +-- CreateEnum +CREATE TYPE "PetSize" AS ENUM ('SMALL', 'MEDIUM', 'LARGE', 'EX_LARGE'); + +-- CreateEnum +CREATE TYPE "PetColor" AS ENUM ('WHITE', 'BLACK', 'BROWN', 'GRAY'); + +-- CreateTable +CREATE TABLE "Pet" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "name" TEXT NOT NULL, + "breed" TEXT NOT NULL, + "age" DOUBLE PRECISION NOT NULL DEFAULT 2, + "color" "PetColor" NOT NULL DEFAULT 'BLACK', + "size" "PetSize" NOT NULL DEFAULT 'MEDIUM', + "description" TEXT NOT NULL, + "image" TEXT, + "category" "PetCategory" NOT NULL DEFAULT 'DOG', + "gender" "PetGender" NOT NULL DEFAULT 'MALE', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "Pet_pkey" PRIMARY KEY ("id") +); diff --git a/packages/services/adoption/prisma/schema.prisma b/packages/services/adoption/prisma/schema.prisma index 5dc1580..11e28d8 100644 --- a/packages/services/adoption/prisma/schema.prisma +++ b/packages/services/adoption/prisma/schema.prisma @@ -13,57 +13,49 @@ datasource db { url = env("DATABASE_URL") } -// create pet schema -// enum petGender { -// MALE -// FEMALE -// } +// schema.prisma -// enum petCategory { -// DOG -// CAT -// BIRD -// } +// Enums +enum PetGender { + MALE + FEMALE +} -// enum petSize { -// SMALL -// MEDIUM -// LARGE -// EX_LARGE -// } +enum PetCategory { + DOG + CAT + BIRD +} -// enum petColor { -// WHITH -// BLACK -// BROWN -// GRAY -// } +enum PetSize { + SMALL + MEDIUM + LARGE + EX_LARGE +} -// enum petBreed { -// LABRADOR -// POODLE -// BEAGLE -// GERMAN_SHEPHERD -// GOLDEN_RETRIEVER -// DACHSHUND -// BULLDOG -// HUSKY -// } +enum PetColor { + WHITE + BLACK + BROWN + GRAY +} +// Model model Pet { - id String @id @default(cuid()) + id String @id @default(uuid()) userId String name String - breed String @unique - age String - color String - size String + breed String + age Float @default(2) + color PetColor @default(BLACK) + size PetSize @default(MEDIUM) description String - images String? - category String - gender String - createdAt DateTime @default(now()) - updatedAt DateTime + image String? + category PetCategory @default(DOG) + gender PetGender @default(MALE) + createdAt DateTime @default(now()) + updatedAt DateTime @default(now()) } // PetAdoption diff --git a/packages/services/adoption/src/controllers/createPet.ts b/packages/services/adoption/src/controllers/createPet.ts index 6b2b3db..a2205b0 100644 --- a/packages/services/adoption/src/controllers/createPet.ts +++ b/packages/services/adoption/src/controllers/createPet.ts @@ -1 +1,23 @@ -console.log("hello world"); +import { Request, Response, NextFunction } from "express"; +import prisma from "@/utils/prisma"; +import { CreatePetSchema } from "@/utils/schemas"; + +const createPet = async (req: Request, res: Response, next: NextFunction) => { + try { + const parsedBody = CreatePetSchema.safeParse(req.body); + + if (!parsedBody.success) { + return next(parsedBody.error); + } + // create pet + const createdPet = await prisma.pet.create({ + data: parsedBody.data, + }); + + res.status(201).json({ pet: createdPet }); + } catch (error) { + next(error); + } +}; + +export default createPet; diff --git a/packages/services/adoption/src/utils/schemas.ts b/packages/services/adoption/src/utils/schemas.ts index 765b576..f84d193 100644 --- a/packages/services/adoption/src/utils/schemas.ts +++ b/packages/services/adoption/src/utils/schemas.ts @@ -1,16 +1,17 @@ +import { PetColor, PetSize, PetCategory, PetGender } from "@prisma/client"; import { z } from "zod"; export const CreatePetSchema = z.object({ userId: z.string(), name: z.string(), breed: z.string(), - age: z.string(), - color: z.string(), - size: z.string(), + age: z.number().optional(), + color: z.nativeEnum(PetColor), + size: z.nativeEnum(PetSize), description: z.string().min(20).max(500), image: z.string().optional(), - category: z.string().optional(), - gender: z.string().optional(), + category: z.nativeEnum(PetCategory), + gender: z.nativeEnum(PetGender), }); export const PetAdoptionSchema = z.object({