Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions exercises/archi-separate-concerns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Architecture Understanding & Separation of Concerns Exercise

This archi-separate-component-front violates proper architecture principles by:
1. Mixing data fetching, business logic, and UI rendering all in one place
2. Performing multiple responsibilities within a single component
3. Lacking proper separation between API calls, data processing, and UI
4. Containing both data fetching and state updates for unrelated features

Your task: Refactor the archi-separate-component-front by:
1. Separating API calls into a dedicated service
2. Breaking down the UI into smaller, focused components
3. Properly separating business logic from presentation
4. Creating a proper architecture with clear separation of concerns

Be careful there could be a few mistakes in the code
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const express = require("express");
const router = express.Router();

const dummyOrders = [
{
id: "1",
customerName: "Alice",
customerEmail: "[email protected]",
date: new Date(),
status: "processing",
total: 120,
},
{
id: "2",
customerName: "Bob",
customerEmail: "[email protected]",
date: new Date(),
status: "completed",
total: 80,
},
];

// Get all orders
router.post("/list", async (req, res) => {
try {
return res.status(200).send({ ok: true, data: dummyOrders });
} catch (error) {
res.status(500).send({ ok: false, message: "Internal error" });
}
});

// Mark order as shipped
router.post("/mark-shipped", async (req, res) => {
try {
const { status } = req.body;
const orderId = req.params.id;

const order = dummyOrders.find((o) => o.id === orderId);
if (!order) {
return res.status(404).send({ ok: false, message: "Order not found" });
}

if (status) {
order.status = status;
}

return res.status(200).send({ ok: true, data: order });
} catch (error) {
res
.status(500)
.send({ ok: false, message: "Failed to update order status" });
}
});

// Send reminder email
router.post("/send-reminder", async (req, res) => {
try {
const { orderId } = req.body;
const order = dummyOrders.find((o) => o.id === orderId);
if (!order)
return res.status(404).send({ ok: false, message: "Order not found" });
return res.status(200).send({
ok: true,
data: { message: `Reminder sent for order #${orderId}` },
});
} catch (error) {
res.status(500).send({ ok: false, message: "Failed to send reminder" });
}
});

module.exports = router;
15 changes: 15 additions & 0 deletions exercises/archi-separate-concerns/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// archi-separate-concern/back/index.js
const express = require("express");
const cors = require("cors");
const app = express();
const port = 3001;
const ordersRoutes = require("./controllers/ordersController");

app.use(cors());
app.use(express.json());

app.use("/api/orders", ordersRoutes);

app.listen(port, () =>
console.log(`API server running at http://localhost:${port}`)
);
Loading