Skip to content

Commit

Permalink
feat: add dishes, stores, users POST API
Browse files Browse the repository at this point in the history
  • Loading branch information
yuting1008 committed May 7, 2024
1 parent a6d5e11 commit feb3655
Show file tree
Hide file tree
Showing 18 changed files with 249 additions and 470 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_URL="postgres://default:6AdOoLzxJB8M@ep-green-cloud-a1c2usdd-pooler.ap-southeast-1.aws.neon.tech:5432/verceldb?sslmode=require"
3 changes: 2 additions & 1 deletion drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import dotenv from "dotenv";
import type { Config } from "drizzle-kit";

// this file is for drizzle-kit, which is used to do our database migrations
dotenv.config({ path: "./.env.local" });
// dotenv.config({ path: "./.env.local" });
dotenv.config({ path: "./.env" });

if (!process.env.POSTGRES_URL) {
throw new Error("POSTGRES_URL must be defined in .env.local");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@typescript-eslint/eslint-plugin": "^7.7.1",
"cypress": "^13.8.1",
"dotenv": "^16.4.5",
"drizzle-kit": "^0.20.17",
"drizzle-kit": "^0.20.18",
"eslint": "^8",
"eslint-config-next": "14.2.2",
"eslint-config-prettier": "^9.1.0",
Expand Down
55 changes: 0 additions & 55 deletions src/app/api/carts/route.test.ts

This file was deleted.

41 changes: 0 additions & 41 deletions src/app/api/carts/route.ts

This file was deleted.

120 changes: 98 additions & 22 deletions src/app/api/dishes/route.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,86 @@
import type { NextRequest } from "next/server";
// import type { NextRequest } from "next/server";

import { describe, expect, it } from "@jest/globals";
// import { describe, expect, it } from "@jest/globals";

// import { POST } from "./route";

// describe("POST /api/dishes", () => {
// it("should return added data with status 201", async () => {
// const requestObj = {
// json: async () => ({
// store_id: 1,
// dishName: "Bento Box",
// category: "Taiwanese",
// quantity: 2,
// price: 60,
// description: "Yummy",
// }),
// } as NextRequest;

// const response = await POST(requestObj);
// const body = await response.json();

// expect(response.status).toBe(201);
// expect(body.data.store_id).toBe(1);
// expect(body.data.dishName).toBe("Bento Box");
// expect(body.data.quantity).toBe(2);
// expect(body.data.price).toBe(60);
// expect(body.data.description).toBe("Yummy");
// });
// });

// describe("POST /api/dishes", () => {
// it("should return an error response with status 400 for invalid request", async () => {
// // Provide invalid data to trigger the catch (error) block
// const requestObj = {
// json: async () => ({
// store_id: "invalid",
// dishName: "Bento Box",
// category: "Taiwanese",
// quantity: 2,
// price: 60,
// description: "Yummy",
// }),
// } as NextRequest;

// const response = await POST(requestObj);
// const body = await response.json();

// expect(response.status).toBe(400);
// expect(body.error).toBe("Invalid request");
// });
// });

import { type NextRequest } from "next/server";

import { describe, expect, it, jest } from "@jest/globals";

import { db } from "@/db";

import { POST } from "./route";

let id = 0;

describe("POST /api/dishes", () => {
it("should return added data with status 201", async () => {
it("should return 400 if request is invalid", async () => {
const requestObj = {
json: async () => ({
store_id: 1,
dishName: "Bento Box",
category: "Taiwanese",
json: async () => ({ invalidField: "Invalid value" }),
} as NextRequest;

const response = await POST(requestObj);
const body = await response.json();

expect(response.status).toBe(400);
expect(body.error).toBe("Invalid request");
});

it("should return 200 with added data if request is valid", async () => {
const requestObj = {
json: async () => ({
quantity: 2,
category: "taiwanese",
storeId: 1,
name: "Bento",
price: 60,
description: "Yummy",
}),
Expand All @@ -20,33 +89,40 @@ describe("POST /api/dishes", () => {
const response = await POST(requestObj);
const body = await response.json();

expect(response.status).toBe(201);
expect(body.data.store_id).toBe(1);
expect(body.data.dishName).toBe("Bento Box");
expect(body.data.quantity).toBe(2);
expect(body.data.price).toBe(60);
expect(body.data.description).toBe("Yummy");
expect(response.status).toBe(200);
expect(body.quantity).toBe(2);
expect(body.category).toBe("taiwanese");
expect(body.storeId).toBe(1);
expect(body.name).toBe("Bento");
expect(body.price).toBe(60);
expect(body.description).toBe("Yummy");
id = body.id;
});
});

describe("POST /api/dishes", () => {
it("should return an error response with status 400 for invalid request", async () => {
// Provide invalid data to trigger the catch (error) block
it("should return 500 if there is an internal server error", async () => {
const requestObj = {
json: async () => ({
store_id: "invalid",
dishName: "Bento Box",
category: "Taiwanese",
quantity: 2,
category: "taiwanese",
storeId: 1,
name: "Bento",
price: 60,
description: "Yummy",
}),
} as NextRequest;

// Mock the db.insert function to throw an error
jest.spyOn(db, "insert").mockImplementation(() => {
throw new Error("Internal server error");
});

const response = await POST(requestObj);
const body = await response.json();

expect(response.status).toBe(400);
expect(body.error).toBe("Invalid request");
expect(response.status).toBe(500);
expect(body.error).toBe("Internal Sever Error");

// Restore the original implementation of db.insert
jest.restoreAllMocks();
});
});
38 changes: 24 additions & 14 deletions src/app/api/dishes/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { NextResponse, type NextRequest } from "next/server";

import { eq } from "drizzle-orm";
import { z } from "zod";

const createCommentRequestSchema = z.object({
store_id: z.number(),
dishName: z.string(),
category: z.string(),
import { db } from "@/db";
import { dishTable } from "@/db/schema";

const createDishRequestSchema = z.object({
quantity: z.number(),
category: z.enum(["taiwanese",
"japanese",
"american",
"healthy meal",
"pastry",
"fruit",
]),
storeId: z.number(),
name: z.string(),
price: z.number(),
description: z.string(),
});
Expand All @@ -15,25 +25,25 @@ export async function POST(request: NextRequest) {
const data = await request.json();

try {
createCommentRequestSchema.parse(data);
createDishRequestSchema.parse(data);
} catch (error) {
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
}

const { store_id, dishName, category, quantity, price, description } = data as z.infer<
typeof createCommentRequestSchema
>;
const { quantity, category, storeId, name, price, description } = data as z.infer<typeof createDishRequestSchema>;

try {
console.log("Creating comment", store_id, dishName, category, quantity, price, description);
const [dish] = await db
.insert(dishTable)
.values({ quantity, category, storeId, name, price, description })
.returning()
.execute();
return NextResponse.json(dish, { status: 200 });
} catch (error) {
console.log(error);
return NextResponse.json(
{ error: "Something went wrong" },
{ error: "Internal Sever Error" },
{ status: 500 },
);
}

return new NextResponse(JSON.stringify({ data }), { status: 201 });
// return new NextResponse(JSON.stringify({ data, message: "OK" }), { status: 201 });
}
}
47 changes: 0 additions & 47 deletions src/app/api/post-reservations/route.test.ts

This file was deleted.

Loading

0 comments on commit feb3655

Please sign in to comment.