diff --git a/src/modules/product/entities/attribute-value.entity.ts b/src/modules/product/entities/attribute-value.entity.ts index 4a287fd..182c4bd 100644 --- a/src/modules/product/entities/attribute-value.entity.ts +++ b/src/modules/product/entities/attribute-value.entity.ts @@ -1,9 +1,10 @@ import { Entity, Property, ManyToOne, PrimaryKey, Unique } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; +import { Attribute } from './attribute.entity'; // import { Attribute } from './attribute.entity'; @Entity({ tableName: 'attribute_values' }) -@Unique({ properties: ['value', 'attributeId'] }) +// @Unique({ properties: ['value', 'attribute'] }) export class AttributeValue extends BaseEntity { @PrimaryKey({ autoincrement: true, type: 'bigint' }) id: bigint; @@ -14,8 +15,8 @@ export class AttributeValue extends BaseEntity { // @ManyToOne(() => Attribute) // attribute: Attribute; - @Property({ type: 'bigint' }) - attributeId: bigint; + @ManyToOne(()=>Attribute) + attribute: Attribute; @Property({ type: 'int', nullable: true }) order?: number; diff --git a/src/modules/product/entities/attribute.entity.ts b/src/modules/product/entities/attribute.entity.ts index 985a1c6..5113a72 100644 --- a/src/modules/product/entities/attribute.entity.ts +++ b/src/modules/product/entities/attribute.entity.ts @@ -1,7 +1,8 @@ -import { Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core'; +import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; import { AttributeType } from '../interface/product.interface'; import { Product } from './product.entity'; +import { AttributeValue } from './attribute-value.entity'; @Entity({ tableName: 'attributes' }) export class Attribute extends BaseEntity { @@ -12,7 +13,10 @@ export class Attribute extends BaseEntity { parent?: Attribute | null @OneToMany(() => Attribute, (c) => c.parent) - children?: Attribute[] + children=new Collection(this) + + @OneToMany(() => AttributeValue, (attrValue) => attrValue.attribute) + values= new Collection(this) @PrimaryKey({ type: 'bigint', autoincrement: true }) id: bigint; diff --git a/src/modules/product/entities/category.entity.ts b/src/modules/product/entities/category.entity.ts index 7cb6a25..30867d5 100644 --- a/src/modules/product/entities/category.entity.ts +++ b/src/modules/product/entities/category.entity.ts @@ -10,7 +10,7 @@ export class Category extends BaseEntity { parent?: Category | null @OneToMany(() => Category, (c) => c.parent) - children?: Category[] + children = new Collection(this) @PrimaryKey({ type: 'bigint', autoincrement: true }) id: bigint diff --git a/src/modules/product/providers/attribute-value.service.ts b/src/modules/product/providers/attribute-value.service.ts index 8c4976c..dea64dd 100644 --- a/src/modules/product/providers/attribute-value.service.ts +++ b/src/modules/product/providers/attribute-value.service.ts @@ -24,7 +24,7 @@ export class AttributeValueService { } const data: RequiredEntityData = { - attributeId: attribute.id, + attribute, value: dto.value, order: dto.order, }; @@ -54,7 +54,7 @@ export class AttributeValueService { } findAll(attributeId: number) { - return this.attributeValueRepository.find({ attributeId }); + return this.attributeValueRepository.find({ attribute: { id: attributeId } }); } async findById(attributeValueId: number): Promise { diff --git a/src/modules/product/providers/attribute.service.ts b/src/modules/product/providers/attribute.service.ts index 856328f..689b167 100644 --- a/src/modules/product/providers/attribute.service.ts +++ b/src/modules/product/providers/attribute.service.ts @@ -79,7 +79,7 @@ export class AttributeService { async findById(attributeId: number): Promise { const attribute = await this.attributeRepository.findOne({ id: attributeId }, - { populate: ['children', 'product'] }); + { populate: ['children', 'product','values'] }); if (!attribute) throw new NotFoundException('Attribute not found'); return attribute; diff --git a/src/modules/product/providers/product.service.ts b/src/modules/product/providers/product.service.ts index 7d45c6a..7207af1 100644 --- a/src/modules/product/providers/product.service.ts +++ b/src/modules/product/providers/product.service.ts @@ -69,7 +69,8 @@ export class ProductService { } async findById(productId: string): Promise { - const product = await this.productRepository.findOne({ id: productId }, { populate: ['category', 'attributes'] }); + const product = await this.productRepository.findOne({ id: productId }, + { populate: ['category', 'attributes', 'attributes.values'] }); if (!product) throw new NotFoundException(productMessage.NOT_FOUND); return product;