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
21 changes: 19 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, updateCustomerDb } = require('../domains/customer.js')

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

const updateCustomer = async (req, res) => {
const id = Number(req.params.id)
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if req.params.id is not a number

Copy link
Contributor

Choose a reason for hiding this comment

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

function isNumber(num) {
return !isNaN(num)
}

console.log( isNumber('hello') )

const { name } = req.body

if (!id || !name) {
return res.status(400).json({error : 'Fields are not correct!'})
Copy link
Contributor

Choose a reason for hiding this comment

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

'Must provide a name'

}
try {
const updatedCustomer = await updateCustomerDb(id, name)
if(!updatedCustomer) {
return res.status(404).json('Customer didnt Find!')
Copy link
Contributor

Choose a reason for hiding this comment

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

'Customer not found'

}
res.status(201).json({customer : updatedCustomer})
} catch (error) {
res.status(500).json({error : 'something Went Wrong!'})
Copy link
Contributor

Choose a reason for hiding this comment

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

'Internal server error'

}
}
module.exports = {
createCustomer
createCustomer, updateCustomer
}
57 changes: 57 additions & 0 deletions src/controllers/movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { getAllMoviesDb, createdMovieDb, getMovieDb, updateMovieDb } = require('../domains/movie.js')

const getAllMovies = async (req, res) => {
try {
const movies = await getAllMoviesDb()
res.status(200).json({movies : movies})
} catch (e) {
res.status(500).json({error : 'something went Wrong!'})
}
}

const createdMovie = async (req, res) => {
const { title , runtimeMins } = req.body
try {
const newMovie = await createdMovieDb(title, runtimeMins)
res.status(201).json({movie : newMovie})
} catch (error) {
res.status(500).json({error : 'coulnt create new Movie at controller!'})
}
}

const getMovieById = async (req, res) => {
const id = Number(req.params.id)
try {
const movie = await getMovieDb(id)
res.status(200).json({movie : movie})
} catch (error) {
res.status(500).json({error: 'could not found the movie!'})
}
}

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

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

try {
const updatedMovie = await updateMovieDb(id, title, runtimeMins)
if(!updatedMovie) {
res.status(404).json({error : 'Movie not found!'})
Copy link
Contributor

Choose a reason for hiding this comment

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

return res.status(404).json({error : 'Movie not found!'})

}
res.status(201).json({ movie : updatedMovie})
} catch (error) {
res.status(500).json({error : 'Could not update the movie!'})
}
}


module.exports = {
getAllMovies,
createdMovie,
getMovieById,
updateMovie
}
14 changes: 14 additions & 0 deletions src/controllers/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { createScreenDb } = require('../domains/screen.js')

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

try {
const createdScreen = await createScreenDb(number)
res.status(201).json({screen : createdScreen})
} catch (error) {
res.status(500).json({error : 'Something Went Wrong!'})
}
}

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

const updateCustomerDb = async (id, name) => {
try {
const updatedCustomer = await prisma.customer.update({
where : {id : id},
data : {
name
},
include : {
contact : true
}
})
return updatedCustomer
} catch (error) {
throw new Error ('Failed to update customer at DB!')
}
}

module.exports = {
createCustomerDb
createCustomerDb, updateCustomerDb
}
70 changes: 70 additions & 0 deletions src/domains/movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const prisma = require('../utils/prisma')

const getAllMoviesDb = async () => {
try {
const movies = await prisma.movie.findMany({
include: {
screenings:true
}
})
return movies
} catch (error) {
throw new Error('Failed to fetch movies from database.')
}
}

const createdMovieDb = async (title, runtimeMins) => {
try {
const newMovie = await prisma.movie.create({
data: {
title,
runtimeMins
},
include : {
screenings : true
}
})
return newMovie
} catch (error) {
throw new Error('Failed to create a new movie.')
}
}

const getMovieDb = async (id) => {
try {
const movieFound = await prisma.movie.findFirst({
include : {
screenings : true
}
})
return movieFound
} catch (error) {
throw new Error ('Failed to fetch movie from database.')
}
}

const updateMovieDb = async (id, title, runtimeMins) => {

try {
const updatedMovie = await prisma.movie.update({
where: { id : id },
data : {
title,
runtimeMins
},
include : {
screenings : true
}
})
return updatedMovie
} catch (error) {
throw new Error ('Failed to update movie at DB!')
}
}

module.exports = {
getAllMoviesDb,
createdMovieDb,
getMovieDb,
updateMovieDb
}
16 changes: 16 additions & 0 deletions src/domains/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const prisma = require('../utils/prisma')

const createScreenDb = async (number) => {
try {
const screen = await prisma.screen.create({
data : {
number
}
})
return screen
} catch (error) {
throw new Error ({error : 'Something went wrong at db'})
}
}

module.exports = {createScreenDb}
5 changes: 2 additions & 3 deletions src/routers/customer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const express = require("express");
const {
createCustomer
} = require('../controllers/customer');
const { createCustomer, updateCustomer } = require('../controllers/customer');

const router = express.Router();

// In index.js, we told express that the /customer route should use this router file
// The below /register route extends that, so the end result will be a URL
// that looks like http://localhost:4040/customer/register
router.post("/register", createCustomer);
router.put('/:id', updateCustomer)

module.exports = router;
10 changes: 10 additions & 0 deletions src/routers/movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express')
const router = express.Router()

const { getAllMovies, createdMovie, getMovieById, updateMovie } = require('../controllers/movie.js')

router.get('/', getAllMovies)
router.post('/', createdMovie)
router.get('/:id', getMovieById)
router.put('/:id', updateMovie)
module.exports = router
8 changes: 8 additions & 0 deletions src/routers/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express')
const router = express.Router()

const { createScreen } = require('../controllers/screen.js')

router.post('/', createScreen)

module.exports = router
4 changes: 4 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ app.use(express.urlencoded({ extended: true }));
const customerRouter = require('./routers/customer');
app.use('/customers', customerRouter);

const movieRouter = require('./routers/movie.js')
app.use('/movies', movieRouter)

const screenRouter = require('./routers/screen.js')
app.use('/screens', screenRouter)
module.exports = app