Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const Validator = use('Validator');
- const HTTP = use("App/HTTPResponse");
- /**
- * Class model for basic operations
- *
- * @author gabriel
- * @class
- */
- class Operation {
- constructor() {
- this.errors = [];
- this.validator = Validator
- }
- /**
- * Common rules
- *
- * @returns {{}}
- */
- get rules() {
- return {}
- }
- async validate() {
- this.errors = [];
- const validation = await this.validator.validate(this, this.rules);
- if (validation.fails()) {
- validation.messages().map((err) => {
- this.addError(HTTP.STATUS_INTERNAL_SERVER_ERROR, err.message);
- });
- return false
- }
- return true;
- }
- /**
- * Function for adding an error message to the array of errors
- *
- * @param code
- * @param message
- * @returns {Promise<void>}
- */
- async addError(code, message) {
- await this.errors.push({ code, message })
- }
- /**
- * Gets the first error on the errors list
- *
- * @returns {Promise<*>}
- */
- getFirstError() {
- return this.errors[0]
- }
- }
- module.exports = Operation;
- class InvoiceInfoOperation extends Operation {
- ...
- async store() {
- if (!this.validate()) {
- return false;
- }
- ...
- ...
- async store({ request, response }) {
- const op = new InvoiceInfoOperation();
- op.invoiceId = request.input("invoiceId");
- op.invoiceAmount = request.input("invoiceAmount");
- op.paymentTarget = request.input("paymentTarget");
- const store = await op.store();
- if (!store) {
- const error = op.getFirstError();
- return await response.send(error.code);
- // return await response.send(error.code, error.message);
- // return response.status(error.status).send(error.message);
- }
- return response.send(200);
- }
Add Comment
Please, Sign In to add comment