docs

Controllers

Responsible for handling requests and directing them to the appropriate action, the controller is a class and can have as many functions as you want, but if you use the .resources method or if you generate the controller using the command line, by default, the functions that must exists in the controller are:

Examples 1: Controller for API’s generated by CLI:

import type { Request, Response } from "@hefestos/core";
import ApiResponse from "app/utils/ApiResponse";
import { UserService } from "app/services/User";
import UserValidator from "app/validations/User";

export default class UserController {
  static async index(request: Request, response: Response) {
    try {
      const { page, filter } = request.query as {
        page?: number;
        filter?: string;
      };

      const data = await UserService.List(page, filter);

      return ApiResponse.pagination(response, data);
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async show(request: Request, response: Response) {
    try {
      const { id } = request.params;
      const data = await UserService.Show(id);

      return ApiResponse.success(response, data);
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async store(request: Request, response: Response) {
    try {
      await UserValidator.Create(request.body);
      const data = await UserService.Create(request.body);

      return ApiResponse.success(response, data);
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async update(request: Request, response: Response) {
    try {
      await UserValidator.Update(request.body);
      const { id } = request.params;
      const data = await UserService.Update(id, request.body);

      return ApiResponse.success(response, data);
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async destroy(request: Request, response: Response) {
    try {
      const { id } = request.params;
      const data = await UserService.Delete(id);

      return ApiResponse.success(response, data);
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }
}

Example 2: Controller for Full Stack applications generated by CLI:

import type { Request, Response } from "@hefestos/core";
import ApiResponse from "app/utils/ApiResponse";
import { UserService } from "app/services/User";
import UserValidator from "app/validations/User";

export default class UserController {
  static async index(request: Request, response: Response) {
    const { page, filter } = request.query as {
      page?: number;
      filter?: string;
    };

    const data = await UserService.List(page, filter);

    return response.render("users/list", { title: "List User", data });
  }

  static async show(request: Request, response: Response) {
    try {
      const { id } = request.params;
      const data = await UserService.Show(id);

      return response.render("users/show", { data });
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async create(request: Request, response: Response) {
    return response.render("users/create", { title: "Create User" });
  }

  static async store(request: Request, response: Response) {
    try {
      await UsersValidator.Create(request.body);
      const data = await UserService.Create(request.body);

      return ApiResponse.success(response, data, "/users");
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async edit(request: Request, response: Response) {
    try {
      const { id } = request.params;
      const data = await UserService.Show(id);

      return response.render("users/edit", { data });
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async update(request: Request, response: Response) {
    try {
      await UsersValidator.Update(request.body);
      const { id } = request.params;
      const data = await UserService.Update(id, request.body);

      return ApiResponse.success(response, data, "/users");
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }

  static async destroy(request: Request, response: Response) {
    try {
      const { id } = request.params;
      const data = await UserService.Delete(id);

      return ApiResponse.success(response, data, "/users");
    } catch (error: any) {
      return ApiResponse.error(response, error);
    }
  }
}

You can import the Request and Response interfaces from within “core”, for example: import type { Request, Response } from "core";. If you are building an api, you can also import and use ApiResponse which contains the functions:

To understand more about the CLI, read about Generate files.

Summary