Exxxx

by evgkch

HTML

<div id="root">

</div>

CSS

body {
  background-color: black;
}

#root {
  display: flex;
  flex-direction: column;  
}

.question {
  display: flex;
  flex-direction: column;
  margin: 5px;
  padding: 5px;
  background-color: aliceblue;
  width: 150px;
  border-radius: 3px;
}

JavaScript

// Иммитация бэка
class Message {	
	constructor(body, owner){
  	this.id = (new Date()).getTime();
  	this.body = body;
    this.owner = owner;    
  }
}

class Data {
	constructor(){
  	this.data = [];
  }
  create(message){
  	const msg = new Message(message);
  	this.data.push(msg);
    return msg.id;
  }
  get(id){
  	return this.data.find(msg => msg.id == id);
  }
  put(id, body){
  	const targetMsg = this.get(id);
    if (targetMsg)
		{
   		targetMsg.body = body;
      return true;
    }
    return false;
  }
  delete(id){
  	let success = false;
  	this.data = this.data.filter(msg => {
    	if (msg.id != id)
      {
      	success = true;
        return false;
      }
      else
      	return true;
    });
    return success;
  }
}

class CustomError {
	constructor(errorCode, message){
  	this.errorCode = errorCode;
    this.message = message;
  }
}

const data = new Data;

// Иммитация роутинга
const _questions = {};

_questions.get = (message, callback) => {
	if (typeof(message.id) == 'number')
  {
    const res = data.get(message.id);
    if (res)
      callback(200, res);
    else
      callback(400, { Error: 'Cannot get data with id = '+id });
  }
	else
  	callback(403, { Error: 'Id must be a number' });
}

_questions.post = (message, callback) => {
	if (message && typeof(message.body) == 'string' && typeof(message.owner) == 'string')
  {
    const msgId = data.create(message);
    callback(200, msgId);
  }
	else
  	callback(403, { Error: 'Body and owner are required fields' });
}

_questions.put = (message, callback) => {
	if (typeof(message.id) == 'number' && typeof(message.body) == 'string')
  {
    const res = data.put(message.id, message.body);
    if (res)
      callback(200, res);
    else
      callback(400, { Error: 'Cannot put data with id = '+id });
  }
	else
  	callback(403, { Error: 'Id must be a number, body is required' });
}

_questions.delete = (message, callback) => {
	const res = data.delete(message.id);
  if (res)
  	callback(200,...