Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1cdbd74
ran npm instsall
martinsalinas0 Apr 12, 2025
920802e
updatesettings.json
martinsalinas0 Apr 15, 2025
96d2601
Merge branch 'master' of https://github.com/martinsalinas0/sunglasses-io
martinsalinas0 Apr 15, 2025
3c81917
update packae and settings.jsonm
martinsalinas0 Apr 15, 2025
595d603
update yaml file
martinsalinas0 Apr 15, 2025
8bed242
add sunglasses/{id} in sawgger.yaml
martinsalinas0 Apr 15, 2025
398fcdc
added all paths needed
martinsalinas0 Apr 16, 2025
d1d91ae
updated all libraries
martinsalinas0 Apr 17, 2025
d944b29
add Brands test
martinsalinas0 Apr 20, 2025
dc2f69e
updated body parser with express built in
martinsalinas0 May 5, 2025
f27f54b
added api/brands endpoint - test- passed
martinsalinas0 May 5, 2025
8c70b36
updated and renamed YAML file
martinsalinas0 May 5, 2025
c2c4332
added notes.txt
martinsalinas0 May 5, 2025
5554f7b
updated package.json
martinsalinas0 May 5, 2025
d631fe9
added /api/brands/:/brandId/products
martinsalinas0 May 5, 2025
5604417
Merge branch 'master' of https://github.com/martinsalinas0/sunglasses-io
martinsalinas0 May 5, 2025
62a1efd
update package.json
martinsalinas0 May 5, 2025
642cc0a
updated package.json with nodemon and dotenv
martinsalinas0 May 5, 2025
fc572b2
added: brands and product controllers and routes
martinsalinas0 May 7, 2025
f16fe2d
2 test passing
martinsalinas0 May 7, 2025
ade1b94
changed the brand IDs
martinsalinas0 May 7, 2025
dfcaf60
add product controller
martinsalinas0 May 8, 2025
fe99e31
brands.contorller
martinsalinas0 May 8, 2025
aed9cff
updated products and users.json
martinsalinas0 May 8, 2025
736b681
add all controllers and routes
martinsalinas0 May 8, 2025
4b5cb99
formatted server.js
martinsalinas0 May 8, 2025
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
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",
"program": "mocha",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
{
"type": "node",
"request": "launch",
Expand Down
22 changes: 21 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
{
}
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#fa1b49",
"activityBar.background": "#fa1b49",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#155e02",
"activityBarBadge.foreground": "#e7e7e7",
"commandCenter.border": "#e7e7e799",
"sash.hoverBorder": "#fa1b49",
"statusBar.background": "#dd0531",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#fa1b49",
"statusBarItem.remoteBackground": "#dd0531",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#dd0531",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#dd053199",
"titleBar.inactiveForeground": "#e7e7e799"
},
"peacock.color": "#dd0531"
}
55 changes: 34 additions & 21 deletions app/server.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const swaggerDocument = YAML.load('./swagger.yaml'); // Replace './swagger.yaml' with the path to your Swagger file
const express = require("express");
const jwt = require("jsonwebtoken");
const swaggerUi = require("swagger-ui-express");
const YAML = require("yamljs");
const swaggerDocument = YAML.load("./sunglasses-api.yaml");
const router = express.Router();
require("dotenv").config();

const app = express();

app.use(bodyParser.json());
const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET;

// Importing the data from JSON files
const users = require('../initial-data/users.json');
const brands = require('../initial-data/brands.json');
const products = require('../initial-data/products.json');
app.use(express.json());

// Error handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));

// Swagger
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
const brandRoute = require("../routes/brands.route");
const productsRoute = require("../routes/products.Route");
const cartRoutes = require("../routes/cart.route");
const { login } = require("../controllers/auth.controller");

app.post("/api/login", login);
app.use("/api", brandRoute);
app.use("/api", productsRoute);
app.use("/api/me", cartRoutes);
app.use(router);

// Starting the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);

const server = app.listen(PORT, () => {
console.log(`Server connected. \nServer is now running on port: ${PORT}`);
});

server.on("error", (err) => {
console.error(`Error starting server: ${err.message}`);
process.exit(1);
});

app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something broke!");
});

module.exports = app;
16 changes: 16 additions & 0 deletions authenticationToken/authentication,.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const jwt = require("jsonwebtoken");
const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET;

const authenticateToken = (req, res, next) => {
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1];
if (!token) return res.status(401).json({ message: "Missing token" });

jwt.verify(token, ACCESS_TOKEN_SECRET, (err, user) => {
if (err) return res.status(403).json({ message: "Invalid token" });
req.user = user;
next();
});
};

module.exports = authenticateToken;
32 changes: 32 additions & 0 deletions controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const jwt = require("jsonwebtoken");
const users = require("../models/users.json");

const ACCESS_TOKEN_SECRET = process.env.ACCESS_TOKEN_SECRET;

const login = (req, res) => {
try {
const { email, password } = req.body;

const user = users.find(u => u.email === email);

if (!user || user.login.password !== password) {
return res.status(401).json({ message: "Invalid credentials" });
}

const token = jwt.sign(
{ id: user.login.md5, email: user.email },
ACCESS_TOKEN_SECRET,
{ expiresIn: "1h" }
);

res.status(200).json({ token });
} catch (error) {
res.status(500).json({ message: error.message });
}
};




module.exports = { login };

25 changes: 25 additions & 0 deletions controllers/brands.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const products = require("../models/products.json");
const brands = require("../models/brands.json");

const getBrands = (req, res) => {
try {
res.status(200).json({ brands });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

const getBrandProducts = (req, res) => {
try {
const { brandId } = req.params;
const brandedProducts = products.filter((product) =>
String(product.brandId === String(brandId))
);

res.status(200).json({ products: brandedProducts });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

module.exports = { getBrands, getBrandProducts };
103 changes: 103 additions & 0 deletions controllers/cart.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const users = require("../models/users.json");
const products = require("../models/products.json");

const getCart = (req, res) => {
try {
const user = users.find((user) => user.email === req.user.email);
if (!user) return res.status(404).json({ message: "user does not exist" });
res.status(200).json(user.cart || []);
} catch (error) {
res.status(500).json({ message: error.message });
}
};

const addToCart = (req, res) => {
try {
const { productId } = req.body;
if (!productId)
return res.status(400).json({ message: "product ID is required" });

const user = users.find((user) => user.email === req.user.email);
if (!user) return res.status(404).json({ message: "user does not exist" });

const product = products.find(
(product) => String(product.id) === String(productId)
);
if (!product) return res.status(400).json({ message: "product not found" });

const productToAdd = user.cart.find(
(product) => String(product.productId) === String(productId)
);

if (productToAdd) {
productToAdd.quantity += 1;
} else {
user.cart.push({
productId: product.id,
item: product.name,
price: product.price,
quantity: 1,
});
}

res.status(200).json({ message: "added to cart", cart: user.cart });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

const removeFromCart = (req, res) => {
try {
const { productId } = req.params;

const user = users.find((user) => user.email === req.user.email);
if (!user) return res.status(404).json({ message: "user does not exist" });

const productInCart = user.cart.some(
(product) => String(product.productId) === String(productId)
);
if (!productInCart) {
return res.status(404).json({ message: "product not found" });
}

user.cart = user.cart.filter(
(product) => String(product.productId) !== String(productId)
);
res.status(200).json({ message: "removed from cart", cart: user.cart });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

const updateProductQuant = (req, res) => {
try {
const { productId } = req.params;
const user = users.find((user) => user.email === req.user.email);
if (!user) return res.status(404).json({ message: "user does not exist" });

const product = products.find(
(product) => String(product.id) === String(productId)
);
if (!product) return res.status(404).json({ message: "product not found" });

const productInCart = user.cart.find(
(product) => String(product.productId) === String(productId)
);
if (productInCart) {
productInCart.quantity += 1;
} else {
user.cart.push({
productId: product.id,
item: product.name,
price: product.price,
quantity: 1,
});
}

res.status(200).json({ message: "cart updated", cart: user.cart });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

module.exports = { getCart, addToCart, removeFromCart, updateProductQuant };
23 changes: 23 additions & 0 deletions controllers/products.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const productsModel = require("../models/products.json");

const getProducts = (req, res) => {
try {
res.status(200).json({ products: productsModel });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

const getProductsByBrand = (req, res) => {
try {
const { brandId } = req.params;
const filteredProducts = productsModel.filter(
(product) => product.brandId === brandId
);
res.status(200).json({ products: filteredProducts });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

module.exports = { getProducts, getProductsByBrand };
22 changes: 0 additions & 22 deletions initial-data/brands.json

This file was deleted.

Loading