Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BK]: Create Attributes, AttributeValues, and ProductVariantAttributes Entities and APIs #22

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"license": "ISC",
"description": "",
"dependencies": {
"dotenv": "^16.4.7",
"axios": "^1.7.9",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"express": "^4.21.1",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"express-validator": "^7.2.0",
"pg": "^8.13.1",
"reflect-metadata": "^0.2.2",
Expand Down
96 changes: 96 additions & 0 deletions src/controllers/AttributeController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {Request, Response} from 'express';
import { AttributeService } from '../services/attribute.service';
import AppDataSource from '../config/ormconfig';

const attributeService = new AttributeService();


//create new attribute
export const createAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const attribute = await attributeService.create(req.body);
res
.status(201)
.json({ success: true, message: "Attribute Created Successfully", data: attribute });
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}


//get all attributes
export const getAllAttributes = async (req: Request, res: Response): Promise<void> => {
try {
const attributes = await attributeService.getAll();
res
.status(200)
.json({ success: true, message: "Attributes Retrieved Successfully", data: attributes });
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//get attribute by id
export const getAttributeById = async (req: Request, res: Response): Promise<void> => {
try {
const attribute = await attributeService.getById(Number(req.params.id));
if (!attribute) {
res
.status(404)
.json({ success: false, message: "Attribute Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Retrieved Successfully", data: attribute });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//update attribute
export const updateAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const attribute = await attributeService.update(Number(req.params.id), req.body)
if (!attribute) {
res
.status(404)
.json({ success: false, message: "Attribute Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Updated Successfully", data: attribute });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}


//delete attribute
export const deleteAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const success = await attributeService.delete(Number(req.params.id));
if (!success) {
res
.status(404)
.json({ success: false, message: "Attribute Not Found" });
} else {
res
.status(200)
.send()
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}
98 changes: 98 additions & 0 deletions src/controllers/AttributeValueController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {Request, Response} from "express";
import { AttributeValueService } from "../services/attributeValue.service";

const attributeValueService = new AttributeValueService();

//create new attribute value
export const createAttributeValue = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValue = await attributeValueService.create(req.body);
if (!attributeValue) {
res
.status(400)
.json({ success: false, message: "Attribute not found" });
} else {
res
.status(201)
.json({ success: true, message: "Attribute Value Created Successfully", data: attributeValue });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//get all attribute values
export const getAllAttributesValues = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValues = await attributeValueService.getAll();
res
.status(200)
.json({ success: true, message: "Attribute Values Retrieved Successfully", data: attributeValues });
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//get attribute value by id
export const getAttributeValueById = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValue = await attributeValueService.getById(Number(req.params.id));
if (!attributeValue) {
res
.status(404)
.json({ success: false, message: "Attribute Value Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Value Retrieved Successfully", data: attributeValue });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//update attribute value
export const updateAttributeValue = async (req: Request, res: Response): Promise<void> => {
try {
const attributeValue = await attributeValueService.update(Number(req.params.id), req.body)
if (!attributeValue) {
res
.status(404)
.json({ success: false, message: "Attribute Value Not Found" });
} else {
res
.status(200)
.json({ success: true, message: "Attribute Value Updated Successfully", data: attributeValue });
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}

//delete attribute value
export const deleteAttributeValue = async (req: Request, res: Response): Promise<void> => {
try {
const success = await attributeValueService.delete(Number(req.params.id));
if (!success) {
res
.status(404)
.json({ success: false, message: "Attribute Value Not Found" });
} else {
res
.status(204)
.send()
}
} catch (error) {
res
.status(500)
.json({ success: false, message: "Internal Server Error", error: error.message });
}
}
72 changes: 72 additions & 0 deletions src/controllers/productVariantAttributeController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Request, Response } from 'express';
import { ProductVariantAttributeService as ProductVariantAttributeServiceClass } from '../services/productVariantAttribute.service';
import AppDataSource from '../config/ormconfig';

const ProductVariantAttributeService = new ProductVariantAttributeServiceClass();

//create product variant attribute
export const createProductVariantAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const productVariantAttribute = await ProductVariantAttributeService.create(req.body);
if (!productVariantAttribute) {
res.status(400).json({ success: false, message: 'Invalid productVariantId or attributeValueId' });
} else {
res.status(201).json({ success: true, message: 'Product Variant Attribute Created Successfully', data: productVariantAttribute });
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}


//get all product variant attributes
export const getAllProductVariantAttributes = async (_req: Request, res: Response): Promise<void> => {
try {
const productVariantAttributes = await ProductVariantAttributeService.getAll();
res.status(200).json({ success: true, message: 'Product Variant Attributes Retrieved', data: productVariantAttributes });
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}

//get product variant attribute by id
export const getProductVariantAttributeById = async (req: Request, res: Response): Promise<void> => {
try {
const productVariantAttribute = await ProductVariantAttributeService.getById(Number(req.params.id));
if (!productVariantAttribute) {
res.status(404).json({ success: false, message: 'Product Variant Attribute not found' });
} else {
res.status(200).json({ success: true, message: 'Product Variant Attribute Retrieved Successfully', data: productVariantAttribute });
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}

//update product variant attribute
export const updateProductVariantAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const productVariantAttribute = await ProductVariantAttributeService.update(Number(req.params.id), req.body);
if (!productVariantAttribute) {
res.status(404).json({ success: false, message: 'Product Variant Attribute not found or invalid data' });
} else {
res.status(200).json({ success: true, message: 'Product Variant Attribute Updated Successfully', data: productVariantAttribute });
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}

//delete product variant attribute
export const deleteProductVariantAttribute = async (req: Request, res: Response): Promise<void> => {
try {
const success = await ProductVariantAttributeService.delete(Number(req.params.id));
if (!success) {
res.status(404).json({ success: false, message: 'Product Variant Attribute not found' });
} else {
res.status(204).send();
}
} catch (error) {
res.status(500).json({ success: false, message: 'Internal Server Error', error: error.message });
}
}
13 changes: 13 additions & 0 deletions src/entities/Attribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from 'typeorm';

@Entity('attributes')
export class Attribute {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@CreateDateColumn()
createdAt: Date;
}
17 changes: 17 additions & 0 deletions src/entities/AttributeValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, CreateDateColumn } from 'typeorm';
import { Attribute } from './Attribute';

@Entity('attribute_values')
export class AttributeValue {
@PrimaryGeneratedColumn()
id: number;

@ManyToOne(() => Attribute)
attribute: Attribute;

@Column()
value: string;

@CreateDateColumn()
createdAt: Date;
}
18 changes: 18 additions & 0 deletions src/entities/ProductVariantAttribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Entity, PrimaryGeneratedColumn, ManyToOne, CreateDateColumn } from 'typeorm';
import { ProductVariant } from './ProductVariant';
import { AttributeValue } from './attributeValue';

@Entity('product_variant_attributes')
export class ProductVariantAttribute {
@PrimaryGeneratedColumn()
id: number;

@ManyToOne(() => ProductVariant)
productVariant: ProductVariant;

@ManyToOne(() => AttributeValue)
attributeValue: AttributeValue;

@CreateDateColumn()
createdAt: Date;
}
18 changes: 18 additions & 0 deletions src/routes/AttributeRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Router } from "express";
import {
createAttribute,
getAllAttributes,
getAttributeById,
updateAttribute,
deleteAttribute,
} from "../controllers/AttributeController";

const router = Router();

router.post("/attributes", createAttribute);
router.get("/attributes", getAllAttributes);
router.get("/attributes/:id", getAttributeById);
router.put("/attributes/:id", updateAttribute);
router.delete("/attributes/:id", deleteAttribute);

export default router;
18 changes: 18 additions & 0 deletions src/routes/AttributeValueRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Router } from "express";
import {
createAttributeValue,
getAllAttributesValues,
getAttributeValueById,
updateAttributeValue,
deleteAttributeValue,
} from "../controllers/AttributeValueController";

const router = Router();

router.post("/attribute-values", createAttributeValue);
router.get("/attribute-values", getAllAttributesValues);
router.get("/attribute-values/:id", getAttributeValueById);
router.put("/attribute-values/:id", updateAttributeValue);
router.delete("/attribute-values/:id", deleteAttributeValue);

export default router;
19 changes: 19 additions & 0 deletions src/routes/ProductVariantAttributeRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Router } from 'express';
import {
createProductVariantAttribute,
getAllProductVariantAttributes,
getProductVariantAttributeById,
updateProductVariantAttribute,
deleteProductVariantAttribute
} from '../controllers/ProductVariantAttributeController';

const router = Router();


router.post('/', createProductVariantAttribute);
router.get('/', getAllProductVariantAttributes);
router.get('/:id', getProductVariantAttributeById);
router.put('/:id', updateProductVariantAttribute);
router.delete('/:id', deleteProductVariantAttribute);

export default router;
Loading
Loading