attribute values

This commit is contained in:
2026-01-16 20:23:15 +03:30
parent 8e3a2b0dec
commit 2ddf892793
6 changed files with 16 additions and 10 deletions
@@ -1,9 +1,10 @@
import { Entity, Property, ManyToOne, PrimaryKey, Unique } from '@mikro-orm/core'; import { Entity, Property, ManyToOne, PrimaryKey, Unique } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Attribute } from './attribute.entity';
// import { Attribute } from './attribute.entity'; // import { Attribute } from './attribute.entity';
@Entity({ tableName: 'attribute_values' }) @Entity({ tableName: 'attribute_values' })
@Unique({ properties: ['value', 'attributeId'] }) // @Unique({ properties: ['value', 'attribute'] })
export class AttributeValue extends BaseEntity { export class AttributeValue extends BaseEntity {
@PrimaryKey({ autoincrement: true, type: 'bigint' }) @PrimaryKey({ autoincrement: true, type: 'bigint' })
id: bigint; id: bigint;
@@ -14,8 +15,8 @@ export class AttributeValue extends BaseEntity {
// @ManyToOne(() => Attribute) // @ManyToOne(() => Attribute)
// attribute: Attribute; // attribute: Attribute;
@Property({ type: 'bigint' }) @ManyToOne(()=>Attribute)
attributeId: bigint; attribute: Attribute;
@Property({ type: 'int', nullable: true }) @Property({ type: 'int', nullable: true })
order?: number; order?: number;
@@ -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 { BaseEntity } from '../../../common/entities/base.entity';
import { AttributeType } from '../interface/product.interface'; import { AttributeType } from '../interface/product.interface';
import { Product } from './product.entity'; import { Product } from './product.entity';
import { AttributeValue } from './attribute-value.entity';
@Entity({ tableName: 'attributes' }) @Entity({ tableName: 'attributes' })
export class Attribute extends BaseEntity { export class Attribute extends BaseEntity {
@@ -12,7 +13,10 @@ export class Attribute extends BaseEntity {
parent?: Attribute | null parent?: Attribute | null
@OneToMany(() => Attribute, (c) => c.parent) @OneToMany(() => Attribute, (c) => c.parent)
children?: Attribute[] children=new Collection<Attribute>(this)
@OneToMany(() => AttributeValue, (attrValue) => attrValue.attribute)
values= new Collection<AttributeValue>(this)
@PrimaryKey({ type: 'bigint', autoincrement: true }) @PrimaryKey({ type: 'bigint', autoincrement: true })
id: bigint; id: bigint;
@@ -10,7 +10,7 @@ export class Category extends BaseEntity {
parent?: Category | null parent?: Category | null
@OneToMany(() => Category, (c) => c.parent) @OneToMany(() => Category, (c) => c.parent)
children?: Category[] children = new Collection<Category>(this)
@PrimaryKey({ type: 'bigint', autoincrement: true }) @PrimaryKey({ type: 'bigint', autoincrement: true })
id: bigint id: bigint
@@ -24,7 +24,7 @@ export class AttributeValueService {
} }
const data: RequiredEntityData<AttributeValue> = { const data: RequiredEntityData<AttributeValue> = {
attributeId: attribute.id, attribute,
value: dto.value, value: dto.value,
order: dto.order, order: dto.order,
}; };
@@ -54,7 +54,7 @@ export class AttributeValueService {
} }
findAll(attributeId: number) { findAll(attributeId: number) {
return this.attributeValueRepository.find({ attributeId }); return this.attributeValueRepository.find({ attribute: { id: attributeId } });
} }
async findById(attributeValueId: number): Promise<AttributeValue> { async findById(attributeValueId: number): Promise<AttributeValue> {
@@ -79,7 +79,7 @@ export class AttributeService {
async findById(attributeId: number): Promise<Attribute> { async findById(attributeId: number): Promise<Attribute> {
const attribute = await this.attributeRepository.findOne({ id: attributeId }, const attribute = await this.attributeRepository.findOne({ id: attributeId },
{ populate: ['children', 'product'] }); { populate: ['children', 'product','values'] });
if (!attribute) throw new NotFoundException('Attribute not found'); if (!attribute) throw new NotFoundException('Attribute not found');
return attribute; return attribute;
@@ -69,7 +69,8 @@ export class ProductService {
} }
async findById(productId: string): Promise<Product> { async findById(productId: string): Promise<Product> {
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); if (!product) throw new NotFoundException(productMessage.NOT_FOUND);
return product; return product;