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
6 changes: 0 additions & 6 deletions .env.example

This file was deleted.

12 changes: 12 additions & 0 deletions prisma/migrations/20240814183117_model/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
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.

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

-- CreateIndex
CREATE UNIQUE INDEX "Screen_number_key" ON "Screen"("number");
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ model Contact {
model Movie {
id Int @id @default(autoincrement())
screenings Screening[]
title String
title String @unique
runtimeMins Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Screen {
id Int @id @default(autoincrement())
number Int
number Int @unique
screenings Screening[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
61 changes: 45 additions & 16 deletions src/controllers/customer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const { PrismaClientKnownRequestError } = require("@prisma/client")
const { createCustomerDb } = require('../domains/customer.js')
const { PrismaClientKnownRequestError } = require("@prisma/client");
const {
createCustomerDb,
updatedCustomerdb,
} = 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.
Expand All @@ -22,9 +21,9 @@ const createCustomer = async (req, res) => {
// 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)
const createdCustomer = await createCustomerDb(name, phone, email);

res.status(201).json({ customer: createdCustomer })
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
Expand All @@ -35,14 +34,44 @@ const createCustomer = async (req, res) => {
// 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" })
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: e.message });
}
}
};

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

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

const updatedCustomer = await updatedCustomerdb(id, name, contact);

res.status(201).json({ customer: updatedCustomer });
} catch (err) {
if (err instanceof PrismaClientKnownRequestError) {
if (err.code === "P2025") {
return res.status(404).json({
error: "A customer with that id does not exist",
});
}
}
//This error message will run if something goes wrong with the errors.
res.status(500).json({ error: err.message });
}
};

module.exports = {
createCustomer
}
createCustomer,
updateCustomer,
};
116 changes: 116 additions & 0 deletions src/controllers/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const { PrismaClientKnownRequestError } = require("@prisma/client");
const prisma = require("../utils/prisma");


const {
getAllMoviesdb,
createdMoviedb,
getMoviedb,
updatedMoviedb,
} = require("../domains/movies");

const getAllMovies = async (req, res) => {
try {
const runtimeLt = Number(req.query.runtimeLt);
const runtimeGt = Number(req.query.runtimeGt);

const allMovies = await getAllMoviesdb(runtimeLt, runtimeGt);

res.status(200).json({ movies: allMovies });
} catch (err) {
console.log("Error:", err);
}
};

const createMovie = async (req, res) => {
const { title, runtimeMins, screenings } = req.body;

if (!title || !runtimeMins) {
return res.status(400).json({
error: "Missing fields in request body",
});
}

try {
const createdMovie = await createdMoviedb(title, runtimeMins, screenings);

res.status(201).json({ movie: createdMovie });
} catch (err) {
if (err instanceof PrismaClientKnownRequestError) {
if (err.code === "P2002") {
return res.status(409).json({
error: "A movie with the provided title already exists",
});
}
}
res.status(500).json({ error: err.message });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we'd typically send back just a 400 - bad request? but 500 is ok

}
};

const getMovieByID = async (req, res) => {
try {
const id = req.params.id;

const movie = await getMoviedb(id);
res.status(200).json({ movie: movie });
} catch (err) {
if (err instanceof PrismaClientKnownRequestError) {
if (err.code === "P2025") {
return res.status(404).json({
error: "A movie with that id does not exist",
});
}
}
res.status(500).json({ error: err.message });
}
};

const updateMovie = async (req, res) => {
const id = Number(req.params.id);
const { title, runtimeMins, screenings } = req.body;

if (!title || !runtimeMins) {
return res.status(400).json({
error: "Missing fields in request body",
});
}

const existingTitle = await prisma.movie.findUnique({
where: {
title: title,
},
});

if(existingTitle) {
return res.status(409).json({
error: "Title already exists",
});
}

try {
const updatedMovie = await updatedMoviedb(
id,
title,
runtimeMins,
screenings
);

res.status(201).json({ movie: updatedMovie });
} catch (err) {
if (err instanceof PrismaClientKnownRequestError) {
if (err.code === "P2025") {
return res.status(404).json({
error: "A movie with that id does not exist",
});
}
}
res.status(500).json({ error: err.message });
}
};

module.exports = {
getAllMovies,
createMovie,
getMovieByID,
updateMovie,
};
31 changes: 31 additions & 0 deletions src/controllers/screens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { PrismaClientKnownRequestError } = require("@prisma/client");
const { createdScreendb } = require("../domains/screens");

const createScreen = async (req, res) => {
const { number, screenings } = req.body;

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

try {
const createdScreen = await createdScreendb(number, screenings);

res.status(201).json({ screen: createdScreen });
} catch (err) {
if (err instanceof PrismaClientKnownRequestError) {
if (err.code === "P2002") {
return res.status(409).json({
error: "A screen with the provided number already exists",
});
}
}
res.status(500).json({ error: err.message });
}
};

module.exports = {
createScreen,
};
31 changes: 31 additions & 0 deletions src/controllers/tickets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { PrismaClientKnownRequestError } = require("@prisma/client");
const { createdTicketdb } = require("../domains/tickets");

const createTicket = async (req, res) => {
const { screeningId, customerId } = req.body;
if (!screeningId || !customerId) {
return res.status(400).json({
error: "Missing fields in request body",
});
}

try {
const createdTicket = await createdTicketdb(screeningId, customerId);

res.status(201).json({ ticket: createdTicket });
} catch (err) {
if (err instanceof PrismaClientKnownRequestError) {
if (err.code === "P2003") {
return res.status(404).json({
error: "A customer or screening does not exist with the provided id",
});
}
}
res.status(500).json({ error: err.message });

}
};

module.exports = {
createTicket,
};
28 changes: 27 additions & 1 deletion src/domains/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ const createCustomerDb = async (name, phone, email) => await prisma.customer.cre
}
})

const updatedCustomerdb = async (id, name, contact) => {
const customerData = {
name,
};

if (contact) {
customerData.contact = {
update: {
data: contact,
},
};
}

return await prisma.customer.update({
data: customerData,
where: {
id,
},
include: {
contact: true,
},
});
};


module.exports = {
createCustomerDb
createCustomerDb,
updatedCustomerdb
}
Loading