Skip to content

all attribute endpoints now working but need optimization #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions src/controllers/attributes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}
}

Expand Down