Commit c9c6a0f4 authored by Daniela Amaral Da Rosa's avatar Daniela Amaral Da Rosa
Browse files

Merge branch 'dev' into 'feat/continuous-delivery'

# Conflicts:
#   src/__tests__/endpoints/recipeEndpoints.ts
parents dff8c977 f8b4e466
Pipeline #1188 passed with stage
in 1 minute and 6 seconds
Showing with 37 additions and 12 deletions
+37 -12
......@@ -133,7 +133,7 @@ describe('Getting recipes - Endpoint', () => {
it('should not return a recipe with an id that doesnt exists', async () => {
const response = await request.get('/recipes/9999');
expect(response.status).toBe(404);
//expect(response.body).toBe(null);
});
});
......
......@@ -106,6 +106,21 @@ export async function findRecipeById(
return populateRecipesWithTagsAndSteps(recipe);
}
/**
* Receives a list of recipe_id
* @param recipes_id
* @returns Promise<Recipe[]>
*/
export async function findRecipesByListOfId(
recipesId: number[],
): Promise<Recipe[]> {
const recipe = await db.select('*')
.from<Recipe>('recipes')
.whereIn('recipe_id', recipesId)
.andWhere('active', 1);
return populateRecipesWithTagsAndSteps(recipe);
}
/**
* Receives an array of recipes and adds its tags
* @param recipes
......
import { celebrate, Joi, Segments } from 'celebrate';
import {celebrate, Joi, Segments} from 'celebrate';
import express from 'express';
import healthCheck from './controllers/healthController';
import {
indexRecipes, postRecipe, searchRecipeById, searchRecipesByUser
} from './controllers/recipeController';
import {indexRecipes, postRecipe, searchRecipeById, searchRecipesByUser} from './controllers/recipeController';
import indexTags from './controllers/tagController';
const routes = express.Router();
......@@ -28,6 +26,7 @@ routes.get('/recipes',
[Segments.QUERY]: {
tag: Joi.alternatives().try(Joi.number(), Joi.array().items(Joi.number())),
title: Joi.string(),
recipeId: Joi.alternatives().try(Joi.number(), Joi.array().items(Joi.number())),
},
}), indexRecipes);
routes.get('/tags', indexTags);
......@@ -44,6 +43,4 @@ routes.get('/recipes/:recipe_id', celebrate({
},
}), searchRecipeById);
routes.get('/tags', indexTags);
export default routes;
import { errors } from 'celebrate';
import {errors} from 'celebrate';
import cors from 'cors';
import express from 'express';
import path from 'path';
......@@ -12,9 +12,9 @@ app.use(express.json());
app.use(routes);
app.use(errors());
if (process.env.NODE_ENV === 'development') {
//if (process.env.NODE_ENV === 'development') {
const swaggerDocument = YAML.load(path.resolve(__dirname, '..', 'swagger.yaml'));
app.use('/api-docs', swagger.serve, swagger.setup(swaggerDocument, { explorer: true }));
}
//}
export default app;
......@@ -2,6 +2,7 @@ import Recipe from '../models/recipe';
import {
findAllRecipes,
findRecipeById,
findRecipesByListOfId,
findRecipesByQueryParams,
findRecipesByUser,
saveRecipe
......@@ -23,10 +24,14 @@ function mountRecipe(recipes: Recipe[]) {
}
export async function getAllRecipes(query: any): Promise<Recipe[]> {
const { tag, title } = query;
const { tag, title, recipeId } = query;
let tags : number[] = [];
let recipes: Recipe[];
if (tag || title) {
if (recipeId) {
let ids : number[] = [];
ids = ids.concat(recipeId);
recipes = await findRecipesByListOfId(ids);
} else if (tag || title) {
tags = typeof tag !== 'undefined' ? tags.concat(tag) : [];
recipes = await findRecipesByQueryParams(title, tags);
} else {
......
......@@ -49,6 +49,14 @@ paths:
items:
type: "number"
collectionFormat: "multi"
- name: "recipeId"
in: "query"
description: "Id da receita"
required: false
type: "array"
items:
type: "number"
collectionFormat: "multi"
- name: "title"
in: "query"
description: "Título da receita"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment