-
Notifications
You must be signed in to change notification settings - Fork 140
Farshad Bagdeli #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Farshad Bagdeli #136
Changes from all commits
95fb0f4
a00fc5e
c7a109a
8aee5eb
b98b7f4
5a07641
6f995cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
|
@@ -43,6 +43,23 @@ const createCustomer = async (req, res) => { | |
| } | ||
| } | ||
|
|
||
| const updateCustomer = async (req, res) => { | ||
| const id = Number(req.params.id) | ||
| const { name } = req.body | ||
|
|
||
| if (!id || !name) { | ||
| return res.status(400).json({error : 'Fields are not correct!'}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!'}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'Internal server error' |
||
| } | ||
| } | ||
| module.exports = { | ||
| createCustomer | ||
| createCustomer, updateCustomer | ||
| } | ||
| 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!'}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
| 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} |
| 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 | ||
| } |
| 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} |
| 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; |
| 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 |
| 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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') )