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
20 changes: 20 additions & 0 deletions packages/api-gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ app.use(express.json());

const USER_SERVICE = "http://user-service:3001";
const PRODUCT_SERVICE = "http://product-service:3002";
const ORDER_SERVICE = "http://order-service:3005";

app.get("/users", async (_req, res) => {
const response = await fetch(`${USER_SERVICE}/users`);
Expand Down Expand Up @@ -44,6 +45,25 @@ app.post("/products", async (req, res) => {
res.status(response.status).json(await response.json());
});

app.get("/orders", async (_req, res) => {
const response = await fetch(`${ORDER_SERVICE}/orders`);
res.json(await response.json());
});

app.get("/orders/:id", async (req, res) => {
const response = await fetch(`${ORDER_SERVICE}/orders/${req.params.id}`);
res.status(response.status).json(await response.json());
});

app.post("/orders", async (req, res) => {
const response = await fetch(`${ORDER_SERVICE}/orders`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(req.body),
});
res.status(response.status).json(await response.json());
});

app.listen(3000, () => {
console.log("api-gateway listening on :3000");
});
18 changes: 18 additions & 0 deletions packages/order-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@playground/order-service",
"version": "1.0.0",
"private": true,
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc --build",
"start": "node dist/index.js"
},
"dependencies": {
"@playground/database": "*",
"express": "^4.21.0"
},
"devDependencies": {
"@types/express": "^5.0.0"
}
}
42 changes: 42 additions & 0 deletions packages/order-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import express from "express";
import { insert, findById, findAll } from "@playground/database";

const app = express();
app.use(express.json());

const PRODUCT_SERVICE = "http://product-service:3002";

app.get("/orders", (_req, res) => {
res.json(findAll("orders"));
});

app.get("/orders/:id", (req, res) => {
const order = findById("orders", req.params.id);
if (!order) {
res.status(404).json({ error: "Order not found" });
return;
}
res.json(order);
});

app.post("/orders", async (req, res) => {
const { productId, userId } = req.body;

const productResponse = await fetch(`${PRODUCT_SERVICE}/products/${productId}`);
if (!productResponse.ok) {
res.status(400).json({ error: "Invalid product" });
return;
}

const order = insert("orders", {
id: crypto.randomUUID(),
productId,
userId,
status: "created",
});
res.status(201).json(order);
});

app.listen(3005, () => {
console.log("order-service listening on :3005");
});
11 changes: 11 additions & 0 deletions packages/order-service/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"],
"references": [
{ "path": "../database" }
]
}
13 changes: 13 additions & 0 deletions packages/product-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { insert, findById, findAll } from "@playground/database";
const app = express();
app.use(express.json());

const USER_SERVICE = "http://user-service:3001";

app.get("/products", (_req, res) => {
res.json(findAll("products"));
});
Expand All @@ -22,6 +24,17 @@ app.post("/products", (req, res) => {
res.status(201).json(product);
});

app.get("/products/:id/creator", async (req, res) => {
const product = findById("products", req.params.id);
if (!product) {
res.status(404).json({ error: "Product not found" });
return;
}
const response = await fetch(`${USER_SERVICE}/users/${product.createdBy}`);
const creator = await response.json();
res.json({ product, creator });
});

app.listen(3002, () => {
console.log("product-service listening on :3002");
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{ "path": "packages/user-service" },
{ "path": "packages/product-service" },
{ "path": "packages/api-gateway" },
{ "path": "packages/frontend" }
{ "path": "packages/frontend" },
{ "path": "packages/order-service" }
]
}