JSFiddle - React, Tailwind, and code Playground

by Hugo Carneiro

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

JavaScript

var sampleData = {
	apps: [
    { id: 1, title: 'Lorem', published: true, userId: 123 },
    { id: 2, title: 'Ipsum', published: false, userId: 123 },
    { id: 3, title: 'Dolor', published: true, userId: 456 },
    { id: 4, title: 'Sit', published: true, userId: 789 },
    { id: 5, title: 'Amet', published: false, userId: 123 },
    { id: 6, title: 'Et', published: true, userId: 123 }
  ],
  organizations: [
  	{ id: 1, name: 'Google', suspended: true, userId: 123 },
    { id: 2, name: 'Apple', suspended: false, userId: 456 },
    { id: 3, name: 'Fliplet', suspended: false, userId: 123 }
  ]
}

// @TODO: This is the model/class you should work out
var User = function(user) {
	this.id = user.id;
}

User.prototype.select = function(key) {
  this.selectKey = key;
  return this;
};

User.prototype.attributes = function(array) {
	this.attributesArr = array;
  return this;
};

User.prototype.where = function(query) {
	query.userId = this.id;
  this.whereQuery = query;
  return this;
};

User.prototype.order = function(order) {
  this.orderArr = order;
  return this;
};

User.prototype.findAll = function() {
	var _this = this; 
	return new Promise(function(resolve, reject) {
  	var data;
  	if (sampleData.hasOwnProperty(_this.selectKey)) {
    	data = sampleData[_this.selectKey];
    }

    data = _.sortBy(_.map(_.filter(data, _this.whereQuery), function(obj) {
      return _.pick(obj, _this.attributesArr);
    }), _this.orderArr);

  	resolve(data);
  });
};

User.prototype.findOne = function() {
	var _this = this; 
	return new Promise(function(resolve, reject) {
  	var data;
  	if (sampleData.hasOwnProperty(_this.selectKey)) {
    	data = sampleData[_this.selectKey];
    }
		
    console.log(data)
    console.log(_this.whereQuery)
    console.log(_this.attributesArr)
    data = _.find(data, _this.whereQuery);
		console.log(data)
  	resolve(data);
  });
};
// ------------------------------------------
// You shouldn't need to edit below this line

var user = new...