File: //var/dev/shahnamag/back-end/src/section/section.service.ts
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateSectionDto } from './dto/create-section.dto';
import { UpdateSectionDto } from './dto/update-section.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Section } from './entities/section.entity';
@Injectable()
export class SectionService {
constructor(
@InjectRepository(Section) private readonly sectionRepository: Repository<Section>,
) {}
create(createSectionDto: CreateSectionDto) {
return 'This action adds a new section';
}
async findAll() {
let sections = await this.sectionRepository.find({
relations: ['children', 'parent'],
order: {
start: 'ASC',
children: { start: 'ASC' }
}
}).catch(error => error);
if(sections instanceof Error)
throw new HttpException({error: 500, message: sections.message}, HttpStatus.INTERNAL_SERVER_ERROR);
return sections.filter(section => section.parent == null).map(section => {delete section.parent; return section;});
}
findOne(id: number) {
return `This action returns a #${id} section`;
}
update(id: number, updateSectionDto: UpdateSectionDto) {
return `This action updates a #${id} section`;
}
remove(id: number) {
return `This action removes a #${id} section`;
}
}