From 84162f2ef86d92fbbc954fd056357b04e13e074f Mon Sep 17 00:00:00 2001 From: "https://github.com': codeplus254" Date: Thu, 26 Sep 2019 19:52:01 +0300 Subject: [PATCH] all attribute endpoints now working but need optimization --- src/controllers/attributes.controller.js | 73 ++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/src/controllers/attributes.controller.js b/src/controllers/attributes.controller.js index 3652d4f..3b9a71d 100644 --- a/src/controllers/attributes.controller.js +++ b/src/controllers/attributes.controller.js @@ -10,6 +10,16 @@ * NB: Check the BACKEND CHALLENGE TEMPLATE DOCUMENTATION in the readme of this repository to see our recommended * endpoints, request body/param, and response object for each of these method */ +import { + Product, + ProductAttribute, + Department, + AttributeValue, + Attribute, + Category, + Sequelize, +} from '../database/models'; + class AttributeController { /** * This method get all attributes @@ -19,7 +29,12 @@ class AttributeController { */ static async getAllAttributes(req, res, next) { // write code to get all attributes from the database here - return res.status(200).json({ message: 'this works' }); + try { + const attributes = await Attribute.findAll(); + return res.status(200).json(attributes); + } catch (error) { + return next(error); + } } /** @@ -30,7 +45,21 @@ class AttributeController { */ static async getSingleAttribute(req, res, next) { // Write code to get a single attribute using the attribute id provided in the request param - return res.status(200).json({ message: 'this works' }); + const { attribute_id } = req.params; // eslint-disable-line + try { + const attribute = await Attribute.findByPk(attribute_id); + if (attribute) { + return res.status(200).json(attribute); + } + return res.status(404).json({ + error: { + status: 404, + message: `Attribute with id ${attribute_id} does not exist`, // eslint-disable-line + } + }); + } catch (error) { + return next(error); + } } /** @@ -42,7 +71,17 @@ class AttributeController { static async getAttributeValues(req, res, next) { // Write code to get all attribute values for an attribute using the attribute id provided in the request param // This function takes the param: attribute_id - return res.status(200).json({ message: 'this works' }); + try { + const { attribute_id } = req.params; // eslint-disable-line + const attributeValues = await AttributeValue.findAll({ + where: { + attribute_id, + }, + }); + return res.status(200).json(attributeValues); + } catch (error) { + return next(error); + } } /** @@ -53,7 +92,33 @@ class AttributeController { */ static async getProductAttributes(req, res, next) { // Write code to get all attribute values for a product using the product id provided in the request param - return res.status(200).json({ message: 'this works' }); + try { + const { product_id } = req.params; // eslint-disable-line + const productAttributeValuesID = await ProductAttribute.findAll({ + where: { + product_id, + }, + attributes: ['attribute_value_id'], + }); + const productAttributes = []; + const valuesIDArray = []; + productAttributeValuesID.map(id => valuesIDArray.push(id.attribute_value_id)); + // eslint-disable-next-line no-plusplus + for (let i = 0; i < valuesIDArray.length; i++) { + // eslint-disable-next-line no-await-in-loop + const productAttribute = await AttributeValue.findOne({ + where: { + attribute_value_id: valuesIDArray[i], + }, + }); + if (productAttribute) { + productAttributes.push(productAttribute) + } + } + return res.status(200).json(productAttributes); + } catch (error) { + return next(error); + } } }