91-Nodejs Course 2023: Restful Routes: Restful Class
Tags: node, typescript, mongodb, fastify
Published: 2022-12-09 09:10:50

---
title: 91-Nodejs Course 2023: Restful Routes: Restful Class
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4emf9ehjlsmj7afs5ms.png
# Use a ratio of 100:42 for best results.
---

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).

```ts

// 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);
```

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:

```ts
// 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) {
    //
  }
}
```

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.

```ts
// 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);
```

Now let's implement the `restfulUser` class.

```ts
// 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;
```

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.

```ts
// 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) {
    //
  }
}
```

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](https://www.paypal.com/paypalme/hassanzohdy), 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](https://github.com/hassanzohdy/nodejs-2023)

## 😍 Join our community

Join our community on [Discord](https://discord.gg/pb2vmdfhGf) 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](https://www.youtube.com/playlist?list=PLGO8ntvxgiZMJc7RN2lIq9WmMOlWZGzmz), 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

- [Event Driven Architecture: A Practical Guide in Javascript](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
- [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
- [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)

Packages & Libraries

- [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
- [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
- [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)

 React Js Packages

- [useFetcher: easiest way to fetch data in React Js](https://dev.to/hassanzohdy/usefetcher-the-easiest-way-to-fetch-data-in-react-45o9)

Courses (Articles)

- [OOP In JS And TS From The Very Beginning](https://dev.to/hassanzohdy/jsts-oop-course-1-introduction-2ld)
- [ES6++: The Ultimate Guide to ES6 and Beyond](https://dev.to/hassanzohdy/es6-1-introduction-h3e)
- [React Js: Let"s Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)