diff --git a/package.json b/package.json index 7225e10..66e2129 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/controllers/AttributeController.ts b/src/controllers/AttributeController.ts index 89d1638..def38cd 100644 --- a/src/controllers/AttributeController.ts +++ b/src/controllers/AttributeController.ts @@ -1,34 +1,96 @@ -// attribute.controller.ts -import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common'; +import {Request, Response} from 'express'; import { AttributeService } from '../services/attribute.service'; -import { Attribute } from '../entities/Attribute'; - -@Controller('attributes') -export class AttributeController { - constructor(private readonly attributeService: AttributeService) {} - - @Post() - create(@Body() data: Partial) { - return this.attributeService.create(data); - } - - @Get() - findAll() { - return this.attributeService.findAll(); - } - - @Get(':id') - findOne(@Param('id') id: number) { - return this.attributeService.findOne(id); - } - - @Put(':id') - update(@Param('id') id: number, @Body() data: Partial) { - return this.attributeService.update(id, data); - } - - @Delete(':id') - delete(@Param('id') id: number) { - return this.attributeService.delete(id); - } +import AppDataSource from '../config/ormconfig'; + +const attributeService = new AttributeService(); + + +//create new attribute +export const createAttribute = async (req: Request, res: Response): Promise => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 }); + } +} \ No newline at end of file diff --git a/src/controllers/AttributeValueController.ts b/src/controllers/AttributeValueController.ts index 8d2716e..424a074 100644 --- a/src/controllers/AttributeValueController.ts +++ b/src/controllers/AttributeValueController.ts @@ -1,33 +1,98 @@ -import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common'; -import { AttributeValueService } from '../services/attributeValue.service'; -import { AttributeValue } from '../entities/AttributeValue'; +import {Request, Response} from "express"; +import { AttributeValueService } from "../services/attributeValue.service"; -@Controller('attribute-values') -export class AttributeValueController { - constructor(private readonly attributeValueService: AttributeValueService) {} +const attributeValueService = new AttributeValueService(); - @Post() - create(@Body() data: Partial) { - return this.attributeValueService.create(data); - } - - @Get() - findAll() { - return this.attributeValueService.findAll(); - } +//create new attribute value +export const createAttributeValue = async (req: Request, res: Response): Promise => { + 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(':id') - findOne(@Param('id') id: number) { - return this.attributeValueService.findOne(id); - } +//get all attribute values +export const getAllAttributesValues = async (req: Request, res: Response): Promise => { + 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 }); + } +} - @Put(':id') - update(@Param('id') id: number, @Body() data: Partial) { - return this.attributeValueService.update(id, data); - } +//get attribute value by id +export const getAttributeValueById = async (req: Request, res: Response): Promise => { + 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 }); + } +} - @Delete(':id') - delete(@Param('id') id: number) { - return this.attributeValueService.delete(id); - } +//update attribute value +export const updateAttributeValue = async (req: Request, res: Response): Promise => { + 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 => { + 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 }); + } +} \ No newline at end of file diff --git a/src/controllers/ProductVariantController.ts b/src/controllers/ProductVariantController.ts deleted file mode 100644 index 601088b..0000000 --- a/src/controllers/ProductVariantController.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common'; -import { ProductVariantAttributeService } from '../services/productVariant.service'; -import { ProductVariantAttribute } from '../entities/ProductVariantAttribute'; - -@Controller('product-variant-attributes') -export class ProductVariantAttributeController { - constructor( - private readonly productVariantAttributeService: ProductVariantAttributeService, - ) {} - - @Post() - create(@Body() data: Partial) { - return this.productVariantAttributeService.create(data); - } - - @Get() - findAll() { - return this.productVariantAttributeService.findAll(); - } - - @Get(':id') - findOne(@Param('id') id: number) { - return this.productVariantAttributeService.findOne(id); - } - - @Put(':id') - update(@Param('id') id: number, @Body() data: Partial) { - return this.productVariantAttributeService.update(id, data); - } - - @Delete(':id') - delete(@Param('id') id: number) { - return this.productVariantAttributeService.delete(id); - } -} diff --git a/src/controllers/productVariantAttributeController.ts b/src/controllers/productVariantAttributeController.ts new file mode 100644 index 0000000..0b3b54c --- /dev/null +++ b/src/controllers/productVariantAttributeController.ts @@ -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 => { + 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 => { + 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 => { + 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 => { + 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 => { + 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 }); + } +} \ No newline at end of file diff --git a/src/entities/Attribute.ts b/src/entities/Attribute.ts index 0cf653e..7f201fd 100644 --- a/src/entities/Attribute.ts +++ b/src/entities/Attribute.ts @@ -5,9 +5,9 @@ export class Attribute { @PrimaryGeneratedColumn() id: number; - @Column({ unique: true }) + @Column() name: string; @CreateDateColumn() createdAt: Date; -} +} \ No newline at end of file diff --git a/src/entities/AttributeValue.ts b/src/entities/AttributeValue.ts index 38872dd..c314937 100644 --- a/src/entities/AttributeValue.ts +++ b/src/entities/AttributeValue.ts @@ -6,7 +6,7 @@ export class AttributeValue { @PrimaryGeneratedColumn() id: number; - @ManyToOne(() => Attribute, { eager: true }) + @ManyToOne(() => Attribute) attribute: Attribute; @Column() @@ -14,4 +14,4 @@ export class AttributeValue { @CreateDateColumn() createdAt: Date; -} +} \ No newline at end of file diff --git a/src/entities/ProductVariantAttribute.ts b/src/entities/ProductVariantAttribute.ts index e69de29..4944d31 100644 --- a/src/entities/ProductVariantAttribute.ts +++ b/src/entities/ProductVariantAttribute.ts @@ -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; +} \ No newline at end of file diff --git a/src/entities/ProductVariantAttributes.ts b/src/entities/ProductVariantAttributes.ts deleted file mode 100644 index a31d646..0000000 --- a/src/entities/ProductVariantAttributes.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Entity, PrimaryGeneratedColumn, ManyToOne, CreateDateColumn } from 'typeorm'; -import { AttributeValue } from './AttributeValue'; -import { ProductVariant } from './ProductVariant'; - - -@Entity('product_variant_attributes') -export class ProductVariantAttribute { - @PrimaryGeneratedColumn() - id: number; - - @ManyToOne(() => ProductVariant, { eager: true }) - productVariant: ProductVariant; - - @ManyToOne(() => AttributeValue, { eager: true }) - attributeValue: AttributeValue; - - @CreateDateColumn() - createdAt: Date; -} - - - - - diff --git a/src/routes/AttributeRouter.ts b/src/routes/AttributeRouter.ts new file mode 100644 index 0000000..de87066 --- /dev/null +++ b/src/routes/AttributeRouter.ts @@ -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; diff --git a/src/routes/AttributeRoutes.ts b/src/routes/AttributeRoutes.ts deleted file mode 100644 index ee8f0b9..0000000 --- a/src/routes/AttributeRoutes.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Module } from '@nestjs/common'; -import { TypeOrmModule } from '@nestjs/typeorm'; -import { Attribute } from '../entities/Attribute'; -import { AttributeService } from '../services/attribute.service'; -import { AttributeController } from '../controllers/Attribute.controller'; - -@Module({ - imports: [TypeOrmModule.forFeature([Attribute])], - controllers: [AttributeController], - providers: [AttributeService], -}) -export class AttributeModule {} diff --git a/src/routes/AttributeValueRoute.ts b/src/routes/AttributeValueRoute.ts new file mode 100644 index 0000000..b8d1a59 --- /dev/null +++ b/src/routes/AttributeValueRoute.ts @@ -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; diff --git a/src/routes/AttributeValueRoutes.ts b/src/routes/AttributeValueRoutes.ts deleted file mode 100644 index b8079f5..0000000 --- a/src/routes/AttributeValueRoutes.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Module } from '@nestjs/common'; -import { TypeOrmModule } from '@nestjs/typeorm'; -import { AttributeValue } from '../entities/AttributeValue'; -import { AttributeValueService } from '../services/attributeValue.service'; -import { AttributeValueController } from '../controllers/AttributeValueController'; - -@Module({ - imports: [TypeOrmModule.forFeature([AttributeValue])], - controllers: [AttributeValueController], - providers: [AttributeValueService], -}) -export class AttributeValueModule {} diff --git a/src/routes/ProductVariantAttributeRoute.ts b/src/routes/ProductVariantAttributeRoute.ts new file mode 100644 index 0000000..4317288 --- /dev/null +++ b/src/routes/ProductVariantAttributeRoute.ts @@ -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; diff --git a/src/routes/ProductVariantRoutes.ts b/src/routes/ProductVariantRoutes.ts deleted file mode 100644 index 4220c8f..0000000 --- a/src/routes/ProductVariantRoutes.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Module } from '@nestjs/common'; -import { TypeOrmModule } from '@nestjs/typeorm'; -import { ProductVariantAttribute } from '../entities/ProductVariantAttribute'; -import { ProductVariantAttributeService } from '../services/productVariant.service'; -import { ProductVariantAttributeController } from '../controllers/ProductVariantController'; - -@Module({ - imports: [TypeOrmModule.forFeature([ProductVariantAttribute])], - controllers: [ProductVariantAttributeController], - providers: [ProductVariantAttributeService], -}) -export class ProductVariantAttributeModule {} diff --git a/src/services/attribute.service.ts b/src/services/attribute.service.ts index 600c6fe..369e4a7 100644 --- a/src/services/attribute.service.ts +++ b/src/services/attribute.service.ts @@ -1,33 +1,46 @@ +import { Repository } from "typeorm"; +import { Attribute } from "../entities/Attribute"; +import AppDataSource from "../config/ormconfig"; -import { Injectable } from '@nestjs/common'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; -import { Attribute } from '../entities/Attribute'; - -@Injectable() export class AttributeService { - constructor( - @InjectRepository(Attribute) - private attributeRepository: Repository, - ) {} - - create(attribute: Partial) { - return this.attributeRepository.save(attribute); - } - - findAll() { - return this.attributeRepository.find(); - } - - findOne(id: number) { - return this.attributeRepository.findOne({ where: { id } }); - } - - update(id: number, updatedData: Partial) { - return this.attributeRepository.update(id, updatedData); - } - - delete(id: number) { - return this.attributeRepository.delete(id); - } + private repository: Repository; + + constructor() { + this.repository = AppDataSource.getRepository(Attribute); + } + + //create new attribute + async create(data: Partial): Promise { + const attribute = this.repository.create(data); + return await this.repository.save(attribute); + } + + + //get all attributes + async getAll(): Promise { + return await this.repository.find(); + } + + + //get attribute by id + async getById(id: number): Promise { + return await this.repository.findOne({ where: { id } }); + } + + + //update attribute + async update(id: number, data: Partial): Promise { + const attribute = await this.getById(id); + if (!attribute) return null; + + Object.assign(attribute, data); + return await this.repository.save(attribute); + } + + + //delete attribute + async delete(id: number): Promise { + const result = await this.repository.delete(id); + return result.affected === 1; + } } \ No newline at end of file diff --git a/src/services/attributeValue.service.ts b/src/services/attributeValue.service.ts index 914409e..dd7d4f4 100644 --- a/src/services/attributeValue.service.ts +++ b/src/services/attributeValue.service.ts @@ -1,32 +1,65 @@ -import { Injectable } from '@nestjs/common'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; -import { AttributeValue } from '../entities/AttributeValue'; +import { Repository } from "typeorm"; +import { AttributeValue } from "../entities/attributeValue"; +import { Attribute } from "../entities/Attribute"; +import AppDataSource from "../config/ormconfig"; -@Injectable() export class AttributeValueService { - constructor( - @InjectRepository(AttributeValue) - private attributeValueRepository: Repository, - ) {} - - create(data: Partial) { - return this.attributeValueRepository.save(data); - } - - findAll() { - return this.attributeValueRepository.find(); - } - - findOne(id: number) { - return this.attributeValueRepository.findOne({ where: { id } }); - } - - update(id: number, updatedData: Partial) { - return this.attributeValueRepository.update(id, updatedData); - } - - delete(id: number) { - return this.attributeValueRepository.delete(id); - } + private repository: Repository; + + constructor() { + this.repository = AppDataSource.getRepository(AttributeValue); + } + + //create new attribute value + async create(data: {value: string; attributeId: number}): Promise { + const attributeRepo = AppDataSource.getRepository(Attribute); + const attribute = await attributeRepo.findOne({ where: { id: data.attributeId } }); + + if (!attribute) return null; + + const attributeValue = this.repository.create({ value: data.value, attribute }); + return await this.repository.save(attributeValue); + } + + + //get all attribute values + async getAll(): Promise { + return await this.repository.find({ relations: ['attribute'] }); + } + + + //get attribute value by id + async getById(id: number): Promise { + return await this.repository.findOne({ where: { id }, relations: ['attribute'] }); + } + + + //update attribute value + async update(id: number, data: { value?: string; attributeId?: number }): Promise { + const attributeValue = await this.getById(id); + if (!attributeValue) return null; + + if (data.attributeId) { + const attributeRepo = AppDataSource.getRepository(Attribute); + const attribute = await attributeRepo.findOne({ where: { id: data.attributeId } }); + if (attribute) { + attributeValue.attribute = attribute; + } + } + + if (data.value) { + attributeValue.value = data.value; + } + + return await this.repository.save(attributeValue); + } + + + + //delete attribute value + async delete(id: number): Promise { + const result = await this.repository.delete(id); + return result.affected === 1; + } } + diff --git a/src/services/productVariant.service.ts b/src/services/productVariant.service.ts deleted file mode 100644 index eb377a3..0000000 --- a/src/services/productVariant.service.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Repository } from 'typeorm'; -import { ProductVariant } from '../entities/ProductVariant'; -import { Product } from '../entities/Product'; -import AppDataSource from '../config/ormconfig'; - -export class ProductVariantService { - private repository: Repository; - - constructor() { - this.repository = AppDataSource.getRepository(ProductVariant); - } - - async create(data: Partial, productId: number): Promise { - const productRepo = AppDataSource.getRepository(Product); - const product = await productRepo.findOne({ where: { id: productId } }); - - if (!product) return null; - - const productVariant = this.repository.create({ ...data, product }); - return await this.repository.save(productVariant); - } - - async getAll(): Promise { - return await this.repository.find({ relations: ['product'] }); - } - - async getById(id: number): Promise { - return await this.repository.findOne({ where: { id }, relations: ['product'] }); - } - - async update(id: number, data: Partial): Promise { - const variant = await this.getById(id); - if (!variant) return null; - - Object.assign(variant, data); - return await this.repository.save(variant); - } - - async delete(id: number): Promise { - const result = await this.repository.delete(id); - return result.affected === 1; - } -} diff --git a/src/services/productVariantAttribute.service.ts b/src/services/productVariantAttribute.service.ts new file mode 100644 index 0000000..32308af --- /dev/null +++ b/src/services/productVariantAttribute.service.ts @@ -0,0 +1,75 @@ +import { Repository } from "typeorm"; +import { ProductVariantAttribute } from "../entities/ProductVariantAttribute"; +import { ProductVariant } from "../entities/ProductVariant"; +import { AttributeValue } from "../entities/attributeValue"; +import AppDataSource from "../config/ormconfig"; + +export class ProductVariantAttributeService { + private repository: Repository; + + constructor() { + this.repository = AppDataSource.getRepository(ProductVariantAttribute); + } + + //create new product variant attribute + async create (data: { productvariantId: number; attributeValueId: number}): Promise { + const productVariantRepo = AppDataSource.getRepository(ProductVariant) + const attributeValueRepo = AppDataSource.getRepository(AttributeValue) + + const productVariant = await productVariantRepo.findOne({ where: { id: data.productvariantId } }); + const attributeValue = await attributeValueRepo.findOne({ where: { id: data.attributeValueId } }); + + if (!productVariant || !attributeValue) return null; + + const productVariantAttribute = this . repository.create ({ + productVariant, + attributeValue + }); + + return await this.repository.save(productVariantAttribute); + } + + + //get all product variant attributes + async getAll(): Promise { + return await this.repository.find({ relations: ['productVariant', 'attributeValue'] }); + } + + + //get product variant attribute by id + async getById(id: number): Promise { + return await this.repository.findOne({ where: { id }, relations: ['productVariant', 'attributeValue'] }); + } + + + //update product variant attribute + async update(id: number, data: { productVariantId?: number; attributeValueId?: number}): Promise { + const productVariantAttribute = await this.getById(id); + if (!productVariantAttribute) return null; + + + if (data.productVariantId) { + const productVariantRepo = AppDataSource.getRepository(ProductVariant); + const productVariant = await productVariantRepo.findOne({ where: { id: data.productVariantId } }); + if (productVariant) { + productVariantAttribute.productVariant = productVariant; + } + } + + if (data.attributeValueId) { + const attributeValueRepo = AppDataSource.getRepository(AttributeValue); + const attributeValue = await attributeValueRepo.findOne({ where: { id: data.attributeValueId } }); + if (attributeValue) { + productVariantAttribute.attributeValue = attributeValue; + } + } + + return await this.repository.save(productVariantAttribute); + } + + //delete product variant attribute + async delete(id: number): Promise { + const result = await this.repository.delete(id); + return result.affected === 1; + } +} \ No newline at end of file