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

Issue #16: Remove dummies files


Signed-off-by: default avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent fa2ff212
No related branches found
No related tags found
1 merge request!33Develop
......@@ -4,6 +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.13 - 26-04-2019
### Removed
- Dummies files as Item and Collection #16 (Horstmann)
## 0.0.13 - 26-04-2019
### Added
- A route to POST a form #10 (Horstmann)
......
{
"name": "form-creator-api",
"version": "0.0.13",
"version": "0.0.14",
"description": "RESTful API used to manage and answer forms.",
"main": "index.js",
"scripts": {
......
// /*
// * 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 posting valid item", (done) => {
// request(server)
// .post("/item")
// .send({qtd: 30, name: "Arrow"})
// .expect(200)
// .expect((res: any) => {
// const message = "Item added. Id on key 'id'";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("id");
// expect(res.body).to.have.property("status");
// expect(res.body.id).to.be.eql(0);
// expect(res.body.status).to.be.eql(message);
// })
// .end(done);
// });
//
// it("should respond 500 when posting invalid item", (done) => {
// request(server)
// .post("/item")
// .send({name: "Arrow"})
// .expect(500)
// .expect((res: any) => {
// const message = "Invalid body. Check out request body keys";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("error");
// expect(res.body.error).to.be.eql(message);
// })
// .end(done);
// });
//
// it("should respond 200 when putting valid item", (done) => {
// request(server)
// .put("/item/0")
// .send({qtd: 29, name: "Arrow"})
// .expect(200)
// .expect((res: any) => {
// const message = "Item updated. Id on key 'id'";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("id");
// expect(res.body).to.have.property("status");
// expect(res.body.id).to.be.eql("0");
// expect(res.body.status).to.be.eql(message);
// })
// .end(done);
// });
//
// it("should respond 500 when putting invalid item", (done) => {
// request(server)
// .put("/item/0")
// .send({name: "Arrow"})
// .expect(500)
// .expect((res: any) => {
// const message = "Invalid body. Check out request body keys";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("error");
// expect(res.body.error).to.be.eql(message);
// })
// .end(done);
// });
//
// it("should respond 500 when putting inexistent item", (done) => {
// request(server)
// .put("/item/1")
// .send({name: "Arrow", qtd: 30})
// .expect(500)
// .expect((res: any) => {
// const message = "Item with id: '1' not found";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("error");
// expect(res.body.error).to.be.eql(message);
// })
// .end(done);
// });
//
// it("should respond 200 when getting valid item", (done) => {
// request(server)
// .get("/item/0")
// .expect(200)
// .expect((res: any) => {
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("qtd");
// expect(res.body).to.have.property("name");
// expect(res.body.qtd).to.be.eql(29);
// expect(res.body.name).to.be.eql("Arrow");
// })
// .end(done);
// });
//
// it("should respond 500 when getting inexistent item", (done) => {
// request(server)
// .get("/item/1")
// .expect(500)
// .expect((res: any) => {
// const message = "Item with id: '1' not found";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("error");
// expect(res.body.error).to.be.eql(message);
// })
// .end(done);
// });
//
// it("should respond 200 when deleting valid item", (done) => {
// request(server)
// .delete("/item/0")
// .expect(200)
// .expect((res: any) => {
// const message = "Item deleted. Id on key 'id'";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("id");
// expect(res.body).to.have.property("status");
// expect(res.body.id).to.be.eql("0");
// expect(res.body.status).to.be.eql(message);
// })
// .end(done);
// });
//
// it("should respond 500 when deleting inexistent item", (done) => {
// request(server)
// .delete("/item/0")
// .expect(500)
// .expect((res: any) => {
// const message = "Item with id: '0' not found";
// expect(res.body).to.be.an("object");
// expect(res.body).to.have.property("error");
// expect(res.body.error).to.be.eql(message);
// })
// .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 { Item } from "../../core/item";
import { Response, NextFunction } from "express";
import { Request } from "../apiTypes";
export class ItemCtrl {
public static read(req: Request, res: Response, next: NextFunction) {
const item = req.collection.getItem(req.params.id);
if (item) {
res.json(item);
return;
}
else {
res.status(500).json({
error: "Item with id: '" + req.params.id + "' not found"
});
return;
}
}
public static write(req: Request, res: Response, next: NextFunction) {
if (!Item.validator(req.body)) {
res.status(500).json({
error: "Invalid body. Check out request body keys"
});
return;
}
const item: Item = new Item(req.body.qtd, req.body.name);
const id = req.collection.addItem(item);
res.json({
id: id
, status: "Item added. Id on key 'id'"
});
}
public static update(req: Request, res: Response, next: NextFunction) {
if (!Item.validator(req.body)) {
res.status(500).json({
error: "Invalid body. Check out request body keys"
});
return;
}
const item: Item = new Item(req.body.qtd, req.body.name);
const updated = req.collection.updateItem(req.params.id, item);
if (updated) {
res.json({
id: req.params.id
, status: "Item updated. Id on key 'id'"
});
}
else {
res.status(500).json({
error: "Item with id: '" + req.params.id + "' not found"
});
}
}
public static del(req: Request, res: Response, next: NextFunction) {
const i = req.collection.deleteItem(req.params.id);
if (i) {
res.json({
id: req.params.id
, status: "Item deleted. Id on key 'id'"
});
}
else {
res.status(500).json({
error: "Item with id: '" + req.params.id + "' not found"
});
}
}
}
/*
* 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 { expect } from "chai";
import { Collection } from "./collection";
import { Item } from "./item";
describe("collection class", () => {
const collection = new Collection ();
it("should add 3 items to the collection", () => {
const items: Item[] = [
new Item(1, "Bow")
, new Item(30, "Arrow")
, new Item(1, "Sword")
];
for (let i = 0; i < items.length; ++i) {
const id = collection.addItem(items[i]);
expect(id).to.be.equal(i);
}
});
it("should update the second item", () => {
const item: Item = new Item(29, "Arrow");
const updated = collection.updateItem(1, item);
expect(updated).to.be.true;
});
it("should NOT update a inexistent item", () => {
const item: Item = new Item(29, "Arrow");
const updated = collection.updateItem(3, item);
expect(updated).to.be.false;
});
it("should delete the third item", () => {
const deleted = collection.deleteItem(2);
expect(deleted).to.be.true;
});
it("should NOT delete a inexistent item", () => {
const deleted = collection.deleteItem(2);
expect(deleted).to.be.false;
});
it("should get the first item", () => {
const item = collection.getItem(0);
expect(item).to.be.an("object");
expect(item).to.have.property("qtd");
expect(item).to.have.property("name");
expect(item.qtd).to.be.equal(1);
expect(item.name).to.be.equal("Bow");
});
it("should NOT get a inexistent item", () => {
const item = collection.getItem(2);
expect(item).to.be.null;
});
});
/*
* 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 { Item } from "./item";
export class Collection {
private items: Item[];
constructor() {
this.items = [];
}
public addItem(i: Item): number {
this.items.push(i);
return this.items.length - 1;
}
public updateItem(id: number, it: Item): boolean {
if (this.items[id]) {
this.items[id] = it;
return true;
}
else {
return false;
}
}
public deleteItem(id: number): boolean {
if (this.items[id]) {
this.items[id] = null;
return true;
}
else {
return false;
}
}
public getItem(id: number): Item {
if (this.items[id]) {
return this.items[id];
}
else {
return null;
}
}
}
/*
* 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/>.
*/
export class Item {
public readonly qtd: number;
public readonly name: string;
constructor(qtd: number, name: string) {
this.qtd = qtd;
this.name = name;
}
public static validator(json: any) {
return (typeof json.qtd === "number" && typeof json.name === "string");
}
}
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