summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/54
diff options
context:
space:
mode:
Diffstat (limited to 'stepped-solutions/54')
-rwxr-xr-xstepped-solutions/54/backend/src/resolvers/Query.js69
-rwxr-xr-xstepped-solutions/54/backend/src/schema.graphql38
-rwxr-xr-xstepped-solutions/54/frontend/components/OrderList.js80
-rwxr-xr-xstepped-solutions/54/frontend/pages/orders.js12
4 files changed, 199 insertions, 0 deletions
diff --git a/stepped-solutions/54/backend/src/resolvers/Query.js b/stepped-solutions/54/backend/src/resolvers/Query.js
new file mode 100755
index 0000000..22a6414
--- /dev/null
+++ b/stepped-solutions/54/backend/src/resolvers/Query.js
@@ -0,0 +1,69 @@
+const { forwardTo } = require('prisma-binding');
+const { hasPermission } = require('../utils');
+
+const Query = {
+ items: forwardTo('db'),
+ item: forwardTo('db'),
+ itemsConnection: forwardTo('db'),
+ me(parent, args, ctx, info) {
+ // check if there is a current user ID
+ if (!ctx.request.userId) {
+ return null;
+ }
+ return ctx.db.query.user(
+ {
+ where: { id: ctx.request.userId },
+ },
+ info
+ );
+ },
+ async users(parent, args, ctx, info) {
+ // 1. Check if they are logged in
+ if (!ctx.request.userId) {
+ throw new Error('You must be logged in!');
+ }
+ console.log(ctx.request.userId);
+ // 2. Check if the user has the permissions to query all the users
+ hasPermission(ctx.request.user, ['ADMIN', 'PERMISSIONUPDATE']);
+
+ // 2. if they do, query all the users!
+ return ctx.db.query.users({}, info);
+ },
+ async order(parent, args, ctx, info) {
+ // 1. Make sure they are logged in
+ if (!ctx.request.userId) {
+ throw new Error('You arent logged in!');
+ }
+ // 2. Query the current order
+ const order = await ctx.db.query.order(
+ {
+ where: { id: args.id },
+ },
+ info
+ );
+ // 3. Check if the have the permissions to see this order
+ const ownsOrder = order.user.id === ctx.request.userId;
+ const hasPermissionToSeeOrder = ctx.request.user.permissions.includes('ADMIN');
+ if (!ownsOrder || !hasPermission) {
+ throw new Error('You cant see this buddd');
+ }
+ // 4. Return the order
+ return order;
+ },
+ async orders(parent, args, ctx, info) {
+ const { userId } = ctx.request;
+ if (!userId) {
+ throw new Error('you must be signed in!');
+ }
+ return ctx.db.query.orders(
+ {
+ where: {
+ user: { id: userId },
+ },
+ },
+ info
+ );
+ },
+};
+
+module.exports = Query;
diff --git a/stepped-solutions/54/backend/src/schema.graphql b/stepped-solutions/54/backend/src/schema.graphql
new file mode 100755
index 0000000..d9ab917
--- /dev/null
+++ b/stepped-solutions/54/backend/src/schema.graphql
@@ -0,0 +1,38 @@
+# import * from './generated/prisma.graphql'
+
+type SuccessMessage {
+ message: String
+}
+
+type Mutation {
+ createItem(title: String, description: String, price: Int, image: String, largeImage: String): Item!
+ updateItem(id: ID!, title: String, description: String, price: Int): Item!
+ deleteItem(id: ID!): Item
+ signup(email: String!, password: String!, name: String!): User!
+ signin(email: String!, password: String!): User!
+ signout: SuccessMessage
+ requestReset(email: String!): SuccessMessage
+ resetPassword(resetToken: String!, password: String!, confirmPassword: String!): User!
+ updatePermissions(permissions: [Permission], userId: ID!): User
+ addToCart(id: ID!): CartItem
+ removeFromCart(id: ID!): CartItem
+ createOrder(token: String!): Order!
+}
+
+type Query {
+ items(where: ItemWhereInput, orderBy: ItemOrderByInput, skip: Int, first: Int): [Item]!
+ item(where: ItemWhereUniqueInput!): Item
+ itemsConnection(where: ItemWhereInput): ItemConnection!
+ me: User
+ users: [User]!
+ order(id: ID!): Order
+ orders(orderBy: OrderOrderByInput): [Order]!
+}
+
+type User{
+ id: ID!
+ name: String!
+ email: String!
+ permissions: [Permission!]!
+ cart: [CartItem!]!
+}
diff --git a/stepped-solutions/54/frontend/components/OrderList.js b/stepped-solutions/54/frontend/components/OrderList.js
new file mode 100755
index 0000000..e2e22ff
--- /dev/null
+++ b/stepped-solutions/54/frontend/components/OrderList.js
@@ -0,0 +1,80 @@
+import React from 'react';
+import { Query } from 'react-apollo';
+import { formatDistance } from 'date-fns';
+import Link from 'next/link';
+import styled from 'styled-components';
+import gql from 'graphql-tag';
+import Error from './ErrorMessage';
+import formatMoney from '../lib/formatMoney';
+import OrderItemStyles from './styles/OrderItemStyles';
+
+const USER_ORDERS_QUERY = gql`
+ query USER_ORDERS_QUERY {
+ orders(orderBy: createdAt_DESC) {
+ id
+ total
+ createdAt
+ items {
+ id
+ title
+ price
+ description
+ quantity
+ image
+ }
+ }
+ }
+`;
+
+const orderUl = styled.ul`
+ display: grid;
+ grid-gap: 4rem;
+ grid-template-columns: repeat(auto-fit, minmax(40%, 1fr));
+`;
+
+class OrderList extends React.Component {
+ render() {
+ return (
+ <Query query={USER_ORDERS_QUERY}>
+ {({ data: { orders }, loading, error }) => {
+ if (loading) return <p>loading...</p>;
+ if (error) return <Error erorr={error} />;
+ console.log(orders);
+ return (
+ <div>
+ <h2>You have {orders.length} orders</h2>
+ <orderUl>
+ {orders.map(order => (
+ <OrderItemStyles key={order.id}>
+ <Link
+ href={{
+ pathname: '/order',
+ query: { id: order.id },
+ }}
+ >
+ <a>
+ <div className="order-meta">
+ <p>{order.items.reduce((a, b) => a + b.quantity, 0)} Items</p>
+ <p>{order.items.length} Products</p>
+ <p>{formatDistance(order.createdAt, new Date())}</p>
+ <p>{formatMoney(order.total)}</p>
+ </div>
+ <div className="images">
+ {order.items.map(item => (
+ <img key={item.id} src={item.image} alt={item.title} />
+ ))}
+ </div>
+ </a>
+ </Link>
+ </OrderItemStyles>
+ ))}
+ </orderUl>
+ </div>
+ );
+ }}
+ </Query>
+ );
+ }
+}
+
+export default OrderList;
diff --git a/stepped-solutions/54/frontend/pages/orders.js b/stepped-solutions/54/frontend/pages/orders.js
new file mode 100755
index 0000000..c65d80d
--- /dev/null
+++ b/stepped-solutions/54/frontend/pages/orders.js
@@ -0,0 +1,12 @@
+import PleaseSignIn from '../components/PleaseSignIn';
+import OrderList from '../components/OrderList';
+
+const OrderPage = props => (
+ <div>
+ <PleaseSignIn>
+ <OrderList />
+ </PleaseSignIn>
+ </div>
+);
+
+export default OrderPage;