Handling the API request with an API Controller

Inside of the users/useCases/createUser folder, we find the CreateUserController.

The CreateUserController has a single dependency injected into it. The CreateUserUseCase. It's the Application Service/Use Case that does all the action.

The responsibilities of a controller are to:

  • Invoke the CreateUserUseCase.
  • Respond to the API call with the appropriate HTTP response code based on the result of the executed use case.

See the CreateUserController below.

// CreateUsersController - Handling the request data from 
// the internet and passing it to the dependency injected 
// use case class for execution.

// users/useCases/createUser/CreateUserController.ts

import { CreateUserUseCase } from "./CreateUserUseCase"
import { CreateUserDTO } from "./CreateUserDTO"
import { CreateUserErrors } from "./CreateUserErrors"
import { BaseController } from "../../../../shared/infra/http/models/BaseController"
import { TextUtils } from "../../../../shared/utils/TextUtils"
import { DecodedExpressRequest } from "../../infra/http/models/decodedRequest"
import * as express from "express"

export class CreateUserController extends BaseController {
  private useCase: CreateUserUseCase

  constructor(useCase: CreateUserUseCase) {
    super()
    this.useCase = useCase
  }

  async executeImpl(
    req: DecodedExpressRequest,
    res: express.Response
  ): Promise<any> {
    let dto: CreateUserDTO = req.body as CreateUserDTO

    dto = {
      username: TextUtils.sanitize(dto.username),
      email: TextUtils.sanitize(dto.email),
      password: dto.password,
    }

    try {
      /**
       * Execute the CreateUserUseCase use case.
       */

      const result = await this.useCase.execute(dto)

      if (result.isLeft()) {
        const error = result.value

        /**
         * This is an example of how one can standardize a
         * RESTful API's error messages.
         *
         * We utilize the Use Case Error types.
         */

        switch (error.constructor) {
          case CreateUserErrors.UsernameTakenError:
            return this.conflict(error.errorValue().message)
          case CreateUserErrors.EmailAlreadyExistsError:
            return this.conflict(error.errorValue().message)
          default
            return this.fail(res, error.errorValue().message)
        }
      } else {
        return this.ok(res)
      }
    } catch (err) {
      return this.fail(res, err)
    }
  }
}

Like this?: If you like the way this Express.js controller uses declarative response methods, check out "Clean & Consistent Express.js Controllers | Enterprise Node.js + TypeScript".

Howdy 👋

This is an online wiki about the main topics over the last 40 years of software design, architecture & testing. It was created by Khalil Stemmler.

This wiki's mission is to help eliminate the unknown unknowns, helping you get up to speed on the best practices and principles to write testable, flexible, and maintainable code.

I created this wiki while I was connecting the dots on my own path. You can read more about that learning journey in the introduction

Already bought it?

If you’ve already purchased the book, click here to re-send your link. You can read the online wiki or download a copy of the book in PDF, EPUB, and Kindle versions.

Want access?

You can read the intro to the book for free and visit solidbook.io to buy the book/wiki! To get an idea of my writing, read some of my best free content here and here.

Need help?

Something not working? Have a question? You can reach me on at khalil@khalilstemmler.com.