feat: solution exercise 4
This commit is contained in:
parent
f5070142cb
commit
971e366a1b
22
src/data.js
22
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 = {
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
}
|
||||
};
|
|
@ -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
|
||||
}
|
||||
`;
|
Loading…
Reference in New Issue