Skip to content

Commit b0f1383

Browse files
authored
Merge pull request #23 from patika-hepsiburada-react-bootcamp/dev
[#9, #13] feat: set values on cache for faster load time
2 parents 451e5b0 + 3eacd23 commit b0f1383

File tree

11 files changed

+92
-5
lines changed

11 files changed

+92
-5
lines changed

client/src/components/Layout/Section/index.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export default function Section() {
2121
<ProductItem />
2222
<ProductItem />
2323
<ProductItem />
24-
<ProductItem />
2524
</ProductRow>
2625
</ProductTable>
2726
</section>

client/src/components/ProductTable/Pagination/styles.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
margin-top: 39px;
77

88
.Button {
9+
cursor: pointer;
910
width: 33px;
1011
height: 33px;
1112
background-color: #ffff;

client/src/components/ProductTable/ProductItem/styles.module.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
min-height: 100%;
99
max-height: 100%;
1010
height: 100%;
11+
margin-right: 14px;
12+
13+
&:last-child {
14+
margin-right: 0;
15+
}
1116

1217
&.Stroke {
1318
border: 1px solid vars.$stroke-border-color;

client/src/components/ProductTable/ProductRow/styles.module.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
.Row {
44
display: flex;
5-
width: 100%;
6-
justify-content: space-between;
5+
min-width: 100%;
76
height: 479px;
87
max-height: 479px;
98
}

server/app.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ app.use("/brands", brands);
2121

2222
//Index route
2323
app.get("/", async (req, res) => {
24-
res.end(`Groove street`);
24+
res.json({
25+
endPoints: [
26+
{ products: [{ all: "/products" }, { id: "products:id" }] },
27+
{ brands: [{ all: "/brands" }, { productIds: "brands/:name" }] },
28+
{ colors: [{ all: "/colors" }, { productIds: "colors/:name" }] },
29+
],
30+
});
2531
});
2632

2733
app.listen(PORT, () => {

server/controllers/brands.controller.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Brand from "../models/brands.model.js";
22
import client from "../libs/redis/index.js";
3+
import hashProductId from "../libs/redis/helpers.js";
34

45
const controller = {};
56

@@ -11,7 +12,9 @@ controller.getAll = async (req, res) => {
1112

1213
// if data exist, send it as a response
1314
if (cachedData) {
14-
if (cachedData <= 0) res.status(404).json("Brands data does not exist.");
15+
if (cachedData <= 0) {
16+
res.status(404).json("Brands data does not exist.");
17+
}
1518
res.status(200).json(cachedData);
1619
} else {
1720
// pull data from db
@@ -22,11 +25,27 @@ controller.getAll = async (req, res) => {
2225

2326
// send data pulled from db
2427
if (brands <= 0) res.status(404).json("Brands data does not exist.");
28+
29+
hashProductId(brands);
2530
res.status(200).json(brands);
2631
}
2732
} catch (err) {
2833
console.error("Error in getting brands data - " + err.message);
2934
res.status(500).json({ error: "Got error in getAll controller of brands" });
3035
}
3136
};
37+
38+
controller.getProductIds = async (req, res) => {
39+
// TODO: call getAll before this function
40+
try {
41+
client.smembers(req.params.name, (err, result) => {
42+
if (err) res.status(500).end(err);
43+
else res.status(200).json(result);
44+
});
45+
} catch (err) {
46+
console.error("Error in getting brands data - " + err.message);
47+
res.status(500).json({ error: "Got error in getAll controller of brands" });
48+
}
49+
};
50+
3251
export default controller;

server/controllers/colors.controller.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Color from "../models/colors.model.js";
22
import client from "../libs/redis/index.js";
3+
import hashProductId from "../libs/redis/helpers.js";
34

45
const controller = {};
56

@@ -22,6 +23,8 @@ controller.getAll = async (req, res) => {
2223

2324
// send data pulled from db
2425
if (colors <= 0) res.status(404).json("Colors data does not exist.");
26+
27+
hashProductId(colors);
2528
res.status(200).json(colors);
2629
}
2730
} catch (err) {
@@ -30,4 +33,17 @@ controller.getAll = async (req, res) => {
3033
}
3134
};
3235

36+
controller.getProductIds = async (req, res) => {
37+
// TODO: call getAll before this function
38+
try {
39+
client.smembers(req.params.name, (err, result) => {
40+
if (err) res.status(500).end(err);
41+
else res.status(200).json(result);
42+
});
43+
} catch (err) {
44+
console.error("Error in getting brands data - " + err.message);
45+
res.status(500).json({ error: "Got error in getAll controller of brands" });
46+
}
47+
};
48+
3349
export default controller;

server/libs/functions.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const slugify = (str) =>
2+
str
3+
.toLowerCase()
4+
.replace(" ", "-")
5+
.replace(/Ü/gim, "u")
6+
.replace(/Ş/gim, "s")
7+
.replace(/I/gim, "i")
8+
.replace(/İ/gim, "i")
9+
.replace(/Ö/gim, "o")
10+
.replace(/Ç/gim, "c")
11+
.replace(/ğ/gim, "g")
12+
.replace(/ü/gim, "u")
13+
.replace(/ş/gim, "s")
14+
.replace(/ı/gim, "i")
15+
.replace(/ö/gim, "o")
16+
.replace(/ç/gim, "c");
17+
18+
export default slugify;

server/libs/redis/helpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import client from "./index.js";
2+
import slugify from "../functions.js";
3+
4+
const hashProductId = async (value) => {
5+
const data = await value;
6+
7+
data.map(async (item) => {
8+
// format as slug
9+
let name = slugify(item.name);
10+
11+
// add all documents as set
12+
await client.sadd(name, item.products);
13+
});
14+
};
15+
16+
export default hashProductId;

server/routes/brands.route.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ router.get("/all", (req, res) => {
77
brandController.getAll(req, res);
88
});
99

10+
router.get("/:name", (req, res) => {
11+
brandController.getProductIds(req, res);
12+
});
13+
1014
export default router;

server/routes/colors.route.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ router.get("/all", (req, res) => {
77
colorController.getAll(req, res);
88
});
99

10+
router.get("/:name", (req, res) => {
11+
colorController.getProductIds(req, res);
12+
});
13+
1014
export default router;

0 commit comments

Comments
 (0)