import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Request,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { NoAuthToken } from 'src/common/decorator/no-token.decorator';
import { TokenData } from 'src/common/decorator/token-data.decorator';
import { TokenDataEntity } from 'src/common/entities/token-data.entity';
import { ArticleManageService } from './article_manage.service';
import { CreateArticleManageDto } from './dto/create-article_manage.dto';
import { UpdateArticleManageDto } from './dto/update-article_manage.dto';
import { updatePortionArticle } from './dto/update-portion-article.dto';
@ApiTags('文章管理')
@Controller('article-manage')
@NoAuthToken()
export class ArticleManageController {
  constructor(private readonly articleManageService: ArticleManageService) {}

  @Post()
  @ApiOperation({ summary: '新建文章' })
  create(
    @Body() createArticleManageDto: CreateArticleManageDto,
    @Request() req,
  ) {
    return this.articleManageService.create(createArticleManageDto, req);
  }

  @Get()
  @ApiOperation({ summary: '获取所有文章' })
  findAll(@Request() req) {
    return this.articleManageService.findAll(req);
  }

  @Get('manuals/:id')
  @ApiOperation({ summary: '获取手册下的文章' })
  findManualsAll(@Param('id') id: string, @TokenData() token: TokenDataEntity) {
    return this.articleManageService.findManualsAll(id, token);
  }

  @Get(':id')
  @ApiOperation({ summary: '根据ID获取文章' })
  findOne(@Param('id') id: string, @Request() req) {
    return this.articleManageService.findOne(+id, req);
  }

  @Patch()
  @ApiOperation({ summary: '编辑文章' })
  update(
    @Body() updateArticleManageDto: UpdateArticleManageDto,
    @Request() req,
  ) {
    return this.articleManageService.update(updateArticleManageDto, req);
  }
  @Post('portion')
  @ApiOperation({ summary: '批量编辑文章' })
  updatePortion(@Body() data: updatePortionArticle, @Request() req) {
    return this.articleManageService.updatePortion(data, req);
  }
  @Post('sort')
  @ApiOperation({ summary: '文章排序' })
  sort(@Body() sortIdArray: number[]) {
    return this.articleManageService.sort(sortIdArray);
  }
  @Delete(':id')
  @ApiOperation({ summary: '删除文章' })
  remove(@Param('id') id: string, @Request() req) {
    return this.articleManageService.remove(+id, req);
  }
}
