import { ResultModel } from '../../domain/models/result.model'; import { DictionaryModel } from '../../domain/models/dictionary.model'; import { EstAbreviatura } from 'src/domain/entity/tables/estabreviatura.entity'; import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, getConnection } from 'typeorm'; import { NumberUtils } from 'src/utils/number.utils'; @Injectable() export class DictionaryService { constructor( @InjectRepository(EstAbreviatura) private dictionaryRepository: Repository ) { } async findAll(): Promise { return await this.dictionaryRepository.find(); } async findByDescription(description: string): Promise { return await this.dictionaryRepository .createQueryBuilder("estabreviatura") .where("PALAVRA LIKE UPPER(:description)||'%'", { description } ).getMany(); } async find(id: number): Promise { return await this.dictionaryRepository.createQueryBuilder("estabreviatura").where( "ID = :id", { id: id } ).getOne(); } async update(id: number, dados: DictionaryModel) { return await this.dictionaryRepository .createQueryBuilder() .update(EstAbreviatura) .set({ abreviatura: dados.nick, palavra: dados.word }) .where("ID = :id", { id }) .execute(); } async delete(id: number) { return await getConnection() .createQueryBuilder() .delete() .from(EstAbreviatura) .where("ID = :id", { id }) .execute(); } async create(dados: DictionaryModel) { try { const id = NumberUtils.getNewId(9999, 1); const newDictionary = new EstAbreviatura(); newDictionary.id = id; newDictionary.abreviatura = dados.nick; newDictionary.palavra = dados.word; await getConnection() .createQueryBuilder() .insert() .into(EstAbreviatura) .values(newDictionary) .execute(); return newDictionary; } catch ( erro ) { return new ResultModel(true, "Ops! Houve um erro ao criar o DicionĂ¡rio.", null, erro); } } }