Skip to content

Commit 1fec527

Browse files
authored
Build: Adding products table to prisma schema && Feat: createProduct route (#16)
1 parent 91efa33 commit 1fec527

11 files changed

Lines changed: 147 additions & 4 deletions

File tree

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- "**.js"
77
- "**.json"
88
- "**.yml"
9-
branches: ["master", "dev_prod", "dev_featsUser"]
9+
branches: ["master", "dev_prod", "dev_featsUser", "dev_featsProducts"]
1010
pull_request:
1111
branches: ["master", "dev_prod"]
1212

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"test:actions": "node --test --experimental-test-coverage",
1313
"dc:up": "docker compose up -d && docker compose logs -f prisma_tests",
1414
"dc:down": "docker compose down --volumes --remove-orphans",
15-
"setup:prisma": "npx prisma migrate deploy && npx prisma db push"
15+
"setup:prisma": "npx prisma generate && npx prisma migrate deploy && npx prisma db push"
1616
},
1717
"repository": {
1818
"type": "git",

prisma/migrations/20250910214544_tables_app/migration.sql renamed to prisma/migrations/20250915002927_tables_app/migration.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ CREATE TABLE "public"."Users" (
99
CONSTRAINT "Users_pkey" PRIMARY KEY ("user_id")
1010
);
1111

12+
-- CreateTable
13+
CREATE TABLE "public"."Products" (
14+
"user_id" TEXT NOT NULL,
15+
"product_id" TEXT NOT NULL,
16+
"product_name" TEXT NOT NULL,
17+
"price" DOUBLE PRECISION NOT NULL,
18+
"category" TEXT NOT NULL,
19+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
20+
21+
CONSTRAINT "Products_pkey" PRIMARY KEY ("product_id")
22+
);
23+
1224
-- CreateTable
1325
CREATE TABLE "public"."UserTokens" (
1426
"id" TEXT NOT NULL,
@@ -21,5 +33,8 @@ CREATE TABLE "public"."UserTokens" (
2133
-- CreateIndex
2234
CREATE UNIQUE INDEX "Users_email_key" ON "public"."Users"("email");
2335

36+
-- AddForeignKey
37+
ALTER TABLE "public"."Products" ADD CONSTRAINT "Products_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."Users"("user_id") ON DELETE CASCADE ON UPDATE CASCADE;
38+
2439
-- AddForeignKey
2540
ALTER TABLE "public"."UserTokens" ADD CONSTRAINT "UserTokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."Users"("user_id") ON DELETE CASCADE ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ model Users {
1313
email String @unique
1414
password String
1515
createdAt DateTime @default(now())
16+
Products Products[]
1617
UserToken UserTokens[]
1718
}
1819

20+
model Products {
21+
user_id String
22+
product_id String @id @default(uuid())
23+
product_name String
24+
price Float @db.DoublePrecision
25+
category String
26+
createdAt DateTime @default(now())
27+
user Users @relation(fields: [user_id], references: [user_id], onDelete: Cascade)
28+
}
29+
1930
model UserTokens {
2031
id String @id @default(uuid())
2132
user_id String
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { ProductsService } from "../services/productsService.js";
2+
import { Middleware } from "../utils/auth/middleware.js";
3+
import { decode } from "jsonwebtoken"
4+
import { once } from "node:events";
5+
6+
export class ProductsController {
7+
_productsService;
8+
_userMiddleware;
9+
10+
constructor(productsService = new ProductsService, userMiddleware = new Middleware()) {
11+
this._productsService = productsService;
12+
this._userMiddleware = userMiddleware;
13+
}
14+
15+
async createProduct(request, response) {
16+
17+
const checkToken = await this._userMiddleware.ensureUserAuthenthicated(request, response);
18+
19+
if(checkToken === true) {
20+
21+
const { product_name, price, category } = JSON.parse(await once(request, "data"));
22+
23+
if(product_name === "" || price === "" || category === "") {
24+
response.writeHead(401);
25+
return response.end(JSON.stringify({ message: "All data must have a value !" }));
26+
27+
}else if(typeof(product_name) !== "string" || typeof(category) !== "string") {
28+
response.writeHead(401);
29+
return response.end(JSON.stringify({ message: "ProductName and Cataegory must be a string !" }));
30+
31+
}else if(typeof(price) !== "number") {
32+
response.writeHead(401);
33+
return response.end(JSON.stringify({ message: "Price must be a float/decimal number !" }));
34+
}
35+
36+
const authToken = request.headers.authorization;
37+
const [, token] = authToken.split(' ');
38+
const getUserId = decode(token);
39+
40+
const createProduct = await this._productsService.create({
41+
product_name,
42+
price,
43+
category,
44+
user_id: getUserId.user.user_id
45+
});
46+
47+
return response.end(JSON.stringify({
48+
product: {
49+
user_id: createProduct.user_id,
50+
product_id: createProduct.product_id,
51+
product_name: createProduct.product_name,
52+
price: createProduct.price,
53+
category: createProduct.category,
54+
createdAt: createProduct.createdAt
55+
}
56+
}));
57+
58+
}
59+
60+
response.writeHead(401);
61+
return response.end(JSON.stringify({ message: checkToken }));
62+
}
63+
64+
}

src/controllers/userController.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ export class UserController {
142142
user_id: findUserById.user_id,
143143
username: findUserById.username,
144144
email: findUserById.email,
145-
createdAt: findUserById.createdAt
145+
createdAt: findUserById.createdAt,
146+
products: findUserById.Products
146147
}
147148
}));
148149

src/models/productsModel.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { PrismaService } from "../database/prisma/prismaService.js";
2+
3+
export class ProductsModelRepository {
4+
_database;
5+
6+
constructor(prismaService = new PrismaService) {
7+
this._database = prismaService;
8+
}
9+
10+
async create(data) {
11+
const create = await this._database._prismaService.products.create({
12+
data: data
13+
});
14+
15+
return create;
16+
}
17+
18+
}

src/models/userModel.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ export class UserModelRepository {
2727

2828
}
2929

30+
async listUser(user_id) {
31+
const listUser = await this._database._prismaService.users.findUnique({
32+
where: {
33+
user_id: user_id
34+
},
35+
include: {
36+
Products: true
37+
}
38+
});
39+
40+
return listUser;
41+
}
42+
3043
async updateUser(user_id, username) {
3144
const update = await this._database._prismaService.users.update({
3245
where: {

src/routes/user.routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { UserController } from "../controllers/userController.js";
2+
import { ProductsController } from "../controllers/productsController.js";
23

34
const userController = new UserController();
5+
const productController = new ProductsController();
46

57
export class UserRoutes {
68

@@ -32,6 +34,10 @@ export class UserRoutes {
3234
case "/deleteUser/":
3335
await userController.deleteUser(request, response);
3436
break;
37+
38+
case "/createProduct":
39+
await productController.createProduct(request, response);
40+
break;
3541

3642
default:
3743
response.writeHead(404);

src/services/productsService.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ProductsModelRepository } from "../models/productsModel.js";
2+
3+
export class ProductsService {
4+
_productsModelRepository;
5+
6+
constructor(productsModelRepository = new ProductsModelRepository) {
7+
this._productsModelRepository = productsModelRepository;
8+
}
9+
10+
async create(data) {
11+
const create = await this._productsModelRepository.create(data);
12+
return create;
13+
}
14+
15+
}

0 commit comments

Comments
 (0)