Skip to content

Commit

Permalink
create pet controller
Browse files Browse the repository at this point in the history
  • Loading branch information
zobkazi committed Apr 20, 2024
1 parent f760f52 commit 87da41d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 69 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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")
);
76 changes: 34 additions & 42 deletions packages/services/adoption/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion packages/services/adoption/src/controllers/createPet.ts
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 6 additions & 5 deletions packages/services/adoption/src/utils/schemas.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit 87da41d

Please sign in to comment.