remove attribute
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
import { IsString, IsOptional, IsInt, Min } from 'class-validator';
|
||||
import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class CreateAttributeValueDto {
|
||||
@IsString()
|
||||
@ApiProperty({ example: 'سلفون' })
|
||||
value: string;
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
@Type(() => Number)
|
||||
@ApiPropertyOptional({ example: 1 })
|
||||
order?: number;
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum, IsNotEmpty } from 'class-validator';
|
||||
import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { FieldType } from 'src/modules/form-builder/interface/print';
|
||||
|
||||
export class CreateAttributeDto {
|
||||
@IsString()
|
||||
@ApiProperty({ example: 'نوع روکش' })
|
||||
name: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@ApiPropertyOptional()
|
||||
parentId?: number;
|
||||
|
||||
|
||||
@IsBoolean()
|
||||
@Type(() => Boolean)
|
||||
@ApiPropertyOptional({ example: true })
|
||||
isRequired: boolean;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsEnum(FieldType)
|
||||
@ApiProperty({ enum: FieldType, example: FieldType.select })
|
||||
type: FieldType;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
@Type(() => Number)
|
||||
@ApiPropertyOptional({ example: 1 })
|
||||
order?: number;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateAttributeValueDto } from './create-attribute-value.dto';
|
||||
|
||||
export class UpdateAttributeValueDto extends
|
||||
PartialType(CreateAttributeValueDto) { }
|
||||
@@ -1,5 +0,0 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateAttributeDto } from './create-attribute.dto';
|
||||
|
||||
export class UpdateAttributeDto extends PartialType(CreateAttributeDto) {
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
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', 'attribute'] })
|
||||
export class AttributeValue extends BaseEntity {
|
||||
@PrimaryKey({ autoincrement: true, type: 'bigint' })
|
||||
id: bigint;
|
||||
|
||||
@Property()
|
||||
value: string;
|
||||
|
||||
// @ManyToOne(() => Attribute)
|
||||
// attribute: Attribute;
|
||||
|
||||
@ManyToOne(()=>Attribute)
|
||||
attribute: Attribute;
|
||||
|
||||
@Property({ type: 'int', nullable: true })
|
||||
order?: number;
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Product } from './product.entity';
|
||||
import { AttributeValue } from './attribute-value.entity';
|
||||
import { FieldType } from 'src/modules/form-builder/interface/print';
|
||||
|
||||
@Entity({ tableName: 'attributes' })
|
||||
export class Attribute extends BaseEntity {
|
||||
@ManyToOne(() => Product)
|
||||
product!: Product
|
||||
|
||||
@ManyToOne(() => Attribute, { nullable: true })
|
||||
parent?: Attribute | null
|
||||
|
||||
@OneToMany(() => Attribute, (c) => c.parent)
|
||||
children = new Collection<Attribute>(this)
|
||||
|
||||
@OneToMany(() => AttributeValue, (attrValue) => attrValue.attribute)
|
||||
values = new Collection<AttributeValue>(this)
|
||||
|
||||
@PrimaryKey({ type: 'bigint', autoincrement: true })
|
||||
id: bigint;
|
||||
|
||||
@Property()
|
||||
name: string;
|
||||
|
||||
@Enum(() => FieldType)
|
||||
type: FieldType;
|
||||
|
||||
@Property({ type: 'boolean', default: false })
|
||||
isRequired: boolean = false;
|
||||
|
||||
@Property({ type: 'int', nullable: true })
|
||||
order?: number;
|
||||
|
||||
}
|
||||
@@ -1,7 +1 @@
|
||||
// export enum AttributeType{
|
||||
// SELECT='select',
|
||||
// TEXT='text',
|
||||
// NUMBER='number',
|
||||
// CHECKBOX='checkbox',
|
||||
// BOOLEAN='boolean',
|
||||
// }
|
||||
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
import { BadGatewayException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { AttributeValueRepository } from '../repositories/attribute-value.repository';
|
||||
import { AttributeRepository } from '../repositories/attribute.repository';
|
||||
import { AttributeValue } from '../entities/attribute-value.entity';
|
||||
import { CreateAttributeValueDto } from '../dto/create-attribute-value.dto';
|
||||
import { UpdateAttributeValueDto } from '../dto/update-attribute-value.dto';
|
||||
|
||||
@Injectable()
|
||||
export class AttributeValueService {
|
||||
|
||||
constructor(
|
||||
private readonly attributeValueRepository: AttributeValueRepository,
|
||||
private readonly attributeRepository: AttributeRepository,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(attributeId: number, dto: CreateAttributeValueDto) {
|
||||
|
||||
const attribute = await this.attributeRepository.findOne({ id: attributeId })
|
||||
if (!attribute) {
|
||||
throw new BadGatewayException('attribute not found')
|
||||
}
|
||||
|
||||
const data: RequiredEntityData<AttributeValue> = {
|
||||
attribute,
|
||||
value: dto.value,
|
||||
order: dto.order,
|
||||
};
|
||||
|
||||
const attributeValue = this.attributeValueRepository.create(data)
|
||||
|
||||
await this.em.persistAndFlush(attributeValue)
|
||||
|
||||
return attributeValue
|
||||
|
||||
}
|
||||
|
||||
async update(attributeValueId: number, dto: UpdateAttributeValueDto) {
|
||||
|
||||
const attributeValue = await this.attributeValueRepository.findOne({ id: attributeValueId })
|
||||
|
||||
if (!attributeValue) {
|
||||
throw new NotFoundException('Attribute value not found');
|
||||
}
|
||||
|
||||
this.attributeValueRepository.assign(attributeValue, dto)
|
||||
|
||||
this.em.persistAndFlush(attributeValue)
|
||||
|
||||
return attributeValue
|
||||
|
||||
}
|
||||
|
||||
findAll(attributeId: number) {
|
||||
return this.attributeValueRepository.find({ attribute: { id: attributeId } });
|
||||
}
|
||||
|
||||
async findById(attributeValueId: number): Promise<AttributeValue> {
|
||||
const attributeValue = await this.attributeValueRepository.findOne({ id: attributeValueId });
|
||||
|
||||
if (!attributeValue) throw new NotFoundException('Attribute value not found');
|
||||
return attributeValue;
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
const attributeValue = await this.findById(id);
|
||||
if (!attributeValue) {
|
||||
throw new NotFoundException('attribute value not found');
|
||||
}
|
||||
|
||||
attributeValue.deletedAt = new Date();
|
||||
return await this.em.persistAndFlush(attributeValue);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
import { BadGatewayException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { AttributeRepository } from '../repositories/attribute.repository';
|
||||
import { CreateAttributeDto } from '../dto/create-attribute.dto';
|
||||
import { Attribute } from '../entities/attribute.entity';
|
||||
import { ProductRepository } from '../repositories/product.repository';
|
||||
import { UpdateAttributeDto } from '../dto/update-attribute.dto';
|
||||
|
||||
@Injectable()
|
||||
export class AttributeService {
|
||||
|
||||
constructor(
|
||||
private readonly attributeRepository: AttributeRepository,
|
||||
private readonly productRepository: ProductRepository,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async create(productId: number, dto: CreateAttributeDto) {
|
||||
let parent: null | Attribute = null
|
||||
|
||||
const product = await this.productRepository.findOne({ id: productId })
|
||||
if (!product) {
|
||||
throw new BadGatewayException('Product not found')
|
||||
}
|
||||
|
||||
if (dto.parentId) {
|
||||
parent = await this.attributeRepository.findOne({ id: dto.parentId })
|
||||
if (!parent) {
|
||||
throw new BadGatewayException('Parent attribute not found')
|
||||
}
|
||||
}
|
||||
|
||||
const data: RequiredEntityData<Attribute> = {
|
||||
product,
|
||||
name: dto.name,
|
||||
isRequired: dto.isRequired,
|
||||
order: dto.order,
|
||||
type: dto.type,
|
||||
parent
|
||||
};
|
||||
|
||||
const attribute = this.attributeRepository.create(data)
|
||||
|
||||
await this.em.persistAndFlush(attribute)
|
||||
|
||||
return attribute
|
||||
|
||||
}
|
||||
|
||||
async update(attributeId: number, dto: UpdateAttributeDto) {
|
||||
|
||||
const attribute = await this.attributeRepository.findOne({ id: attributeId })
|
||||
|
||||
if (!attribute) {
|
||||
throw new NotFoundException('Attribute not found');
|
||||
}
|
||||
|
||||
if (dto.parentId) {
|
||||
const parent = await this.attributeRepository.findOne({ id: dto.parentId })
|
||||
if (!parent) {
|
||||
throw new NotFoundException('parent attribute not found');
|
||||
}
|
||||
|
||||
this.attributeRepository.assign(attribute, { parent })
|
||||
}
|
||||
|
||||
this.attributeRepository.assign(attribute, dto)
|
||||
|
||||
this.em.persistAndFlush(attribute)
|
||||
|
||||
return attribute
|
||||
|
||||
}
|
||||
|
||||
findAll(productId: number) {
|
||||
return this.attributeRepository.find({ product: { id: productId } });
|
||||
}
|
||||
|
||||
async findById(attributeId: number): Promise<Attribute> {
|
||||
const attribute = await this.attributeRepository.findOne({ id: attributeId },
|
||||
{ populate: ['children', 'product','values'] });
|
||||
|
||||
if (!attribute) throw new NotFoundException('Attribute not found');
|
||||
return attribute;
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
const attribute = await this.findById(id);
|
||||
if (!attribute) {
|
||||
throw new NotFoundException('attribute not found');
|
||||
}
|
||||
if (attribute.children && attribute.children.length > 0) {
|
||||
throw new NotFoundException('attribute has children');
|
||||
}
|
||||
attribute.deletedAt = new Date();
|
||||
return await this.em.persistAndFlush(attribute);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { AttributeValue } from '../entities/attribute-value.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class AttributeValueRepository extends EntityRepository<AttributeValue> {
|
||||
constructor(readonly em: EntityManager) {
|
||||
super(em, AttributeValue);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { Attribute } from '../entities/attribute.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class AttributeRepository extends EntityRepository<Attribute> {
|
||||
constructor(readonly em: EntityManager) {
|
||||
super(em, Attribute);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user