DEV Community

Cover image for 91-Nodejs Course 2023: Restful Routes: Restful Class
Hasan Zohdy
Hasan Zohdy

Posted on

91-Nodejs Course 2023: Restful Routes: Restful Class

We've done our implementation of our resource method, but we'll have to write the following code for each restful routes (Which we'll use a lot in our real projects).


// src/app/users/routes.ts
import router from "core/router";
import login from "./controllers/auth/login";
import listUsers from './controllers/users/list';
import getUser from './controllers/users/get';
import createUser from './controllers/users/create';
import updateUser from './controllers/users/update';
import patchUser from './controllers/users/patch';

const restfulUser = {
    list: listUsers,
    get: getUser,
    create: createUser,
    update: updateUser,
    patch: patchUser,
};

router.resource("/users", restfulUser);

router.post("/login", login);
Enter fullscreen mode Exit fullscreen mode

That's a lot of code to write, why? because we're (Developers clan) are too lazy,so let's do something better.

🎯 Restful Class

Imagine that we can perform the same thing but just by wrapping our restful methods in a class, and pass the class to the resource method, that's what we're going to do.

📝 Workflow

So here is the how this is going to work:

  • First, we'll create our Restful class, that will contain all the restful methods which are:
  1. list: Get all resources
  2. get: Get a specific resource
  3. create: Create a new resource
  4. update: Update a specific resource
  5. delete: Delete a specific resource
  6. patch: Update a specific resource

Now let's implement what'we just said.

🚀 Implementation

Go to router directory then create restful.ts file and add the following code:

// src/core/router/restful.ts
import { Request, Response } from "core/http";
import { RouteResource } from "core/router/types";

export default class Restful implements RouteResource {
  /**
   * List records
   */
  public async list() {
    //
  }

  /**
   * Get single record
   */
  public async get(request: Request, response: Response) {
    //
  }

  /**
   * Create record
   */
  public async create(request: Request) {
    //
  }

  /**
   * Update record
   */
  public async update(request: Request, response: Response) {
    //
  }

  /**
   * Delete record
   */
  public async delete(request: Request, response: Response) {
    //
  }

  /**
   * Patch record
   */
  public async patch(request: Request, response: Response) {
    //
  }
}
Enter fullscreen mode Exit fullscreen mode

We just created a Restful class, that contains all the restful methods, all of them are of course have no implementation yet.

The question is, how we're going to use it?

🎯 Usage

Before start implementing, we need to know first how this is going to work, to have a clear vision of what we're going to do, let's create a RestfulUser class, that will contain all the restful methods, and we'll use it in our users routes.

// src/app/users/routes.ts
import router from "core/router";
import login from "./controllers/auth/login";
import restfulUser from './controllers/restful-user';

router.resource("/users", restfulUser);

router.post("/login", login);
Enter fullscreen mode Exit fullscreen mode

Now let's implement the restfulUser class.

// src/app/users/controllers/restful-user.ts
import { Request, Response } from "core/http";
import Restful from "core/router/restful";
import { RouteResource } from "core/router/types";

class RestfulUser extends Restful implements RouteResource {
  /**
   * List records
   */
  public async list() {
    //
  }

  /**
   * Get single record
   */
  public async get(request: Request, response: Response) {
    //
  }

  /**
   * Create record
   */
  public async create(request: Request) {
    //
  }

  /**
   * Update record
   */
  public async update(request: Request, response: Response) {
    //
  }

  /**
   * Delete record
   */
  public async delete(request: Request, response: Response) {
    //
  }

  /**
   * Patch record
   */
  public async patch(request: Request, response: Response) {
    //
  }
}

const restfulUser = new RestfulUser();

export default restfulUser;
Enter fullscreen mode Exit fullscreen mode

Nothing here is new, we just created a RestfulUser class, that extends the Restful class, we just created an instance of the users class to be used later.

Let's implement our list method at first.

// src/app/users/controllers/restful-user.ts
import { Request, Response } from "core/http";
import Restful from "core/router/restful";
import { RouteResource } from "core/router/types";
import User from './../models/user';

class RestfulUser extends Restful implements RouteResource {
  /**
   * List records
   */
  public async list(request: Request, response: Response) {
    const users = await User.list();
    return response.success({
      users
    });
  }

  /**
   * Get single record
   */
  public async get(request: Request, response: Response) {
    //
  }

  /**
   * Create record
   */
  public async create(request: Request) {
    //
  }

  /**
   * Update record
   */
  public async update(request: Request, response: Response) {
    //
  }

  /**
   * Delete record
   */
  public async delete(request: Request, response: Response) {
    //
  }

  /**
   * Patch record
   */
  public async patch(request: Request, response: Response) {
    //
  }
}
Enter fullscreen mode Exit fullscreen mode

We listed the users in our list method, and returned the users in the response.

We're going to do this for all the restful methods, and that would work fine.

Is This the best way to do it?

The answer is definitely no, we're going to see how to make it better when we start customizing the restful class to perform what we need for each single method.

This, will make our underlying code (Restful class) a little bit more complex, but the high level code (RestfulUser class) will be much more cleaner and easier to read.

And this is what we're going to do in the next article.

🎨 Conclusion

In this article, we introduced the concept of restful resources, and we implemented the restful class, and we used it in our users routes.

☕♨️ Buy me a Coffee ♨️☕

If you enjoy my articles and see it useful to you, you may buy me a coffee, it will help me to keep going and keep creating more content.

🚀 Project Repository

You can find the latest updates of this project on Github

😍 Join our community

Join our community on Discord to get help and support (Node Js 2023 Channel).

🎞️ Video Course (Arabic Voice)

If you want to learn this course in video format, you can find it on Youtube, the course is in Arabic language.

📚 Bonus Content 📚

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

Packages & Libraries

React Js Packages

Courses (Articles)

Top comments (0)