Category - CRUD

1. CategoryController

    import {
Controller,
Get,
HttpCode,
Param,
Put,
Delete,
Body,
Query,
HttpException,
HttpStatus,
Post,
UseGuards,
Logger,
} from "@nestjs/common";

import { JwtAuthGuard } from "src/guard/jwt-auth.guard";
import { CategoryService } from "./category.service";

@Controller("api/v1/categories")
export class CategoryController {

private readonly logger = new Logger(CategoryController.name);

constructor(private readonly categoryService: CategoryService) {}

@Get()
@HttpCode(200)
async getCategories() {
const data = await this.categoryService.getAllCategories();
return data;
}

@UseGuards(JwtAuthGuard)
@Post()
@HttpCode(201)
async addCategory(@Body() category: any) {
try {
const data = await this.categoryService.addCategory(category);
return data;
} catch (err) {
const message = { errorMessage: err.message };
throw new HttpException(message, HttpStatus.BAD_REQUEST);
}
}

@UseGuards(JwtAuthGuard)
@Put(":id")
@HttpCode(200)
async updateCategory(@Param("id") id: number, @Body() category: any) {
try {
category.id = id;
const data = await this.categoryService.updateCategory(category);
return data;
} catch (err) {
const message = { errorMessage: err.message };
throw new HttpException(message, HttpStatus.BAD_REQUEST);
}
}


@Get(":id")
@HttpCode(200)
async getCategory(@Param("id") id: number) {
const data = await this.categoryService.getCategory(id);
return data;
}

@UseGuards(JwtAuthGuard)
@Delete(":id")
@HttpCode(200)
async deleteCategory(@Param("id") id: number) {
const data = await this.categoryService.deleteCategory(id);
return data;
}
}

2. Category Service

    import { Injectable, Logger } from '@nestjs/common';
import { CategoryDAO } from './category.dao';

@Injectable()
export class CategoryService {

private readonly logger = new Logger(CategoryService.name);

constructor(private readonly categoryDAO: CategoryDAO) {}

async getAllCategories() {
const categories = await this.categoryDAO.findAll();
return categories;
}

async getCategory(id:number) {
const categories = await this.categoryDAO.findOne(id);
return categories;
}

async addCategory(category:any) {
const categoryId = await this.categoryDAO.save(category);
return categoryId;
}

async updateCategory(category:any) {
const result = await this.categoryDAO.update(category);
return result;
}

async deleteCategory(id:number) {
const categories = await this.categoryDAO.delete(id);
return categories;
}
}

3. CategoryDAO

    import { Injectable, Logger } from "@nestjs/common";
import { Connection } from "typeorm";

@Injectable()
export class CategoryDAO {
private readonly logger = new Logger(CategoryDAO.name);

constructor(private connection: Connection) {}

async findAll() {
const sql =
"select ct.id,ct.name, count(*) as noOfCourses from ct_categories ct, ct_courses c where c.category = ct.id group by ct.id, ct.name order by ct.display_order";
const results = await this.connection.query(sql);
return results;
}

async findOne(id: number) {
const sql =
"select ct.id,ct.name, count(*) as noOfCourses from ct_categories ct, ct_courses c where c.category = ct.id and ct.id = ?";
const results = await this.connection.query(sql, [id]);
return results.length > 0 ? results[0] : null;
}


async save(category:any) {
const sql = "insert into ct_categories (name) values (?)";
const result = await this.connection.query(sql, [category.name]);
return result.insertId;
}

async delete(id: number) {
const sql = "delete from ct_categories where id = ? ";
const result = await this.connection.query(sql, [id]);
return result.affectedRows;
}

async update(category:any) {
const sql = "update ct_categories set name = ? where id = ? ";
const result = await this.connection.query(sql, [category.name, category.id]);
return result.affectedRows;
}
}

CategoryModule

    import { Module } from '@nestjs/common';
import { CategoryController } from './category.controller';
import { CategoryDAO } from './category.dao';
import { CategoryService } from './category.service';

@Module({
controllers: [CategoryController],
providers: [CategoryService, CategoryDAO]
})
export class CategoryModule {}