File: /var/dev/shahnamag/back-end/src/section/section.controller.ts
import { Controller, Get, Post, Body, Patch, Param, Delete, HttpException, HttpStatus } from '@nestjs/common';
import { SectionService } from './section.service';
import { CreateSectionDto } from './dto/create-section.dto';
import { UpdateSectionDto } from './dto/update-section.dto';
import { Public } from 'src/user/auth.guard';
@Controller('section')
export class SectionController {
constructor(private readonly sectionService: SectionService) {}
@Post()
create(@Body() createSectionDto: CreateSectionDto) {
return this.sectionService.create(createSectionDto);
}
@Public()
@Get()
async findAll() {
let sections = await this.sectionService.findAll().catch(error => error);
if(sections instanceof Error)
throw new HttpException({error: 500, message: sections.message}, HttpStatus.INTERNAL_SERVER_ERROR);
for(let section of sections) {
section.title = {fa: section.title, en: section.translation};
delete section.translation;
for(let child of section.children) {
child.title = {fa: child.title, en: child.translation};
delete child.translation;
}
}
return {data: sections, error: 0};
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.sectionService.findOne(+id);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateSectionDto: UpdateSectionDto) {
return this.sectionService.update(+id, updateSectionDto);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.sectionService.remove(+id);
}
}