Skip to content
Open
6 changes: 0 additions & 6 deletions .env.example

This file was deleted.

40 changes: 38 additions & 2 deletions src/controllers/customer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { PrismaClientKnownRequestError } = require("@prisma/client")
const { createCustomerDb } = require('../domains/customer.js')
const { createCustomerDb, updateCustomer, getCustomerByID } = require('../domains/customer.js')

const createCustomer = async (req, res) => {
const {
Expand Down Expand Up @@ -43,6 +43,42 @@ const createCustomer = async (req, res) => {
}
}


const updateCustomerDetails = async (req, res) => {
const id = Number(req.params.id)
const { name, contact: {phone, email} = {} } = req.body

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

const { name, contact } = req.body
const { phone, email } = contact

const foundCustomerId = await getCustomerByID(id)
if (!name) {
return res.status(400).json({
error: "Customer name field missing"
})
}
if (!foundCustomerId) {
return res.status(404).json({
error: "Customer not found with that ID, choose another"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"Customer not found with that ID"

})
}
try {
const updatedCustomer = await updateCustomer(id, name, phone, email)

res.status(201).json({
customer: updatedCustomer
})
} catch(e) {
if (e instanceof PrismaClientKnownRequestError) {
if(e.code === "P2001") {
return res.status(404).json({
error: "Customer not found with that ID, choose another"
})
}
}
res.status(500).json({
error: e.message
})
}
}

module.exports = {
createCustomer
createCustomer,
updateCustomerDetails
}
134 changes: 134 additions & 0 deletions src/controllers/movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const {
getAllMovies,
createMovie,
updateMovie,
getAllMoviesByRuntimeLt,
getAllMoviesByRuntimeGt,
getMovieByTitle,
getMovieById
} = require('../domains/movie.js')

const { PrismaClientKnownRequestError } = require('@prisma/client/runtime/library')

const getAll = async (req, res) => {
const { runtimeLt,runtimeGt } = req.query
let allMovies

if(runtimeGt) {
allMovies = await getAllMoviesByRuntimeGt(Number(runtimeGt))
}
if (runtimeLt) {
allMovies = await getAllMoviesByRuntimeLt(Number(runtimeLt))
}
allMovies = await getAllMovies()

res.status(200).json({
movies: allMovies
})
}

const addMovie = async (req, res) => {
const {
title,
runtimeMins
} = req.body

const titleFound = await getMovieByTitle(title)

if (!title || !runtimeMins) {
return res.status(400).json({
error: "Movie title or runtimeMins field missing"
})
}
if (titleFound) {
return res.status(409).json({
error: "Movie with that title already exists"
})
}
try {
const newMovie = await createMovie(title, runtimeMins)

res.status(201).json({
movie: newMovie
})
} catch(e) {
if(e instanceof PrismaClientKnownRequestError) {
if(e.code === "P2002") {
return res.status(409).json({
error: "Movie with that title already exists"
})
}
res.status(500).json({
error: e.message
})
}
}
}

const findByID = async (req, res) => {
const id = Number(req.params.id)
const found = await getMovieById(id)
if (!found) {
return res.status(404).json({
error: "Could not find movie with that ID"
})
} else {
res.status(200).json({
movie: found
})
}
}

const updateMovieByID = async (req, res) => {
const {
title,
runtimeMins
} = req.body
const id = Number(req.params.id)
const foundId = await getMovieById(id)
const foundTitle = await getMovieByTitle(title)
if (!title || !runtimeMins) {
return res.status(400).json({
error: "Movie title or runtimeMins field missing"
})
}
if (!foundId) {
return res.status(404).json({
error: "Could not find movie with that ID"
})
}
if (foundTitle) {
return res.status(409).json({
error: "Movie with that title already exists"
})
}
try {
const updatedMovie = await updateMovie(id, title,
runtimeMins)
res.status(201).json({
movie: updatedMovie
})
} catch(e) {
if (e instanceof PrismaClientKnownRequestError) {
if(e.code === "P2002") {
return res.status(409).json({
error: "Movie with that title already exists"
})
} else if (e.code === "P2001") {
return res.status(404).json({
error: "Could not find movie with that ID"
})
}
}
res.status(500).json({
error: e.message
})
}
}

module.exports = {
getAll,
addMovie,
findByID,
updateMovieByID
}
36 changes: 36 additions & 0 deletions src/controllers/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { PrismaClientKnownRequestError } = require("@prisma/client/runtime/library")
const { createScreen, findScreen } = require("../domains/screen")

const addScreen = async (req, res) => {
const { number } = req.body
const screenFound = await findScreen(number)
if (!number) {
return res.status(400).json({
error: "Number field missing from screenings"
})
}
if (screenFound) {
return res.status(409).json({
error: "This screen already exists please select another screen"
})
}
try {
const createdScreen = await createScreen(req)

res.status(201).json({
screen: createdScreen
})
} catch(e) {
if(e instanceof PrismaClientKnownRequestError) {
if(e.code === "P2002") {
return res.status(409).json({
error: "This screen already exists please select another screen"
})
}
}
}
}

module.exports = {
addScreen
}
45 changes: 45 additions & 0 deletions src/controllers/ticket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { error } = require("console")
const { getCustomerByID } = require("../domains/customer")
const { findScreeningById } = require("../domains/screen")
const { createTicket } = require("../../test/helpers/createTicket.js")
const { PrismaClientKnownRequestError } = require("@prisma/client/runtime/library")

const createTicketController = async (req, res) => {

const {screeningId, customerId} = req.body
const foundScreenId = await findScreeningById(screeningId)
const foundCustomerId = await getCustomerByID(customerId)

if (!screeningId || !customerId) {
return res.status(400).json({
error: "Missing fields from the ticket"
})
}
if (!foundScreenId || !foundCustomerId) {
return res.status(404).json({
error: "The screen or customer does not exist, please select another ID"
})
}
try {
const createdTicket = await createTicket(screeningId, customerId)
res.status(201).json({
ticket: createdTicket
})
} catch (e) {
if(e instanceof PrismaClientKnownRequestError) {
if(e.code === "P2001") {
return res.status(404).json({
error: "The screen or customer does not exist, please select another ID"
})
}
}
res.status(500).json({
error: e.message
})
}

}

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


const updateCustomer = async (id, name, phone, email) => await prisma.customer.update({
where: {
id: id
},
data: {
name: name,
contact: {
update: {
phone: phone,
email: email
}
}
},
include: {
contact: true
}
})

const getCustomerByID = async (id) => await prisma.customer.findUnique({
where: {
id: Number(id)
},
include: {
contact: true
}
})

module.exports = {
createCustomerDb
createCustomerDb,
updateCustomer,
getCustomerByID
}
Loading