Commit c60bffaf authored by Diego Iván's avatar Diego Iván
Browse files

agregando servicio para category

parent 2a54fab1
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
import { Injectable } from '@nestjs/common';
import { CreateCategoryDto } from './dto/create-category.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Category } from './entities/category.entity';
import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { CreateCategoryReqDto } from './dto/create-category-req.dto';

@Injectable()
export class CategoryService {
  constructor(@InjectRepository(Category) private categoryRepository) {}

  async create(createCategoryDto: CreateCategoryReqDto): Promise<void> {
    const categoriyEN: CreateCategoryDto = {
      name: createCategoryDto.nameEN,
      language: LANGUAGES.EN,
    };

    const insertedId: number = (await this.categoryRepository.insert(categoriyEN)).raw.insertId;

    await this.categoryRepository.insert({
      idCategory: insertedId,
      name: createCategoryDto.nameES,
      language: LANGUAGES.ES,
    });
  }

  async findAll(lang: string) {
    return await this.categoryRepository.find({ where: { language: lang }, select: ['idCategory', 'name'] });
  }

  // update(id: number, updateCategoryDto: UpdateCategoryDto) {
  //   return `This action updates a #${id} category`;
  // }

  async remove(idCategory: number) {
    await this.categoryRepository.delete({ idCategory });
  }
}