Skip to content

Controllers

Danny SMc edited this page May 13, 2021 · 4 revisions

Feature: Controllers

This section will be about creating a controller, this controller will be a simple API controller.

1. Create the structure.
Below we shall create a new ./src/controller folder and then create our first controller file.

mkdir ./src/controller
touch ./src/controller/people.ts



2. Let's create our controller.
The below will create our first controller and explain what is happening, add the below to your ./src/controller/people.ts file.

// We import the required pieces of the framework for the controller.
import { Controller, Route, AbstractController, IReturn, IContext } from 'rewyre';

// We create our controller, using the root path, so any paths after would be simply what they show,
// then we define the namespace as `people`.
@Controller('/', 'people')
export class PeopleController extends AbstractController {

	// We are going to create an array of people, I shall add a user.
	private people: Array<any> = [
		{
			id: 'a12b4c51-d717-4ebb-bc9f-0e49169d9f67',
			name: 'John Doe',
			email: 'john.doe@example.com',
			position: 'Manager',
		},
	];

	// We create our first route, which uses the root path, so access would be: http://localhost:8080
	// to access this route from the page. You can see we simply return a status of 200 OK, and then
	// the contents of the JSON, the rewyre framework will automatically use the right return headers
	// and can distinguish between string (HTML) or JSON (data).
	@Route('GET', '/')
	public async list(context: IContext): Promise<IReturn> {
		return { status: 200, content: this.people };
	}

	// Let's create our create function, which is a post request to the root again, same as above,
	// and we shall create a new entry and then respond with a 201 Created.
	@Route('POST', '/')
	public async create(context: IContext): Promise<IReturn> {
		this.people.push(context.body);
		return { status: 201 };
	}

	// Now we shall create our delete function, which uses a parameter to delete the person based
	// on an ID, as you can see we find the ID and try to delete otherwise throw a 404.
	@Route('DELETE', '/:id')
	public async delete(context: IContext): Promise<IReturn> {
		const deleteIndex: number = this.people.map(person => person.id).indexOf(context.params.id);
		if (deleteIndex < 0) return { status: 404 };
		return { status: 204 };
	}

	// Finally we shall create a new method to get a single person, similar to delete.
	@Route('GET', '/:id')
	public async get(context: IContext): Promise<IReturn> {
		const findIndex: number = this.people.map(person => person.id).indexOf(context.params.id);
		if (!findIndex < 0) return { status: 404 };
		return { status: 200, content: this.people[findIndex] };
	}
}



3. Register the controller.
So we have a controller, but we have not registered it, so let's go back to our ./src/application.ts file and register it.

Note the new (v2) database structure to define our database.

import { Framework, Drivers } from 'rewyre';

// Import the controller.
import { PeopleController } from '../controller/people';

(async () => {

	const application: Framework = new Framework({
		port: 8080,
		database: true,
		databases: [
			{
				unique: 'main',
				host: 'localhost',
				port: 27017,
				name: 'demo-rewyre-app',
				driver: Drivers.MONGO,
				default: true,
			},
		],
	});

	// Register the class.
	application.register([ PeopleController ]);

	await application.start();
});

Useful Links:

Clone this wiki locally