Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
6 changes: 0 additions & 6 deletions .env.example

This file was deleted.

134 changes: 126 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
},
"dependencies": {
"@prisma/client": "^5.16.1",
"@supabase/supabase-js": "^2.44.3",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"morgan": "^1.10.0"
}
Expand Down
29 changes: 29 additions & 0 deletions prisma/migrations/20240708112245_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Warnings:

- A unique constraint covering the columns `[title]` on the table `Movie` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[number]` on the table `Screen` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateTable
CREATE TABLE "Review" (
"id" SERIAL NOT NULL,
"movieId" INTEGER NOT NULL,
"customerId" INTEGER NOT NULL,
"rating" INTEGER NOT NULL,
"content" TEXT NOT NULL,

CONSTRAINT "Review_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Movie_title_key" ON "Movie"("title");

-- CreateIndex
CREATE UNIQUE INDEX "Screen_number_key" ON "Screen"("number");

-- AddForeignKey
ALTER TABLE "Review" ADD CONSTRAINT "Review_movieId_fkey" FOREIGN KEY ("movieId") REFERENCES "Movie"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Review" ADD CONSTRAINT "Review_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ model Ticket {
customerId Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
}
2 changes: 1 addition & 1 deletion prisma/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ seed()
console.error(e);
await prisma.$disconnect();
})
.finally(() => process.exit(1));
.finally(() => process.exit(1));
76 changes: 42 additions & 34 deletions src/controllers/customer.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,56 @@
const { PrismaClientKnownRequestError } = require("@prisma/client")
const { createCustomerDb } = require('../domains/customer.js')
const { PrismaClientKnownRequestError } = require('@prisma/client');
const { createCustomerDb, updateCustomerDb } = require('../domains/customer.js');

const createCustomer = async (req, res) => {
const {
name,
phone,
email
} = req.body
const { name, phone, email } = req.body;

if (!name || !phone || !email) {
return res.status(400).json({
error: "Missing fields in request body"
})
error: 'Missing fields in request body',
});
}

// Try-catch is a very common way to handle errors in JavaScript.
// It allows us to customise how we want errors that are thrown to be handled.
// Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

// Here, if Prisma throws an error in the process of trying to create a new customer,
// instead of the Prisma error being thrown (and the app potentially crashing) we exit the
// `try` block (bypassing the `res.status` code) and enter the `catch` block.
try {
const createdCustomer = await createCustomerDb(name, phone, email)

res.status(201).json({ customer: createdCustomer })
} catch (e) {
// In this catch block, we are able to specify how different Prisma errors are handled.
// Prisma throws errors with its own codes. P2002 is the error code for
// "Unique constraint failed on the {constraint}". In our case, the {constraint} is the
// email field which we have set as needing to be unique in the prisma.schema.
// To handle this, we return a custom 409 (conflict) error as a response to the client.
// Prisma error codes: https://www.prisma.io/docs/orm/reference/error-reference#common
// HTTP error codes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses
if (e instanceof PrismaClientKnownRequestError) {
if (e.code === "P2002") {
return res.status(409).json({ error: "A customer with the provided email already exists" })
const createdCustomer = await createCustomerDb(name, phone, email);

res.status(201).json({ customer: createdCustomer });
} catch (error) {
if (error instanceof PrismaClientKnownRequestError) {
if (error.code === 'P2002') {
return res.status(409).json({
error: 'A customer with the provided email already exists',
});
}
}

res.status(500).json({ error: e.message })
res.status(500).json({ error: error.message });
}
};

const updateCustomer = async (req, res) => {
const { name, contact } = req.body;
const id = Number(req.params.id);

if (!name) {
return res.status(400).json({
error: 'Missing fields in request body',
});
}

try {
const updatedCustomer = await updateCustomerDb(id, name, contact);

res.status(201).json({ customer: updatedCustomer });
} catch (error) {
if (error.code === 'P2025') {
return res.status(404).json({ error: error.message });
}

res.status(500).json({ error: error.message });
}
}
};

module.exports = {
createCustomer
}
createCustomer,
updateCustomer,
};
Loading