Commit ddbede99 authored by Matheus Horstmann's avatar Matheus Horstmann 🐴

Issue #23: Create OptHandler for FormAnswer and InputAnswer

Signed-off-by: Matheus Horstmann's avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent c5d757f8
Pipeline #20429 passed with stages
in 1 minute and 4 seconds
......@@ -4,6 +4,17 @@ 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.17 - 25-05-2019
### Added
- inputAnswer method in OptHandler #23 (Horstmann)
- formAnswer method in OptHandler #23 (Horstmann)
### Changed
- FormsAnswer class to have an dictionary of InputsAnswer
- FormsAnswer's constructor to use dictionary
## 0.0.16 - 06-05-2019
### Added
- A FormsAnswer Class to store answers from forms #22 (Horstmann)
......
......@@ -21,7 +21,7 @@
import { OptHandler } from "../utils/optHandler";
import { Form } from "./form";
import { InputAnswer, InputAnswerDict, InputAnswerOptions } from "./inputAnswer";
import { InputAnswer, InputAnswerDict, InputAnswerOptions, InputAnswerOptionsDict } from "./inputAnswer";
/** Parameters used to create a FormAnswer object. */
export interface FormAnswerOptions {
......@@ -35,8 +35,9 @@ export interface FormAnswerOptions {
timestamp: Date;
/** A dictionary of inputsAnswers. containing the answers */
inputsAnswer: InputAnswerDict;
inputsAnswerOptions: InputAnswerOptionsDict;
}
/**
* Form Class to manage project's forms
*/
......@@ -48,7 +49,7 @@ export class FormAnswer {
/** Time when it is answered */
public readonly timestamp: Date;
/** Array of inputsAnswer. containing the answers */
public readonly inputAnswers: InputAnswer[];
public readonly inputAnswers: InputAnswerDict;
/**
* Creates a new instance of FormAnswer Class
......@@ -58,26 +59,13 @@ export class FormAnswer {
this.id = options.id ? options.id : null;
this.form = options.form;
this.timestamp = options.timestamp;
this.inputAnswers = [];
for (const inputId of Object.keys(options.inputsAnswer)) {
const inputAnswerArray: InputAnswer[] = options.inputsAnswer[inputId].map((i: InputAnswer, p: number) => {
/** When a user answers it, it may not contain the placement and which input this answer is from
* so needs to check before create a InputAnswerOptions, and enumerate placement and inputsId in case this came from a POST
* Else if it came from a GET the DB already have the id, placement and idInput so needs to be equal DB
*/
const inputAnswerstmp: InputAnswerOptions = {
id: (i.id ? i.id : null)
, idInput: (i.idInput ? i.idInput : inputId)
, placement: (i.placement ? i.placement : p)
, value: i.value
};
return new InputAnswer(inputAnswerstmp);
});
this.inputAnswers = this.inputAnswers.concat(inputAnswerArray);
}
this.inputAnswers = {};
for (const key of Object.keys(options.inputsAnswerOptions)){
this.inputAnswers[parseInt(key, 10)] = options.inputsAnswerOptions[parseInt(key, 10)].map( (i: InputAnswerOptions) => {
return new InputAnswer(i);
});
}
}
}
......@@ -31,12 +31,18 @@
value: string;
}
/** Parameters used to create a dictionary to uses as an collection of answers. */
export interface InputAnswerDict {
/** Parameters used to create a dictionary to uses as an collection of InputAnswerOptions Object. */
export interface InputAnswerOptionsDict {
/** A key is a identifier of a Input instance */
[key: number]: InputAnswerOptions[];
}
/** Parameters used to create a dictionary to uses as an collection of InputAnswer Object.. */
export interface InputAnswerDict {
/** A key is a identifier of a Input instance */
[key: number]: InputAnswer[];
}
/**
* Input Class to manage project's inputs forms
*/
......
This diff is collapsed.
......@@ -19,15 +19,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { FormOptions } from "../core/form";
import { Input, InputOptions, Validation } from "../core/input";
import { InputType} from "./enumHandler";
import { ErrorHandler} from "./errorHandler";
import { Form, FormOptions } from "../core/form";
import { FormAnswerOptions } from "../core/formAnswer";
import { InputOptions, Validation } from "../core/input";
import { InputAnswer, InputAnswerDict, InputAnswerOptions, InputAnswerOptionsDict } from "../core/inputAnswer";
import { InputType} from "./enumHandler";
import { ErrorHandler} from "./errorHandler";
/**
* OptHandler to handle an object and transform into a Classoptions to be used in Class's constructor
*/
export class OptHandler {
export class OptHandler {
/**
* Return an FormOptions instance with a parsed object, The main objective is parse any error previously
* @param obj - object that should be parsed.
......@@ -88,10 +90,66 @@
})
};
// for ( const tmpValidation of obj.validation ){
// option.validation.push({type: tmpValidation.type, arguments: tmpValidation.arguments});
// }
return option;
}
/**
* Return an formAnswerOptions instance with a parsed and validated object, The main objective is parse any error previously
* @param obj - object that should be parsed.
* @returns - An FormAnswerOptions instance.
*/
public static formAnswer(obj: any): FormAnswerOptions{
if (obj.form === undefined || !(obj.form instanceof Form)){
throw ErrorHandler.notFound("Form");
}
if (obj.timestamp === undefined || !(obj.timestamp instanceof Date)){
throw ErrorHandler.notFound("Answer Date");
}
if (obj.inputsAnswerOptions === undefined ){
throw ErrorHandler.notFound("Input Answers");
}
const inputsAnswerOptionsTmp: InputAnswerOptionsDict = {};
for (const key of Object.keys(obj.inputsAnswerOptions)){
inputsAnswerOptionsTmp[parseInt(key, 10)] = obj.inputsAnswerOptions[parseInt(key, 10)].map( (i: InputAnswerOptions) => {
return OptHandler.inputAnswer(i);
});
}
const option: FormAnswerOptions = {
id: obj.id
, form: obj.form
, timestamp: obj.timestamp
, inputsAnswerOptions: inputsAnswerOptionsTmp
};
return option;
}
/**
* Return an InputAnswerOptions instance with a parsed and validated object, The main objective is parse any error previously
* @param obj - object that should be parsed.
* @returns - An InputAnswerOptions instance.
*/
public static inputAnswer(obj: any): InputAnswerOptions{
if (obj.idInput === undefined){
throw ErrorHandler.notFound("idInput");
}
if (obj.placement === undefined){
throw ErrorHandler.notFound("Placement");
}
if (obj.value === undefined){
throw ErrorHandler.notFound("Value");
}
const option: InputAnswerOptions = {
idInput: obj.idInput
, placement: obj.placement
, value: obj.value
};
return option;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment