Skip to content

Commit

Permalink
#22: API endpoints
Browse files Browse the repository at this point in the history
 - reworked API docs,
 - server
 - client
  • Loading branch information
dartandrevinsky committed Jun 29, 2021
1 parent 5fc43e6 commit 5b8cb56
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 46 deletions.
6 changes: 3 additions & 3 deletions server/src/api/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const addressResource = ({ config, db }) => resource({
create(req, res) {

const { body } = req;
const { address, time, origin } = body;
if (!address || !time || !origin) {
res.status(400).json({
const { address, time } = body;
if (!address || !time) {
return res.status(400).json({
message: `Missing field`,
code: 400,
errors: {
Expand Down
12 changes: 6 additions & 6 deletions server/src/api/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ const cartResource = ({ config, db }) => resource({
/** Property name to store preloaded entity on `request`. */
id: facetName,

/** POST / - Create a new entity */
create(req, res) {
index({ params }, res) {
inMemoCart.current = Object.assign({}, initialCart, {
id: String(new Date().getTime()),
subtotal: 0.0,
items: []
});

res.json(inMemoCart.current);
},


/** PUT /:id - Update a given entity */
update({ body, ...req }, res) {

Expand All @@ -35,15 +35,15 @@ const cartResource = ({ config, db }) => resource({
const result = {
id,
count: body.qty,
meta: { restaurantId: body.restaurantId }
restaurantId: body.restaurantId
};
inMemoCart.current.items = [
...inMemoCart.current.items,
result
];
console.log(JSON.stringify(inMemoCart.current.items, null, 2));
return new Promise(rs => setTimeout(() => {
res.send(result);
res.send(inMemoCart.current);
rs();
}, 2000));
} else {
Expand All @@ -53,7 +53,7 @@ const cartResource = ({ config, db }) => resource({
// res.send(result);
console.log(JSON.stringify(inMemoCart.current.items, null, 2));
return new Promise(rs => setTimeout(() => {
res.send(result);
res.send(inMemoCart.current);
rs();
}, 2000));
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export default ({ config, db }) => {
let api = Router();

// mount the facets resource
api.use('/address', addresses({ config, db }));
api.use('/restaurants', restaurants({ config, db }));
api.use('/cart/address', addresses({ config, db }));
api.use('/cart', cart({ config, db }));
api.use('/restaurants', restaurants({ config, db }));

// perhaps expose some API metadata at the root
api.get('/', (req, res) => {
Expand Down
14 changes: 13 additions & 1 deletion server/src/models/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ const restaurants = [
'id': '121721',
'name': 'Nandos Banani',
'address': 'Road-11, Banani, Dhaka',
street1: '',
street2: '',
locality: 'city',
region: 'state',
postcode: '',
country: '',
"cuisine": "Indian",
'delivery-fee': 75.00,
avgDeliveryTime: 60
}, {
'id': '6317637',
'name': 'Le Petit Souffle',
"cuisine": "Indian",
'address': 'Third Floor, Century City Mall, Kalayaan Avenue, Poblacion, Makati City',
'delivery-fee': 75.00,
avgDeliveryTime: 60
Expand All @@ -25,24 +33,28 @@ const restaurants = [
'id': '6304287',
'name': 'Izakaya Kikufuji',
'address': 'Little Tokyo, 2277 Chino Roces Avenue, Legaspi Village, Makati City',
"cuisine": "Fast Food",
'delivery-fee': 75.00,
avgDeliveryTime: 60
}, {
'id': '6300002',
'name': 'Heat - Edsa Shangri-La',
'address': 'Edsa Shangri-La, 1 Garden Way, Ortigas, Mandaluyong City\n',
'address': 'Edsa Shangri-La, 1 Garden Way, Ortigas, Mandaluyong City',
"cuisine": "Fast Food",
'delivery-fee': 75.00,
avgDeliveryTime: 60
}, {
'id': '6318506',
'name': 'Ooma',
'address': 'Third Floor, Mega Fashion Hall, SM Megamall, Ortigas, Mandaluyong City',
"cuisine": "Fast Food",
'delivery-fee': 75.00,
avgDeliveryTime: 60
}, {
'id': '63185068',
'name': 'All-Ooma - All items',
'address': 'Fourth Floor, Mega Fashion Hall, SM Megamall, Ortigas, Mandaluyong City',
"cuisine": "Oriental",
'delivery-fee': 80.00,
avgDeliveryTime: 55
},
Expand Down
17 changes: 0 additions & 17 deletions server/src/models/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const menu = [
"position": 1,
"price": "250.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Appeteasers",
"discount": {
"type": "",
Expand All @@ -21,7 +20,6 @@ const menu = [
"position": 1,
"price": "250.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Appeteasers",
"discount": {
"type": "",
Expand All @@ -35,7 +33,6 @@ const menu = [
"position": 1,
"price": "215.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Appeteasers",
"discount": {
"type": "",
Expand All @@ -49,7 +46,6 @@ const menu = [
"position": 1,
"price": "215.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Appeteasers",
"discount": {
"type": "",
Expand All @@ -63,7 +59,6 @@ const menu = [
"position": 1,
"price": "205.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Appeteasers",
"discount": {
"type": "",
Expand All @@ -77,7 +72,6 @@ const menu = [
"position": 1,
"price": "615.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Appeteasers",
"discount": {
"type": "",
Expand All @@ -91,7 +85,6 @@ const menu = [
"position": 1,
"price": "520.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Appeteasers",
"discount": {
"type": "",
Expand All @@ -105,7 +98,6 @@ const menu = [
"position": 1,
"price": "190.00",
"consumable": "1:1",
"cuisine_name": "Fast Food",
"category_name": "Fino sides",
"discount": {
"type": "",
Expand All @@ -119,7 +111,6 @@ const menu = [
"position": 2,
"price": "330.00",
"consumable": "1:1",
"cuisine_name": "Fast Food",
"category_name": "Fino sides",
"discount": {
"type": "",
Expand All @@ -133,7 +124,6 @@ const menu = [
"position": 1,
"price": "190.00",
"consumable": "1:1",
"cuisine_name": "Fast Food",
"category_name": "Fino sides",
"discount": {
"type": "",
Expand All @@ -147,7 +137,6 @@ const menu = [
"position": 2,
"price": "330.00",
"consumable": "1:1",
"cuisine_name": "Fast Food",
"category_name": "Fino sides",
"discount": {
"type": "",
Expand All @@ -161,7 +150,6 @@ const menu = [
"position": 1,
"price": "385.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Peri-peri chicken",
"discount": {
"type": "",
Expand All @@ -175,7 +163,6 @@ const menu = [
"position": 2,
"price": "485.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Peri-peri chicken",
"discount": {
"type": "",
Expand All @@ -189,7 +176,6 @@ const menu = [
"position": 3,
"price": "575.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Peri-peri chicken",
"discount": {
"type": "",
Expand All @@ -203,7 +189,6 @@ const menu = [
"position": 1,
"price": "685.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Peri-peri chicken",
"discount": {
"type": "",
Expand All @@ -217,7 +202,6 @@ const menu = [
"position": 2,
"price": "785.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Peri-peri chicken",
"discount": {
"type": "",
Expand All @@ -231,7 +215,6 @@ const menu = [
"position": 3,
"price": "875.00",
"consumable": "1:1",
"cuisine_name": "Indian",
"category_name": "Peri-peri chicken",
"discount": {
"type": "",
Expand Down
12 changes: 6 additions & 6 deletions src/features/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const urlResolver = obtainFQDNUrl(API_URL, window.location);

const apiRoutes = prepareRoutesForFetch({
postAddressObtainRestaurants: [
`POST /address`,
(address, time, origin) => ({ address, time, origin })
`POST /cart/address`,
(address, time) => ({ address, time })
],
getRestaurantById: restaurantId => `/restaurants/${ restaurantId }`,
postCreateNewCart: `POST /cart`,
getCart: `GET /cart`,
putUpdateCartWithItem: [
(cartId, restaurantId, itemId, qty) => `PUT /cart/${ cartId }`,
(cartId, restaurantId, itemId, qty) => ({ cartId, restaurantId, itemId, qty })
(cartId, restaurantId, itemId, quantity) => `PUT /cart/${ cartId }`,
(cartId, restaurantId, itemId, quantity) => ({ cartId, restaurantId, itemId, quantity })
]
}, urlResolver);

export const { postAddressObtainRestaurants, getRestaurantById, putUpdateCartWithItem, postCreateNewCart } = apiRoutes;
export const { postAddressObtainRestaurants, getRestaurantById, putUpdateCartWithItem, getCart } = apiRoutes;

function prepareRoutesForFetch(routes, urlResolver) {
return Object.fromEntries(Array.from(Object.entries(routes), ([ k, v ]) => {
Expand Down
15 changes: 8 additions & 7 deletions src/features/cart/cartSlice.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import { postCreateNewCart, putUpdateCartWithItem } from '../actions/api';
import { getCart, putUpdateCartWithItem } from '../actions/api';

const ns = 'cart';

export const obtainNewCartAsyncThunk = createAsyncThunk(
export const obtainCartAsyncThunk = createAsyncThunk(
'cart/createNew',
async (data, { rejectWithValue, dispatch }) => {
void data;
return postCreateNewCart();
return getCart();
});

export const updateCartWithItemAsyncThunk = createAsyncThunk(
Expand All @@ -22,7 +22,8 @@ export const updateCartWithItemAsyncThunk = createAsyncThunk(
});

const initialState = {
id: null,
id: '123',
subTotal: 0,
status: null,
items: []
};
Expand All @@ -38,14 +39,14 @@ export const cartSlice = createSlice({
resetCart: () => Object.assign({}, initialState, { items: [] })
},
extraReducers: builder => builder
.addCase(obtainNewCartAsyncThunk.pending, (state, { payload }) => {
.addCase(obtainCartAsyncThunk.pending, (state, { payload }) => {
state.status = 'pending';
})
.addCase(obtainNewCartAsyncThunk.fulfilled,
.addCase(obtainCartAsyncThunk.fulfilled,
(state, { payload, ...rest }) => {
return Object.assign({}, state, payload, { status: 'ready' });
})
.addCase(obtainNewCartAsyncThunk.rejected, (state, { payload, ...rest }) => {
.addCase(obtainCartAsyncThunk.rejected, (state, { payload, ...rest }) => {
state.status = 'error';
state.items = [];
})
Expand Down
8 changes: 4 additions & 4 deletions src/ui/pages/RestaurantPage/menuItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
accessCart,
accessCartItems,
accessCartStatus,
obtainNewCartAsyncThunk
obtainCartAsyncThunk
} from '../../../features/cart/cartSlice';
import React, { useCallback, useEffect, useMemo } from 'react';
import { retrieveRestaurantByIdAsyncThunk } from '../../../features/address/addressSlice';
Expand All @@ -23,7 +23,7 @@ import { e2eAssist } from '../../../testability';
'position': 1,
'price': '250.00',
'consumable': '1:1',
'cuisine_name': 'Indian',
'cuisine': 'Indian',
'category_name': 'Appeteasers',
'discount': {
'type': '',
Expand Down Expand Up @@ -64,7 +64,7 @@ export function MenuItems({ restaurantId }) {
if (cartStatus) {
return;
}
dispatch(obtainNewCartAsyncThunk());
dispatch(obtainCartAsyncThunk());
}, [ cartStatus, dispatch ]);


Expand All @@ -90,7 +90,7 @@ export function MenuItems({ restaurantId }) {
text: 'Food Item',
sort: true
}, {
dataField: 'cuisine_name',
dataField: 'cuisine',
text: 'Cuisine',
sort: true
}, {
Expand Down

0 comments on commit 5b8cb56

Please sign in to comment.