'use strict' // Bookshelf const bookshelf = require('../database/bookshelf').bookshelf const model = require('../models/ingredient-model') // Model validation const Validator = require('jsonschema').Validator const v = new Validator() const IngredientValidation = require("../validations/IngredientValidation") v.addSchema(IngredientValidation, "/IngredientValidation") // Validations const regexXSS = RegExp(/<[^>]*script/) // Error handling const { HttpError } = require('../validations/Errors') class IngredientRepository { constructor() { } getAll() { return new Promise((resolve, reject) => { model.forge() .fetchAll({ withRelated: ['spells'] }) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { console.log(err) reject(new HttpError(500, "Couldn't get ingredients")) }) }) } getOne(id) { return new Promise((resolve, reject) => { model.forge() .where({ 'id' : id }) .fetch({ withRelated: ['spells']}) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { console.log(err) reject(new HttpError(500, "Couldn't get ingredient")) }) }) } } module.exports = IngredientRepository