Skip to content

Commit

Permalink
feat: enable retry for addItem and add more debug info
Browse files Browse the repository at this point in the history
Signed-off-by: Raymond Feng <[email protected]>
  • Loading branch information
raymondfeng committed Sep 20, 2018
1 parent ec06800 commit f2f1805
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 15 deletions.
169 changes: 166 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@loopback/repository": "^0.16.5",
"@loopback/rest": "^0.22.2",
"bcryptjs": "^2.4.3",
"debug": "^4.0.1",
"isemail": "^3.1.3",
"loopback-connector-kv-redis": "^3.0.0",
"loopback-connector-mongodb": "^3.7.1"
Expand All @@ -71,6 +72,7 @@
"@loopback/build": "^0.7.3",
"@loopback/testlab": "^0.12.2",
"@types/bcryptjs": "^2.4.1",
"@types/debug": "0.0.30",
"@types/mocha": "^5.0.0",
"@types/node": "^10.9.4",
"commitizen": "^2.10.1",
Expand Down
17 changes: 16 additions & 1 deletion src/controllers/shopping-cart.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
import {repository} from '@loopback/repository';
import {ShoppingCartRepository} from '../repositories';
import {ShoppingCart, ShoppingCartItem} from '../models';
import * as debugFactory from 'debug';
const debug = debugFactory('loopback:example:shopping');
import {retry} from '../utils/retry';

/**
* Controller for shopping cart
Expand All @@ -35,6 +38,7 @@ export class ShoppingCartController {
@param.path.string('userId') userId: string,
@requestBody({description: 'shopping cart'}) cart: ShoppingCart,
) {
debug('Create shopping cart %s: %j', userId, cart);
if (userId !== cart.userId) {
throw new HttpErrors.BadRequest(
`User id does not match: ${userId} !== ${cart.userId}`,
Expand All @@ -49,7 +53,9 @@ export class ShoppingCartController {
*/
@get('/shoppingCarts/{userId}')
async get(@param.path.string('userId') userId: string) {
debug('Get shopping cart %s', userId);
const cart = await this.shoppingCartRepository.get(userId);
debug('Shopping cart %s: %j', userId, cart);
if (cart == null) {
throw new HttpErrors.NotFound(
`Shopping cart not found for user: ${userId}`,
Expand All @@ -65,6 +71,7 @@ export class ShoppingCartController {
*/
@del('/shoppingCarts/{userId}')
async remove(@param.path.string('userId') userId: string) {
debug('Remove shopping cart %s', userId);
await this.shoppingCartRepository.delete(userId);
}

Expand All @@ -78,6 +85,14 @@ export class ShoppingCartController {
@param.path.string('userId') userId: string,
@requestBody({description: 'shopping cart item'}) item: ShoppingCartItem,
) {
await this.shoppingCartRepository.addItem(userId, item);
debug('Add item %j to shopping cart %s', item, userId);
return retry<ShoppingCart>(
{
run: () => this.shoppingCartRepository.addItem(userId, item),
description: `update the shopping cart for '${userId}'`,
},
10,
100,
);
}
}
7 changes: 5 additions & 2 deletions src/repositories/shopping-cart.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class ShoppingCartRepository extends DefaultKeyValueRepository<
* @param userId User id
* @param check A function that checks the current value and produces a new
* value. It returns `null` to abort.
*
* @returns A promise of the updated ShoppingCart instance or `null` if the
* transaction fails. See https://github.com/NodeRedis/node_redis#optimistic-locks
*/
async checkAndSet(
userId: string,
Expand All @@ -66,7 +69,7 @@ export class ShoppingCartRepository extends DefaultKeyValueRepository<
if (!cart) return null;
await execute('MULTI', []);
await this.set(userId, cart);
await execute('EXEC', []);
return cart;
const result = await execute('EXEC', []);
return result == null ? null : cart;
}
}
Loading

0 comments on commit f2f1805

Please sign in to comment.