RESTController
Definition
RESTController is used for making restful web services and handle the requests made by the client. It allows to handle all REST APIs such as GET, POST, DELETE, PUT, PATCH requests. RestController creates the request DTO and executes the UseCase with this DTO created. Then, it just handles the result to present with the appropriate format and status code, the data to the client.
Examples
Declaring a RESTController
RESTController UpdateTodoRESTController (updateTodoUseCase: UpdateTodoUseCase) {
method: REST.Methods.PUT;
execute(request, response) {
const dto = UpdateTodoRequestDTO({ id: request.params.id, completed: request.body.completed, title: request.body.title });
const result = this.updateTodoUseCase.execute(dto);
if (result is Error) {
switch (result.getClass()) {
case ApplicationErrors.ToDoNotFoundError: {
this.notFound(response, result);
break;
}
default: {
this.fail(response, result.message);
}
}
} else {
this.ok(response);
}
}
}
Syntax
Declaring a RESTController
RESTController <identifier name with a Controller suffix> (<dependencies>) {
method: REST.Methods.<REST API Methods>;
execute (<request>, <response>) {
<statements>
<UseCase execution statement>
<statements>
}
}
References for the above: