File

src/category/category.service.ts

Index

Methods

Constructor

constructor(categoryRp: Repository<CategoryEntity>)
Parameters :
Name Type Optional
categoryRp Repository<CategoryEntity> No

Methods

Async deleteCategory
deleteCategory(id: any)
Parameters :
Name Type Optional
id any No
Returns : unknown
Async findAllCategorys
findAllCategorys()
Returns : unknown
Async findAllCategorysActive
findAllCategorysActive()
Returns : unknown
Async saveCategory
saveCategory(category: any)
Parameters :
Name Type Optional
category any No
Returns : unknown
Async updateCategory
updateCategory(id: number, category: any)
Parameters :
Name Type Optional
id number No
category any No
Returns : unknown
import { Injectable } from '@nestjs/common';
import { InjectRepository} from '@nestjs/typeorm'
import { Repository } from 'typeorm';
import { CategoryEntity } from 'src/database/entity/category-entity/category-entity';
import { ExceptionErrorMessage } from 'src/validation/exception-error';
@Injectable()
export class CategoryService {
    constructor(@InjectRepository(CategoryEntity)
    private readonly categoryRp:Repository<CategoryEntity>){

    }
    //Guardar Categoria
    async saveCategory(category:any){
        try {
            return await this.categoryRp.save(category);
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }

    //Actualizar Categoria
    async updateCategory(id:number, category:any){
        try {
            await this.categoryRp.update(id,category);
            return await this.categoryRp.findOneBy({id:id});
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }

    //Listar Categorias
    async findAllCategorys(){
        try {
            return await this.categoryRp.find({order:{createdAt:'DESC'}});
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }
    

    //Listar Categorias
    async findAllCategorysActive(){
        try {
            return await this.categoryRp.find({order:{name:'ASC'},where:{status:true}});
        } catch (error) {
            ExceptionErrorMessage(error);            
        }
    }



    //Borrar Categoria
    async deleteCategory(id:any){
        try {
            await this.categoryRp.createQueryBuilder()
                .update(CategoryEntity)
                .set({ status: false})
                .where("id = :id", { id: id})
                .execute();
            return {message:"Se ha modificado el registro seleccionado"};
        }
        catch(error){
            ExceptionErrorMessage(error);            
        }
    }


}






results matching ""

    No results matching ""