...

/

Exercise: Create a REST API

Exercise: Create a REST API

Implement a complete REST API using Deno and Oak in this practical exercise.

We'll cover the following...

The coding exercise below implements a complete REST API server. The server exposes some endpoints to manage fictional employee entities. With these APIs, our server will provide CRUD actions (C = Create, R = Read, U = Update, D = Delete) to any connected client.

Note: For the sake of simplicity, authentication mechanisms have been omitted from the exercise.

In the widget below, we can find the main project structure to realize our first Deno web server. This example lacks portions of code, and it is your task to complete them.

At the end of the exercise, you’ll have developed a fully functional web server with CRUD endpoints!

import { Employee } from '../model/employee.ts';
import * as dataLayer from '../data/data.ts';

export const getAllEmployees = ({ response }: { response: any }) => {
  response.body = dataLayer.getAllRecords();
};

export const getEmployeeById = ({ params, response }: { params: { id: string }; response: any }) => {
  // TODO: Given the id passed as parameter, return the details for the 
  // target employee entity.
};

export const addEmployee = async ({ request, response }: { request: any; response: any }) => {
  // TODO: Add a new employee to the DB using the data passed as Request payload.

  // Hint 1: The following lines of code allow you to deal with the
  // request payload:
  // const body = request.body();
  // await body.value;

  // Hint 2: have a look at data.ts file to see how you can generate a random UUID
};

export const deleteEmployee = ({ params, response }: { params: { id: string }; response: any }) => {
 // TODO: Removed the employee from the DB using the ID passed as parameter.

};

export const updateEmployee = async ({ params, request, response }: {
  params: { id: string };
  request: any;
  response: any;
}) => {
  // Update an existing employee using its ID passed as parameter.
  // The new values are passed as Request payload. You should be able to execute also partial updates,
  // that is, it should be possible to just update the employee name and not the whole object.
};
Deno REST API exercise

Project structure

Keeping a modular project structure separates responsibilities between the different project parts.

...