Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
form-creator-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
simmctic
form-creator
form-creator-api
Commits
d6d0960b
Commit
d6d0960b
authored
Apr 26, 2019
by
Matheus Horstmann
🐴
Committed by
Lucas Fernandes de Oliveira
Apr 26, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#10
: Add route to post form
Signed-off-by:
Matheus Horstmann
<
mch15@inf.ufpr.br
>
parent
c451d0a8
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
151 additions
and
26 deletions
+151
-26
CHANGELOG.md
CHANGELOG.md
+8
-0
package.json
package.json
+1
-1
src/api/controllers/form.spec.ts
src/api/controllers/form.spec.ts
+89
-2
src/api/controllers/form.ts
src/api/controllers/form.ts
+30
-1
src/main.ts
src/main.ts
+1
-0
src/utils/dbHandler.spec.ts
src/utils/dbHandler.spec.ts
+22
-22
No files found.
CHANGELOG.md
View file @
d6d0960b
...
...
@@ -4,6 +4,14 @@ 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
### Added
-
A route to POST a form #10 (Horstmann)
-
Tests on route POST (Horstmann)
### Changed
-
dbHandler's tests to suit with new forms and inputs insertion
## 0.0.12 - 25-04-2019
### Added
-
OptHandler to standardize constructors from Forms and Inputs #19 (Horstmann)
...
...
package.json
View file @
d6d0960b
{
"name"
:
"form-creator-api"
,
"version"
:
"0.0.1
2
"
,
"version"
:
"0.0.1
3
"
,
"description"
:
"RESTful API used to manage and answer forms."
,
"main"
:
"index.js"
,
"scripts"
:
{
...
...
src/api/controllers/form.spec.ts
View file @
d6d0960b
...
...
@@ -36,7 +36,6 @@
.
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
"
);
...
...
@@ -113,7 +112,7 @@
.
end
(
done
);
});
it
(
"
should respond 500 when getting inexistent
ite
m
"
,
(
done
)
=>
{
it
(
"
should respond 500 when getting inexistent
for
m
"
,
(
done
)
=>
{
request
(
server
)
.
get
(
"
/forms/10
"
)
.
expect
(
500
)
...
...
@@ -127,4 +126,92 @@
.
end
(
done
);
});
it
(
"
should respond 200 when posting valid form
"
,
(
done
)
=>
{
request
(
server
)
.
post
(
"
/forms
"
)
.
send
({
title
:
'
Form Title 4
'
,
description
:
'
Form Description 4
'
,
inputs
:[
{
placement
:
0
,
description
:
'
Description Question 1 Form 4
'
,
question
:
'
Question 1 Form 4
'
,
type
:
1
,
validation
:
[]
}
,
{
placement
:
1
,
description
:
'
Description Question 2 Form 4
'
,
question
:
'
Question 2 Form 4
'
,
type
:
1
,
validation
:
[
{
type
:
3
,
arguments
:
[
'
10
'
]
}
,
{
type
:
4
,
arguments
:
[
'
2
'
]
}
]
}
,
{
placement
:
2
,
description
:
'
Description Question 3 Form 4
'
,
question
:
'
Question 3 Form 4
'
,
type
:
1
,
validation
:
[
{
type
:
1
,
arguments
:
[
'
\\
d{5}-
\\
d{3}
'
]
}
,
{
type
:
2
,
arguments
:
[]
}
]
}
]
})
.
expect
(
200
)
.
end
(
done
);
});
it
(
"
should respond 500 when posting malformed form
"
,
(
done
)
=>
{
request
(
server
)
.
post
(
"
/forms
"
)
.
send
({
description
:
'
Form Description 4
'
,
inputs
:[
{
placement
:
0
,
description
:
'
Description Question 1 Form 4
'
,
question
:
'
Question 1 Form 4
'
,
type
:
1
,
validation
:
[]
}
,
{
placement
:
1
,
description
:
'
Description Question 2 Form 4
'
,
question
:
'
Question 2 Form 4
'
,
type
:
1
,
validation
:
[
{
type
:
3
,
arguments
:
[
'
10
'
]
}
,
{
type
:
4
,
arguments
:
[
'
2
'
]
}
]
}
,
{
placement
:
2
,
description
:
'
Description Question 3 Form 4
'
,
question
:
'
Question 3 Form 4
'
,
type
:
1
,
validation
:
[
{
type
:
1
,
arguments
:
[
'
\\
d{5}-
\\
d{3}
'
]
}
,
{
type
:
2
,
arguments
:
[]
}
]
}
]
})
.
expect
(
500
)
.
expect
((
res
:
any
)
=>
{
const
message
=
"
Could not insert form. Some error has occurred. Check error property for details.
"
;
expect
(
res
.
body
).
to
.
be
.
an
(
"
object
"
);
expect
(
res
.
body
).
to
.
have
.
property
(
"
error
"
);
expect
(
res
.
body
.
message
).
to
.
be
.
equal
(
message
);
})
.
end
(
done
);
});
});
src/api/controllers/form.ts
View file @
d6d0960b
...
...
@@ -19,7 +19,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import
{
Form
}
from
"
../../core/form
"
;
import
{
OptHandler
}
from
"
../../utils/optHandler
"
;
import
{
Form
,
FormOptions
}
from
"
../../core/form
"
;
import
{
Response
,
NextFunction
}
from
"
express
"
;
import
{
Request
}
from
"
../apiTypes
"
;
...
...
@@ -66,4 +67,32 @@ export class FormCtrl {
}
public
static
write
(
req
:
Request
,
res
:
Response
,
next
:
NextFunction
)
{
try
{
const
form
:
Form
=
new
Form
(
OptHandler
.
form
(
req
.
body
));
req
.
db
.
writeForm
(
form
,
(
err
:
Error
,
formResult
:
Form
)
=>
{
if
(
err
){
throw
err
;
}
else
{
res
.
json
({
id
:
formResult
.
id
,
message
:
"
Form added. Id on key 'id'
"
});
return
;
}
});
}
catch
(
e
){
res
.
status
(
500
).
json
({
message
:
"
Could not insert form. Some error has occurred. Check error property for details.
"
,
error
:
e
.
message
});
return
}
}
}
src/main.ts
View file @
d6d0960b
...
...
@@ -49,6 +49,7 @@ app.use("/", DbHandlerMw());
app
.
get
(
"
/forms/
"
,
FormCtrl
.
list
);
app
.
get
(
"
/forms/:id
"
,
FormCtrl
.
read
);
app
.
post
(
"
/forms
"
,
FormCtrl
.
write
);
// Listening
...
...
src/utils/dbHandler.spec.ts
View file @
d6d0960b
...
...
@@ -35,7 +35,7 @@ describe("Database Handler", () => {
it
(
"
should insert a form
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
INSERT INTO form(id, title, description)
\
VALUES
\
(
4, 'Form Title 4', 'Form Description 4
');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
(
5, 'Form Title 5', 'Form Description 5
');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
INSERT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
1
);
...
...
@@ -52,7 +52,7 @@ describe("Database Handler", () => {
(
callback
:
(
err
:
Error
,
result
?:
QueryResult
)
=>
void
)
=>
{
dbhandler
.
executeQuery
(
"
INSERT INTO form(id, title, description)
\
VALUES
\
(
5, 'Form Title 5
', 'Form Description 6');
"
,
callback
);
(
6, 'Form Title 6
', 'Form Description 6');
"
,
callback
);
},
(
cb
:
(
err
:
Error
,
result
?:
QueryResult
)
=>
void
)
=>
{
dbhandler
.
rollback
(
cb
);
...
...
@@ -73,13 +73,13 @@ describe("Database Handler", () => {
dbhandler
.
executeQuery
(
"
SELECT * FROM form;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
SELECT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
4
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
5
);
done
();
});
});
it
(
"
should remove non existent form
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM form WHERE id=
5
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM form WHERE id=
6
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
DELETE
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
0
);
...
...
@@ -89,7 +89,7 @@ describe("Database Handler", () => {
});
it
(
"
should remove existent form
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM form WHERE id=
4
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM form WHERE id=
5
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
DELETE
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
1
);
...
...
@@ -101,8 +101,8 @@ describe("Database Handler", () => {
it
(
"
should insert a input
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
INSERT INTO input(id, id_form, placement, input_type, question, description)
\
VALUES
\
(
9
, 2, 3,'TEXT', 'Question 3 Form 2', 'Description Question 3 Form 2'),
\
(1
0
, 2, 4,'TEXT', 'Question 4 Form 2', 'Description Question 4 Form 2');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
(
12
, 2, 3,'TEXT', 'Question 3 Form 2', 'Description Question 3 Form 2'),
\
(1
3
, 2, 4,'TEXT', 'Question 4 Form 2', 'Description Question 4 Form 2');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
INSERT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
2
);
...
...
@@ -114,14 +114,14 @@ describe("Database Handler", () => {
dbhandler
.
executeQuery
(
"
SELECT * FROM input;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
SELECT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
9
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
12
);
done
();
});
});
it
(
"
should remove non existent input
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input WHERE id=1
1
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input WHERE id=1
4
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
DELETE
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
0
);
...
...
@@ -130,7 +130,7 @@ describe("Database Handler", () => {
});
it
(
"
should remove existent input
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input WHERE id=
9 OR id=10
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input WHERE id=
12 OR id=13
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
DELETE
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
2
);
...
...
@@ -142,8 +142,8 @@ describe("Database Handler", () => {
it
(
"
should insert a input validations
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
INSERT INTO input_validation(id, id_input, validation_type)
\
VALUES
\
(
9
, 2, 'MAXCHAR'),
\
(1
0
, 5, 'MANDATORY');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
(
13
, 2, 'MAXCHAR'),
\
(1
4
, 5, 'MANDATORY');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
INSERT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
2
);
...
...
@@ -155,14 +155,14 @@ describe("Database Handler", () => {
dbhandler
.
executeQuery
(
"
SELECT * FROM input_validation;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
SELECT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
1
0
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
1
4
);
done
();
});
});
it
(
"
should remove non existent input validations
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input_validation WHERE id=1
1
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input_validation WHERE id=1
5
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
DELETE
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
0
);
...
...
@@ -172,7 +172,7 @@ describe("Database Handler", () => {
it
(
"
should remove existent input validations
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input_validation WHERE id=
9 OR id=10
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input_validation WHERE id=
13 OR id=14
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
DELETE
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
2
);
...
...
@@ -183,8 +183,8 @@ describe("Database Handler", () => {
it
(
"
should insert a input validations arguments
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
INSERT INTO input_validation_argument(id, id_input_validation, placement, argument)
\
VALUES
\
(
5
, 1, 2, '10'),
\
(
6
, 2, 2, '2');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
(
8
, 1, 2, '10'),
\
(
9
, 2, 2, '2');
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
INSERT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
2
);
...
...
@@ -197,7 +197,7 @@ describe("Database Handler", () => {
dbhandler
.
executeQuery
(
"
SELECT * FROM input_validation_argument;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
SELECT
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
6
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
9
);
done
();
});
});
...
...
@@ -212,7 +212,7 @@ describe("Database Handler", () => {
});
it
(
"
should remove existent input validations arguments
"
,
(
done
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input_validation_argument WHERE id=
5 OR id=6
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
dbhandler
.
executeQuery
(
"
DELETE FROM input_validation_argument WHERE id=
8 OR id=9
;
"
,
(
err
:
Error
,
result
?:
QueryResult
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
result
.
command
).
to
.
be
.
equal
(
"
DELETE
"
);
expect
(
result
.
rowCount
).
to
.
be
.
equal
(
2
);
...
...
@@ -305,7 +305,7 @@ describe("Read and Write on Database", () => {
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
);
expect
(
forms
.
length
).
to
.
be
.
equal
(
4
);
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
));
...
...
@@ -411,8 +411,8 @@ describe("Read and Write on Database", () => {
const
form
=
new
Form
(
OptHandler
.
form
(
formObj
));
dbhandler
.
writeForm
(
form
,
(
err
:
Error
,
formResult
:
Form
)
=>
{
expect
(
err
).
to
.
be
.
a
(
"
null
"
);
expect
(
formResult
.
id
).
to
.
be
.
equal
(
4
);
let
inputId
:
number
=
8
;
expect
(
formResult
.
id
).
to
.
be
.
equal
(
5
);
let
inputId
:
number
=
11
;
for
(
const
input
of
formResult
.
inputs
){
expect
(
input
.
id
).
to
.
be
.
equal
(
inputId
);
inputId
++
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment