We discussed the following in our previous article on Developing REST APIs with ExpressWebJs v2(A Nodejs Framework) part 1.:
.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 App/Http/Controllers
directory looks like so:
"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:
const employeesModel = require("../../Model/Employees_model");
OR we can do:
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
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
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
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);
}
}
Note!: we didn't validate our data before saving to storage. Data validation in ExpressWebJs will be discussed in my next article. You can also read it up from the documentation site at ExpressWebJs Data Validation
Updating employee record
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);
}
}
We can also do
let employee = await employeesModel.query()
.where('id','=',req.params.id)
.update(req.body);
Deleting an employee
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);
}
}
We can also do
let employee = await employeesModel.query()
.deleteById(req.params.id);
Conclusion
Well done! You have learned how to build a rest API with ExpressWebjs. Need to use ExpressWebjs to build your API or micro-service? I'd bet on ExpressWebjs as the tool of choice for speed and ease of use.
I will be discussing on ExpressWebjs Authentication and ExpressWebjs Data Validation in my next article.
You can follow me on twitter @EmekaIgbokwe
You can follow ExpressWebJs on twitter @expresswebjs
and don't forget to star on github ExpressWebJs
Please, let me know if you have any questions in the comment section. ๐
Top comments (0)