-
Notifications
You must be signed in to change notification settings - Fork 140
Leonardo Lodi #133
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
LeonardoSaraceli
wants to merge
7
commits into
boolean-uk:main
Choose a base branch
from
LeonardoSaraceli: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
Leonardo Lodi #133
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a8951d
add movie, screen and customer routers, controllers and domains files
188c2ed
add error handling and new info for http methods
d15a573
add movie tests
d048259
add screen and ticket tests
9821ba5
add get all movies http method only with future screenings
7a255b0
add review model, route, controller, domain and errors to it
effcdc5
add tests for review model
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
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,16 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "Review" ( | ||
| "id" SERIAL 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; |
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20240702164900_reviews_add_content_column/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: | ||
|
|
||
| - Added the required column `content` to the `Review` table without a default value. This is not possible if the table is not empty. | ||
|
|
||
| */ | ||
| -- AlterTable | ||
| ALTER TABLE "Review" ADD COLUMN "content" TEXT NOT NULL; |
11 changes: 11 additions & 0 deletions
11
prisma/migrations/20240702180147_cascade_deletes/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,11 @@ | ||
| -- DropForeignKey | ||
| ALTER TABLE "Review" DROP CONSTRAINT "Review_customerId_fkey"; | ||
|
|
||
| -- DropForeignKey | ||
| ALTER TABLE "Review" DROP CONSTRAINT "Review_movieId_fkey"; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Review" ADD CONSTRAINT "Review_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "Review" ADD CONSTRAINT "Review_movieId_fkey" FOREIGN KEY ("movieId") REFERENCES "Movie"("id") ON DELETE CASCADE 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,105 +1,150 @@ | ||
| const { PrismaClient } = require('@prisma/client'); | ||
| const prisma = new PrismaClient(); | ||
| const { PrismaClient } = require("@prisma/client") | ||
| const prisma = new PrismaClient() | ||
|
|
||
| async function seed() { | ||
| await createCustomer(); | ||
| const movies = await createMovies(); | ||
| const screens = await createScreens(); | ||
| await createScreenings(screens, movies); | ||
| await createCustomer() | ||
| const movies = await createMovies() | ||
| const screens = await createScreens() | ||
| await createScreenings(screens, movies) | ||
| await creteReviews() | ||
|
|
||
| process.exit(0); | ||
| process.exit(0) | ||
| } | ||
|
|
||
| async function createCustomer() { | ||
| const customer = await prisma.customer.create({ | ||
| data: { | ||
| name: 'Alice', | ||
| name: "Alice", | ||
| contact: { | ||
| create: { | ||
| email: 'alice@boolean.co.uk', | ||
| phone: '1234567890' | ||
| } | ||
| } | ||
| email: "alice@boolean.co.uk", | ||
| phone: "1234567890", | ||
| }, | ||
| }, | ||
| }, | ||
| include: { | ||
| contact: true | ||
| } | ||
| }); | ||
| contact: true, | ||
| }, | ||
| }) | ||
|
|
||
| console.log('Customer created', customer); | ||
| console.log("Customer created", customer) | ||
|
|
||
| return customer; | ||
| return customer | ||
| } | ||
|
|
||
| async function createMovies() { | ||
| const rawMovies = [ | ||
| { title: 'The Matrix', runtimeMins: 120 }, | ||
| { title: 'Dodgeball', runtimeMins: 154 }, | ||
| ]; | ||
| { title: "The Matrix", runtimeMins: 120 }, | ||
| { title: "Dodgeball", runtimeMins: 154 }, | ||
| ] | ||
|
|
||
| const movies = []; | ||
| const movies = [] | ||
|
|
||
| for (const rawMovie of rawMovies) { | ||
| const movie = await prisma.movie.create({ data: rawMovie }); | ||
| movies.push(movie); | ||
| const movie = await prisma.movie.create({ data: rawMovie }) | ||
| movies.push(movie) | ||
| } | ||
|
|
||
| console.log('Movies created', movies); | ||
| console.log("Movies created", movies) | ||
|
|
||
| return movies; | ||
| return movies | ||
| } | ||
|
|
||
| async function createScreens() { | ||
| const rawScreens = [ | ||
| { number: 1 }, { number: 2 } | ||
| ]; | ||
| const rawScreens = [{ number: 1 }, { number: 2 }] | ||
|
|
||
| const screens = []; | ||
| const screens = [] | ||
|
|
||
| for (const rawScreen of rawScreens) { | ||
| const screen = await prisma.screen.create({ | ||
| data: rawScreen | ||
| }); | ||
| data: rawScreen, | ||
| }) | ||
|
|
||
| console.log('Screen created', screen); | ||
| console.log("Screen created", screen) | ||
|
|
||
| screens.push(screen); | ||
| screens.push(screen) | ||
| } | ||
|
|
||
| return screens; | ||
| return screens | ||
| } | ||
|
|
||
| async function createScreenings(screens, movies) { | ||
| const screeningDate = new Date(); | ||
| const screeningDate = new Date() | ||
|
|
||
| for (const screen of screens) { | ||
| for (let i = 0; i < movies.length; i++) { | ||
| screeningDate.setDate(screeningDate.getDate() + i); | ||
| screeningDate.setDate(screeningDate.getDate() + i) | ||
|
|
||
| const screening = await prisma.screening.create({ | ||
| data: { | ||
| startsAt: screeningDate, | ||
| movie: { | ||
| connect: { | ||
| id: movies[i].id | ||
| } | ||
| id: movies[i].id, | ||
| }, | ||
| }, | ||
| screen: { | ||
| connect: { | ||
| id: screen.id | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| id: screen.id, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| console.log('Screening created', screening); | ||
| console.log("Screening created", screening) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function creteReviews() { | ||
| const customer = await prisma.customer.create({ | ||
| data: { | ||
| name: "Leo", | ||
| contact: { | ||
| create: { | ||
| email: "leo@boolean.co.pt", | ||
| phone: "1234567890", | ||
| }, | ||
| }, | ||
| }, | ||
| include: { | ||
| contact: true, | ||
| }, | ||
| }) | ||
|
|
||
| const movie = await prisma.movie.create({ | ||
| data: { | ||
| title: "Cars", | ||
| runtimeMins: 130, | ||
| screenings: { | ||
| create: [ | ||
| { | ||
| screenId: 1, | ||
| startsAt: "2024-07-02T17:05:34.644Z", | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const review = await prisma.review.create({ | ||
| data: { | ||
| customerId: customer.id, | ||
| movieId: movie.id, | ||
| content: "Great movie!", | ||
| }, | ||
| include: { | ||
| customer: true, | ||
| movie: true, | ||
| }, | ||
| }) | ||
|
|
||
| console.log("Review created", review) | ||
| } | ||
|
|
||
| seed() | ||
| .catch(async e => { | ||
| console.error(e); | ||
| await prisma.$disconnect(); | ||
| .catch(async (e) => { | ||
| console.error(e) | ||
| await prisma.$disconnect() | ||
| }) | ||
| .finally(() => process.exit(1)); | ||
| .finally(() => process.exit(1)) |
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,28 @@ | ||
| const { PrismaClientKnownRequestError } = require("@prisma/client") | ||
| const { createCustomerDb } = require('../domains/customer.js') | ||
| const { | ||
| createCustomerDb, | ||
| updateCostumerDb, | ||
| } = require("../domains/customer.js") | ||
|
|
||
| const createCustomer = async (req, res) => { | ||
| const { | ||
| name, | ||
| phone, | ||
| } = req.body | ||
| const { name, phone, email } = req.body | ||
| const customer = await createCustomerDb(name, phone, email) | ||
|
|
||
| if (!name || !phone || !email) { | ||
| return res.status(400).json({ | ||
| 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, | ||
| }) | ||
| } | ||
|
|
||
| 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" }) | ||
| } | ||
| } | ||
| const updateCostumer = async (req, res) => { | ||
| const paramsId = Number(req.params.id) | ||
| const { name, contact } = req.body | ||
| const customer = await updateCostumerDb(paramsId, name, contact) | ||
|
|
||
| res.status(500).json({ error: e.message }) | ||
| } | ||
| res.status(201).json({ | ||
| customer, | ||
| }) | ||
| } | ||
|
|
||
| module.exports = { | ||
| createCustomer | ||
| createCustomer, | ||
| updateCostumer, | ||
| } | ||
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.
What if this is not a number?
Always validate everything the client gives you