Skip to content

Commit

Permalink
Add allow amounts to be posted with ingredients for recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
holdonowgo committed Jun 21, 2017
1 parent e31622c commit 3a905f9
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 14 deletions.
24 changes: 19 additions & 5 deletions api/controllers/clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,25 @@ function getUsersSearchResponse(req, res) {
let first_name = req.swagger.params.first_name.value;
let last_name = req.swagger.params.last_name.value;
let promises = [];
// promises.push(knex("clients").select("id"));
promises.push(knex("clients").select("id", "first_name", "last_name", "email", "is_super_user").where(`clients.first_name`, `like`, `%${first_name}%`).orWhere(`clients.last_name`, `like`, `%${last_name}%`).orWhere(`clients.email`, `like`, `%${email}%`));
promises.push(knex("clients_recipes").join('recipes', 'recipes.id', '=', 'clients_recipes.recipe_id').select("clients_recipes.client_id", "recipes.*")
.orderBy('name'));
promises.push(knex("recipe_steps").join('recipes', 'recipes.id', '=', 'recipe_steps.recipe_id').select("recipe_steps.*"));

promises.push(
knex("clients")
.select("id", "first_name", "last_name", "email", "image_url", "is_super_user")
.where(`clients.first_name`, `like`, `%${first_name}%`)
.orWhere(`clients.last_name`, `like`, `%${last_name}%`)
.orWhere(`clients.email`, `like`, `%${email}%`));

promises.push(
knex("clients_recipes")
.join('recipes', 'recipes.id', '=', 'clients_recipes.recipe_id')
.select("clients_recipes.client_id", "recipes.*")
.orderBy('name'));

promises.push(
knex("recipe_steps")
.join('recipes', 'recipes.id', '=', 'recipe_steps.recipe_id')
.select("recipe_steps.*"));

Promise.all(promises).then((results) => {
let clients = results[0];
let recipes = results[1];
Expand Down
8 changes: 6 additions & 2 deletions api/controllers/recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ function postRecipe(req, res) {
let recipe;
let name = req.swagger.params.recipe.value.name;
let description = req.swagger.params.recipe.value.description;
let cook_time = req.swagger.params.recipe.value.cook_time;
let prep_time = req.swagger.params.recipe.value.prep_time;
let image_url = req.swagger.params.recipe.value.image_url;

// to insert into the recipe_steps table
Expand All @@ -281,13 +283,15 @@ function postRecipe(req, res) {
} else {
return knex("recipes").insert({"name": name,
"description": description,
"cook_time": cook_time,
"prep_time": prep_time,
"image_url": image_url})
.returning("*");
}
}).then((recipes) => { // insert ingredients
recipe = recipes[0];
let data = ingredients.map((value) => {
return {recipe_id: recipe.id, ingredient_id: value};
return {recipe_id: recipe.id, ingredient_id: value.id, amount: value.amount};
});
return knex('ingredients_recipes').insert(data).returning("*");
}).then(() => { // insert instructions
Expand Down Expand Up @@ -329,7 +333,7 @@ function updateRecipe(req, res) {
//to insert into ingredients_recipes table

let data = recipe.ingredients.map((value) => {
return {recipe_id: recipe.id, ingredient_id: value};
return {recipe_id: recipe.id, ingredient_id: value.id, amount: value.amount};
});

return knex('ingredients_recipes').insert(data).returning("*");
Expand Down
8 changes: 7 additions & 1 deletion api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,13 @@ definitions:
ingredients:
type: array
items:
type: integer
# type: integer
type: object
properties:
id:
type: integer
amount:
type: string
tags:
type: array
items:
Expand Down
1 change: 1 addition & 0 deletions migrations/20170319175614_clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports.up = function(knex, Promise) {
table.string('first_name').notNullable().defaultTo('');
table.string('last_name').notNullable().defaultTo('');
table.string('email').notNullable().unique();
table.string('image_url');
table.boolean('is_super_user').notNullable().defaultTo(false);
table.specificType('hashed_password', 'char(60)').notNullable();
table.timestamp('created_at').notNullable().defaultTo(knex.raw('now()'));
Expand Down
60 changes: 59 additions & 1 deletion public/api-docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,58 @@ paths:
# schema:
# $ref: "#/definitions/ErrorResponse"

/clients/{user_id}/safeIngredients:
x-swagger-router-controller: ingredients
get:
description: get ingredients that are safe for the client
operationId: getSafeIngredients
parameters:
- name: token
in: header
description: Users authentication token
required: true
type: string

- name: user_id
type: integer
in: path
required: true
responses:
"200":
description: Success
schema:
$ref: "#/definitions/GetIngredientsListResponse"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"

/clients/{user_id}/saferecipes:
x-swagger-router-controller: recipes
get:
description: get recipes that are safe for the client
operationId: getSafeRecipes
parameters:
- name: token
in: header
description: Users authentication token
required: true
type: string

- name: user_id
type: integer
in: path
required: true
responses:
"200":
description: Success
schema:
$ref: "#/definitions/GetRecipesListResponse"
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"

/clients/{user_id}/recipes:
x-swagger-router-controller: recipes
get:
Expand Down Expand Up @@ -1515,7 +1567,13 @@ definitions:
ingredients:
type: array
items:
type: integer
# type: integer
type: object
properties:
id:
type: integer
amount:
type: string
tags:
type: array
items:
Expand Down
23 changes: 19 additions & 4 deletions seeds/01_clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ exports.seed = function(knex, Promise) {
first_name: 'Marvin',
last_name: 'Gaye',
email: '[email protected]',
image_url: `http://www.eurweb.com/wp-content/uploads/2016/11/marvin-gaye-2.jpg`,
is_super_user: false,
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS', // youreawizard
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS',
created_at: new Date('2016-06-29 14:26:16 UTC'),
updated_at: new Date('2016-06-29 14:26:16 UTC')
},
Expand All @@ -19,8 +20,9 @@ exports.seed = function(knex, Promise) {
first_name: 'Al',
last_name: 'Green',
email: '[email protected]',
image_url: `http://www.hirecords.com/assets/img/al-green.jpg`,
is_super_user: false,
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS', // youreawizard
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS',
created_at: new Date('2016-06-29 14:26:16 UTC'),
updated_at: new Date('2016-06-29 14:26:16 UTC')
},
Expand All @@ -29,8 +31,9 @@ exports.seed = function(knex, Promise) {
first_name: 'Randall',
last_name: 'Spencer',
email: '[email protected]',
image_url: `https://media.licdn.com/mpr/mpr/shrinknp_400_400/AAEAAQAAAAAAAAbyAAAAJDdmOWM1MTgwLTc2NjYtNDQ4Yy05ZTM1LWQyMzhmZjkwN2FlYg.jpg`,
is_super_user: true,
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS', // youreawizard
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS',
created_at: new Date('2016-06-29 14:26:16 UTC'),
updated_at: new Date('2016-06-29 14:26:16 UTC')
},
Expand All @@ -39,8 +42,20 @@ exports.seed = function(knex, Promise) {
first_name: 'Aom',
last_name: 'Sithanant',
email: '[email protected]',
image_url: `https://media.licdn.com/mpr/mpr/shrink_200_200/AAEAAQAAAAAAAAtCAAAAJGE2YjFjMWRhLTBiY2MtNDgwMi04MTNiLTJjZTI0NWVkODg4Nw.jpg`,
is_super_user: true,
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS', // youreawizard
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS',
created_at: new Date('2016-06-29 14:26:16 UTC'),
updated_at: new Date('2016-06-29 14:26:16 UTC')
},
{
id: 5,
first_name: 'Michael',
last_name: 'Martinez',
email: '[email protected]',
image_url: `http://i.imgur.com/lL5Y56B.jpg`,
is_super_user: true,
hashed_password: '$2a$12$C9AYYmcLVGYlGoO4vSZTPud9ArJwbGRsJ6TUsNULzR48z8fOnTXbS',
created_at: new Date('2016-06-29 14:26:16 UTC'),
updated_at: new Date('2016-06-29 14:26:16 UTC')
}])
Expand Down
5 changes: 5 additions & 0 deletions test/api/controllers/clients_routes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ suite('clients tests', () => {
"first_name": 'Marvin',
"last_name": 'Gaye',
"email": '[email protected]',
"image_url": '',
"is_super_user": false,
"recipes": [
{
Expand Down Expand Up @@ -169,6 +170,7 @@ suite('clients tests', () => {
"first_name": 'Aom',
"last_name": 'Sithanant',
"email": '[email protected]',
"image_url": '',
"is_super_user": true,
"recipes": [
{
Expand Down Expand Up @@ -310,6 +312,7 @@ suite('clients tests', () => {
"first_name": 'Aom',
"last_name": 'Sithanant',
"email": '[email protected]',
"image_url": '',
"is_super_user": true,
"recipes": [
{
Expand Down Expand Up @@ -368,6 +371,7 @@ suite('clients tests', () => {
first_name: 'John',
last_name: 'Siracusa',
email: '[email protected]',
"image_url": '',
is_super_user: false
}).expect('Content-Type', /json/).end((httpErr, _res) => {
if (httpErr) {
Expand All @@ -386,6 +390,7 @@ suite('clients tests', () => {
first_name: 'John',
last_name: 'Siracusa',
email: '[email protected]',
"image_url": '',
is_super_user: false
});

Expand Down
3 changes: 2 additions & 1 deletion test/api/controllers/recipes_routes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,8 @@ suite("recipes test", () => {
}
],
ingredients: [
1, 3
{ id: 1, amount: '1 cup' },
{ id: 3, amount: '2 tsbp' }
],
tags: ['no-cook', 'asian', 'vegetarian', 'vegan']
}).expect("Content-Type", /json/)
Expand Down

0 comments on commit 3a905f9

Please sign in to comment.