File: //var/dev/shahnamag/back-end/src/keyword/keyword.service.ts
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateKeywordDto } from './dto/create-keyword.dto';
import { UpdateKeywordDto } from './dto/update-keyword.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Keyword } from './entities/keyword.entity';
import { Repository } from 'typeorm';
@Injectable()
export class KeywordService {
constructor(@InjectRepository(Keyword) private keywordRepo: Repository<Keyword>) {}
create(createKeywordDto: CreateKeywordDto) {
return 'This action adds a new keyword';
}
async findAll() {
return await this.keywordRepo.find({relations: ['verseKeywords']});
}
async findOne(id: number, loadVerses: boolean = false): Promise<Keyword | null> {
let relations = loadVerses ? ['verses'] : undefined;
return await this.keywordRepo.findOne({where: {id}, relations});
}
async update(id: number, updateKeywordDto: UpdateKeywordDto) {
const keyword = await this.keywordRepo.findOneById(id);
if(!keyword)
throw new HttpException({error: 'کلمه کلیدی پیدا نشد.'}, HttpStatus.NOT_FOUND);
let {desc, ...obj} = updateKeywordDto;
obj.descFa = desc?.fa;
obj.descEn = desc?.en;
await this.keywordRepo.update(id, obj);
return {error: 0, data: true};
}
remove(id: number) {
return `This action removes a #${id} keyword`;
}
}