import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateKnowledgePointDto } from '../../dto/dtos-question-manager/knowledge-point/create-knowledge-point.dto';
import { UpdateKnowledgePointDto } from '../../dto/dtos-question-manager/knowledge-point/update-knowledge-point.dto';
import { KnowledgePoint } from '../../entities/entities-question-manager/knowledge-point.entity';

@Injectable()
export class KnowledgePointService {
  constructor(
    @InjectRepository(KnowledgePoint)
    private knowledgePointRepository: Repository<KnowledgePoint>,
  ) {}

  /* 
    创建知识点
  */
  async create(createKnowledgePointDto: CreateKnowledgePointDto) {
    const knowledgeItem = await this.knowledgePointRepository.findOne({
      where: { name: createKnowledgePointDto.name, delFlag: 0 },
    });
    if (!knowledgeItem) {
      return await this.knowledgePointRepository.save(createKnowledgePointDto);
    }
    return knowledgeItem;
  }
  /* 
    查找所有知识点
  */
  async findAll() {
    return this.knowledgePointRepository.find({
      select: ['id', 'name'],
      where: { delFlag: 0 },
    });
  }
  /* 
    更新知识点
  */
  async update(id: number, updateKnowledgePointDto: UpdateKnowledgePointDto) {
    return this.knowledgePointRepository.update(id, updateKnowledgePointDto);
  }
  /* 
    删除知识点
  */
  async remove(id: number) {
    await this.knowledgePointRepository.update(id, { delFlag: 1 });
    return true;
  }
}
