-
Notifications
You must be signed in to change notification settings - Fork 140
Will Baxter #134
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
Open
MrStashy
wants to merge
25
commits into
boolean-uk:main
Choose a base branch
from
MrStashy:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Will Baxter #134
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f6dde51
Project cloned
MrStashy 5cdb74d
Get all movies endpoint
MrStashy eef1149
Get movies with screenings
MrStashy 5948912
Get movie by id
MrStashy d338522
Update movie by id
MrStashy 649d8b2
Update customer by ID
MrStashy f405439
Create a screen
MrStashy 005bbc0
Get movies with runtime limits
MrStashy 862ece1
Reject movie POST if missing fields
MrStashy 6515051
Throw error if adding a movie with a title that already exists in db
MrStashy 23ede7f
Throw error if getting movie ID fails because no movie exists
MrStashy de3acaf
Throw errors when putting a movie with existing title, missing fields…
MrStashy b4c77a6
Update customer and contact simultaneously
MrStashy e6a0ec2
Throw error when updating a non-existant customer
MrStashy 5dea551
Throw error when updating customer without a name
MrStashy 64801d8
Updated Screens model to make numbers unique, added error handling fo…
MrStashy 9c5557f
Helpers and routes for tickets
MrStashy d02ef6b
Create tickets
MrStashy d049d98
Reject ticket creation if wrong data type, no IDs found, or no IDs pr…
MrStashy 844fc4d
Revert "Reject ticket creation if wrong data type, no IDs found, or n…
MrStashy 4c66320
Refactored to simplify ticket data validation
MrStashy 1d2038d
Refactored error handling for creatMovie to reduce DB calls
MrStashy 798b853
Refactored movies controllers to reduce DB calls
MrStashy 76d2e3c
Reviews routes and functions for get and post
MrStashy c4dbf5c
Fixed getMovies controller to rerturn screenings accurately
MrStashy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20240702092124_added_unique_contraint_screen_number/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* | ||
| Warnings: | ||
|
|
||
| - 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 "Screen_number_key" ON "Screen"("number"); |
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20240702162530_movie_title_unique_field/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* | ||
| Warnings: | ||
|
|
||
| - A unique constraint covering the columns `[title]` on the table `Movie` will be added. If there are existing duplicate values, this will fail. | ||
|
|
||
| */ | ||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "Movie_title_key" ON "Movie"("title"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "Review" ( | ||
| "id" SERIAL NOT NULL, | ||
| "content" TEXT NOT NULL, | ||
| "title" VARCHAR(100) NOT NULL, | ||
| "customerId" INTEGER NOT NULL, | ||
| "movieId" INTEGER NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "Review_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Review" ADD CONSTRAINT "Review_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Review" ADD CONSTRAINT "Review_movieId_fkey" FOREIGN KEY ("movieId") REFERENCES "Movie"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,62 @@ | ||
| const { PrismaClientKnownRequestError } = require("@prisma/client") | ||
| const { createCustomerDb } = require('../domains/customer.js') | ||
| const { PrismaClientKnownRequestError } = require("@prisma/client"); | ||
| const { | ||
| createCustomerDb, | ||
| updateCustomerByIdDb, | ||
| } = require("../domains/customer.js"); | ||
| const { DataNotFoundError, MissingFieldsError } = require("../errors/errors.js"); | ||
|
|
||
| const createCustomer = async (req, res) => { | ||
| const { | ||
| name, | ||
| phone, | ||
| } = 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 }) | ||
| 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" }) | ||
| 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 }); | ||
| } | ||
| }; | ||
|
|
||
| async function updateCustomerById(req, res) { | ||
| const id = Number(req.params.id); | ||
| if (isNaN(id)) { | ||
| throw new DataNotFoundError('No customer found with that ID') | ||
| } | ||
|
|
||
| const newProps = req.body; | ||
| if (!newProps.name) { | ||
| throw new MissingFieldsError('Customers require a name') | ||
| } | ||
|
|
||
| try { | ||
| const customer = await updateCustomerByIdDb(id, newProps); | ||
| res.status(201).json({ customer }); | ||
| } catch (e) { | ||
| if (e.code === 'P2025') { | ||
| return res.status(404).json({ error: e.message }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| createCustomer | ||
| } | ||
| createCustomer, | ||
| updateCustomerById, | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| // 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| const { PrismaClientKnownRequestError } = require("@prisma/client"); | ||
| const { | ||
| getMoviesDb, | ||
| createMovieDb, | ||
| getMovieByIdDb, | ||
| updateMovieByIdDb, | ||
| getMoviesWithQueryDb, | ||
| } = require("../domains/movies.js"); | ||
| const { | ||
| MissingFieldsError, | ||
| } = require("../errors/errors.js"); | ||
|
|
||
| async function getMovies(req, res) { | ||
| let movies; | ||
| if (Object.keys(req.query).length > 0) { | ||
| movies = await getMoviesWithQueryDb(req.query); | ||
| } else { | ||
|
|
||
| movies = await getMoviesDb(); | ||
| } | ||
| res.status(200).json({ movies }); | ||
| } | ||
|
|
||
| async function createMovie(req, res) { | ||
| const newMovie = req.body; | ||
|
|
||
| const requiredFields = ["title", "runtimeMins"]; | ||
| if (!requiredFields.every((field) => newMovie[field])) { | ||
| throw new MissingFieldsError("Movies require a title and runtime"); | ||
| } | ||
|
|
||
| try { | ||
| const movie = await createMovieDb(newMovie); | ||
| res.status(201).json({ movie }); | ||
| } catch (e) { | ||
| if (e.code === "P2002") { | ||
| res.status(409).json({ error: "A movie with that title already exists" }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function getMovieById(req, res) { | ||
| const id = Number(req.params.id); | ||
|
|
||
| try { | ||
| const movie = await getMovieByIdDb(id); | ||
| res.status(200).json({ movie }); | ||
| } catch (e) { | ||
| if (e.code === "P2025") { | ||
| res.status(404).json({ error: "No movie found with that ID" }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function updateMovieById(req, res) { | ||
| const id = Number(req.params.id); | ||
| const updatedProps = req.body; | ||
|
|
||
| if (!updatedProps.title && !updatedProps.runtimeMins) { | ||
| throw new MissingFieldsError("Updating movies requires a title or runtime"); | ||
| } | ||
|
|
||
| try { | ||
| const movie = await updateMovieByIdDb(id, updatedProps); | ||
| res.status(201).json({ movie }); | ||
| } catch (e) { | ||
| if (e.code === "P2025") { | ||
| res.status(404).json({ error: "No movie found with that ID" }); | ||
| } | ||
| if (e.code === "P2002") { | ||
| res.status(409).json({ error: "A movie with that title already exists" }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| getMovies, | ||
| createMovie, | ||
| getMovieById, | ||
| updateMovieById, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const { getAllReviewsDb, createReviewDb } = require("../domains/reviews") | ||
| const { MissingFieldsError } = require("../errors/errors") | ||
|
|
||
| async function getAllReviews(req, res) { | ||
| const reviews = await getAllReviewsDb() | ||
| res.status(200).json( {reviews} ) | ||
| } | ||
|
|
||
| async function createReview(req, res) { | ||
| const { customerId, movieId, title, content } = req.body | ||
|
|
||
| if (!customerId || !movieId || !title || !content) { | ||
| throw new MissingFieldsError("Reviews require a customer ID, movie ID, title, and content") | ||
| } | ||
|
|
||
| const review = await createReviewDb(customerId, movieId, title, content ) | ||
|
|
||
| res.status(201).json({ review }) | ||
| } | ||
|
|
||
| module.exports = { getAllReviews, createReview } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const { createScreenDb } = require('../domains/screens') | ||
| const { MissingFieldsError, DataAlreadyExistsError } = require('../errors/errors') | ||
|
|
||
| async function createScreen(req, res) { | ||
| const props = req.body | ||
|
|
||
| if (!props.number) { | ||
| throw new MissingFieldsError('All screens require a screen number') | ||
| } | ||
|
|
||
| try { | ||
| const screen = await createScreenDb(props) | ||
| res.status(201).json({ screen }) | ||
| } catch (e) { | ||
| if (e.code === 'P2002') { | ||
| throw new DataAlreadyExistsError('There is already a screen with that number') | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = { createScreen } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Top marks