Skip to content
Snippets Groups Projects
Commit e325d395 authored by Matheus Horstmann's avatar Matheus Horstmann :horse: Committed by Lucas Fernandes de Oliveira
Browse files

Issue #8: Create route to list forms


Signed-off-by: default avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent 10e7c204
No related branches found
No related tags found
1 merge request!33Develop
......@@ -4,7 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 0.0.8 - 10-04-2019
### Changed
- main.ts to remove more dummie class
- dbHandler to include method listForms
### Added
- Form controller and method to list all forms
## 0.0.7 - 10-04-2019
### Changed
- main.ts to include dbHandler Middleware and remove dummie class
......
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as request from "supertest";
import { expect } from "chai";
import * as server from "../../main";
describe("API data controller", () => {
it("should respond 200 when getting a list of forms", (done) => {
request(server)
.get("/forms")
.expect(200)
.expect((res: any) => {
expect(res.body).to.be.an("array");
expect(res.body[0].id).to.be.equal(1);
expect(res.body[0].title).to.be.equal("Form Title 1");
expect(res.body[0].description).to.be.equal("Form Description 1");
expect(res.body[1].id).to.be.equal(2);
expect(res.body[1].title).to.be.equal("Form Title 2");
expect(res.body[1].description).to.be.equal("Form Description 2");
expect(res.body[2].id).to.be.equal(3);
expect(res.body[2].title).to.be.equal("Form Title 3");
expect(res.body[2].description).to.be.equal("Form Description 3");
})
.end(done);
});
});
/*
* form-creator-api. RESTful API to manage and answer forms.
* Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
*
* This file is part of form-creator-api.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Form } from "../../core/form";
import { Response, NextFunction } from "express";
import { Request } from "../apiTypes";
export class FormCtrl {
public static list(req: Request, res: Response, next: NextFunction) {
req.db.listForms((err: Error, forms?: Form[]) => {
if (err){
res.status(500).json({
message: "Could not list forms. Some error has occurred. Check error property for details.",
error: err
});
return;
}
if (forms) {
const mappedForms = forms.map(form => ({
id: form.id,
title: form.title,
description: form.description
}));
res.json(mappedForms);
return;
}
});
}
}
......@@ -39,18 +39,16 @@ const port = process.env.PORT;
import { DbHandlerMw } from "./api/middlewares/dbHandler";
// Include controllers
import { ItemCtrl } from "./api/controllers/item";
import { FormCtrl } from "./api/controllers/form";
// Setup middlewares
app.use("/", bodyParser.json());
app.use("/", CollectionMw());
app.use("/", DbHandlerMw());
// Setup routes
app.get("/item/:id", ItemCtrl.read);
app.post("/item", ItemCtrl.write);
app.put("/item/:id", ItemCtrl.update);
app.delete("/item/:id", ItemCtrl.del);
app.get("/forms/", FormCtrl.list);
// Listening
......
......@@ -300,6 +300,19 @@ describe("Database Handler", () => {
describe("Read and Write on Database", () => {
const dbhandler = new DbHandler(configs.poolconfig);
it("should list all forms", (done) => {
dbhandler.listForms((err: Error, forms?: Form[]) => {
expect(err).to.be.a("null");
expect(forms.length).to.be.equal(3);
for (let i = 0; i < forms.length; i++){
expect(forms[i].id).to.be.equal(i + 1);
expect(forms[i].title).to.be.equal("Form Title " + (i + 1));
expect(forms[i].description).to.be.equal("Form Description " + (i + 1));
}
done();
});
});
it("should read an existent form", (done) => {
dbhandler.readForm(1, (err: Error, form: Form) => {
expect(err).to.be.a("null");
......
......@@ -90,6 +90,37 @@ export class DbHandler {
this.executeQuery("BEGIN;", cb);
}
/**
* Asynchronously list all forms in database.
* @param cb - Callback function which contains the data read.
* @param cb.err - Error information when the method fails.
* @param cb.form - list of form or a empty list if there is no form on database.
*/
public listForms(cb: (err: Error, forms?: Form[]) => void){
const query: string = "SELECT id, title, description FROM form;";
const forms: Form[] = [];
this.executeQuery(query, (err: Error, result?: QueryResult) => {
if (err) {
cb(err);
return;
}
for (const row of result.rows){
const inputs: Input[] = [];
const formTmp = new Form (
row["title"],
row["description"],
inputs,
row["id"]
);
forms.push(formTmp);
}
cb(err, forms);
});
}
/**
* Asynchronously executes a query and get a Form.
* @param id - Form identifier to be founded.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment