Developing REST APIs with ExpressWebJs v2(A Nodejs Framework) part 2.
Published: 2021-02-18 07:53:37

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhd64mfpewjs4j24pr59.png)

### We discussed the following in our previous article on [Developing REST APIs with ExpressWebJs v2(A Nodejs Framework) part 1.](https://dev.to/alexigbokwe/developing-rest-apis-with-expresswebjs-v2-a-nodejs-framework-part-1-1fg4):

  .Installing ExpressWebJs
  .Database setup
  .Creating models and migration
  .Creating controllers
  .Creating routes

In this article, we are going to focus on the business logic of our employee record.

Our newly created EmployeesController in <code>App/Http/Controllers</code> directory looks like so:
```js
  "use strict";

        class EmployeesController{
          /**
           * Display a listing of the resource.
           */
          index = async (req, res, next) =>{
            try{
              //
            }catch (error) {
              return next(error);
            }
          }

          /**
           * Show the form for creating a new resource.
           *
           * @return Response
           */
          create = async (req, res, next) => {
            try{
              //
            }catch (error) {
              return next(error);
            }
          }

          /**
           * Store a newly created resource in storage.
           * @param  Request 
           * @return Response
           */
          store = async (req, res, next) => {
            try{
              //
            }catch (error) {
              return next(error);
            }
          }

          /**
           * Display the specified resource.
           * @param  Request
           * @return Response
           */
          show = async (req, res, next) => {
            try{
              //
            }catch (error) {
              return next(error);
            }
          }

          /**
           * Show the form for editing the specified resource.
           * @param  Request
           * @return Response
           */
          edit = async (req, res, next) => {
            try{
              //
            }catch (error) {
              return next(error);
            }
          }

          /**
           * Update the specified resource in storage.
           * @param  Request
           * @return Response
           */
          update = async (req, res, next) => {
            try{
              //
            }catch (error) {
              return next(error);
            }
          }

          /**
           * Remove the specified resource from storage.
           *
           * @param Request
           * @return Response
           */
          destroy = async (req, res, next) => {
            try{
              //
            }catch (error) {
              return next(error);
            }
          }
        }

        module.exports = EmployeesController;
```

## Adding our model.
We can require our model into our controller like so:
```js
const employeesModel = require("../../Model/Employees_model");
```
OR we can do:
```js
const employeesModel = require("@model/Employees_model");
```

Now that we have our employeesModel in our EmployeesController, we can now start querying our database.

### Getting all employees 
```js
const employeesModel = require("@model/Employees_model");

class EmployeesController{
  /**
   * Display a listing of the resource.
  */
  index = async (req, res, next) =>{
    try{
       let employees = await employeesModel.query();
       return res.status(200).json(employees);
    }catch (error) {
       return next(error);
      }
    }
```
### Getting a particular employee 
```js
const employeesModel = require("@model/Employees_model");

class EmployeesController{
  /**
    * Display the specified resource.
    * @param  Request
    * @return Response
  */
  show = async (req, res, next) =>{
    try{
       let employee = await employeesModel.query()
                       .findById(req.params.id);
       if(employee){
         return res.status(200).json(employee);
       }
       return res.status(404).json({msg:'No record found'});
    }catch (error) {
       return next(error);
      }
    }
```

### Saving employee record
```js
const employeesModel = require("@model/Employees_model");

class EmployeesController{
  /**
    * Store a newly created resource in storage.
    * @param  Request 
    * @return Response
   */
  store = async (req, res, next) =>{
    try{
       let employee = await employeesModel.query()
                       .insert(req.body);
       if(employee){
         return res.status(200).json(employee);
       }
       return res.status(404).json({msg:'No record found'});
    }catch (error) {
       return next(error);
      }
    }
```
<code>Note!: we didn't validate our data before saving to storage. Data validation in [ExpressWebJs](https://expresswebjs.com/) will be discussed in my next article. You can also read it up from the documentation site at [ExpressWebJs Data Validation](https://expresswebjs.com/2.0/guide/validation.html)</code>

### Updating employee record
```js
const employeesModel = require("@model/Employees_model");

class EmployeesController{
  /**
    * Update the specified resource in storage.
    * @param  Request 
    * @return Response
   */
  update = async (req, res, next) =>{
    try{
       let employee = await employeesModel.query()
                       .findById(req.params.id)
                       .update(req.body);
       if(employee){
         return res.status(200).json(employee);
       }
       return res.status(404).json({msg:'No record found'});
    }catch (error) {
       return next(error);
      }
    }
```
<code>We can also do</code> 
```js
let employee = await employeesModel.query()
                       .where('id','=',req.params.id)
                       .update(req.body);
```

### Deleting an employee 
```js
const employeesModel = require("@model/Employees_model");

class EmployeesController{
  /**
    * Remove the specified resource in storage.
    * @param  Request 
    * @return Response
   */
  destroy = async (req, res, next) =>{
    try{
       let employee = await employeesModel.query()
                       .findById(req.params.id)
                       .delete(req.body);
       if(employee){
         return res.status(200).json(
          {msg:'Employee successfully removed'}
         );
       }
       return res.status(404).json({msg:'No record found'});
    }catch (error) {
       return next(error);
      }
    }
```
<code>We can also do</code> 
```js
let employee = await employeesModel.query()
                       .deleteById(req.params.id);
```

### Conclusion
Well done! You have learned how to build a rest API with [ExpressWebjs](https://expresswebjs.com/). Need to use [ExpressWebjs](https://expresswebjs.com/) to build your API or micro-service? I'd bet on [ExpressWebjs](https://expresswebjs.com/) as the tool of choice for speed and ease of use.

I will be discussing on [ExpressWebjs Authentication ](https://expresswebjs.com/2.0/guide/security.html#password-hashing) and [ExpressWebjs Data Validation ](https://expresswebjs.com/2.0/guide/validation.html#defining-the-routes) in my next article.
You can follow me on twitter [@EmekaIgbokwe](https://twitter.com/EmekaIgbokwe)
You can follow ExpressWebJs on twitter [@expresswebjs](https://twitter.com/expresswebjs)
and don't forget to star on github [ExpressWebJs](https://github.com/Alexigbokwe/ExpressWebJs)

Please, let me know if you have any questions in the comment section. 😊