diff --git a/src/data.js b/src/data.js index 7c75b95..221aaea 100644 --- a/src/data.js +++ b/src/data.js @@ -81,11 +81,29 @@ function getStoreProducts(storeId) { } function getReservationProducts(reservationId) { - return []; + return reservationProducts + // Join + .filter((rp) => rp.reservationId === reservationId) + .map((rp) => ({ + product: products.find(p => p.id === rp.productId), + quantity: rp.quantity + })); } function createReservation(reservation) { - return {}; + const reservationId = uuid(); + const newReservation = { + date: new Date(), + id: reservationId, + }; + reservations.push(newReservation); + reservationProducts = reservationProducts.concat( + reservation.reservationProducts.map((reservationProduct) => ({ + ...reservationProduct, + reservationId + }) + )); + return newReservation; } module.exports = { diff --git a/src/resolvers.js b/src/resolvers.js index 402b2c5..e302e99 100644 --- a/src/resolvers.js +++ b/src/resolvers.js @@ -47,6 +47,11 @@ module.exports = { return data.getStore(id); }, }, + Reservation: { + reservationProducts: async ({ id }) => { + return data.getReservationProducts(id); + } + }, Store: { products: async ({ id: storeId }) => { return data.getStoreProducts(storeId); @@ -62,5 +67,14 @@ module.exports = { const { city, name, number, postalCode, street } = input; return data.createStore({ city, name, number, postalCode, street }); }, + createReservation: async (parent, { input }) => { + try { + await createReservationSchema.validate(input); + } catch (e) { + throw new UserInputError('Invalid input.', { validationErrors: e.errors }); + } + const { reservationProducts } = input; + return data.createReservation({ reservationProducts }); + } } }; \ No newline at end of file diff --git a/src/typeDefs.js b/src/typeDefs.js index 25b2345..bcaba06 100644 --- a/src/typeDefs.js +++ b/src/typeDefs.js @@ -23,6 +23,17 @@ module.exports = gql` street: String } + type ReservationProduct { + product: Product + quantity: Int + } + + type Reservation { + date: String + id: String + reservationProducts: [ReservationProduct] + } + # The "Query" type is the root of all GraphQL queries. # (A "Mutation" type will be covered later on.) type Query { @@ -38,7 +49,17 @@ module.exports = gql` street: String } + input ReservationProductInput { + productId: String! + quantity: Int + } + + input ReservationInput { + reservationProducts: [ReservationProductInput] + } + type Mutation { createStore(input: StoreInput): Store + createReservation(input: ReservationInput): Reservation } `; \ No newline at end of file