Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes c-common and d-messenger #30

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions JavaScript/c-commonjs/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ const users = db.crud('users');

module.exports = {
async read(id) {
return users.read(id, ['id', 'login']);
return await users.read(id, ['id', 'login']);
},

async create({ login, password }) {
const passwordHash = await common.hash(password);
return users.create({ login, password: passwordHash });
return await users.create({ login, password: passwordHash });
},

async update(id, { login, password }) {
const passwordHash = await common.hash(password);
return users.update(id, { login, password: passwordHash });
return await users.update(id, { login, password: passwordHash });
},

async delete(id) {
return users.delete(id);
return await users.delete(id);
},

async find(mask) {
const sql = 'SELECT login from users where login like $1';
return users.query(sql, [mask]);
return await users.query(sql, [mask]);
},
};
2 changes: 1 addition & 1 deletion JavaScript/c-commonjs/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const crud = (table) => ({
},

async delete(id) {
const sql = 'DELETE FROM ${table} WHERE id = $1';
const sql = `DELETE FROM ${table} WHERE id = $1`;
return pool.query(sql, [id]);
},
});
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/c-commonjs/static/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ const scaffold = (url) => {
say: ['message'],
}
});
const data = await api.talks.say('hello');
const data = await api.user.read();
console.dir({ data });
})();
32 changes: 17 additions & 15 deletions JavaScript/c-commonjs/transport/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ const receiveArgs = async (req) => {
};

module.exports = (routing, port, console) => {
http.createServer(async (req, res) => {
res.writeHead(200, HEADERS);
if (req.method !== 'POST') return void res.end('"Not found"');
const { url, socket } = req;
const [place, name, method] = url.substring(1).split('/');
if (place !== 'api') return void res.end('"Not found"');
const entity = routing[name];
if (!entity) return void res.end('"Not found"');
const handler = entity[method];
if (!handler) return void res.end('"Not found"');
const { args } = await receiveArgs(req);
console.log(`${socket.remoteAddress} ${method} ${url}`);
const result = await handler(args);
res.end(JSON.stringify(result));
}).listen(port);
http
.createServer(async (req, res) => {
res.writeHead(200, HEADERS);
if (req.method !== 'POST') return void res.end('"Not found"');
const { url, socket } = req;
const [place, name, method] = url.substring(1).split('/');
if (place !== 'api') return void res.end('"Not found"');
const entity = routing[name];
if (!entity) return void res.end('"Not found"');
const handler = entity[method];
if (!handler) return void res.end('"Not found"');
const { args } = await receiveArgs(req);
console.log(`${socket.remoteAddress} ${method} ${url}`);
const result = await handler(...args);
res.end(JSON.stringify(result));
})
.listen(port);

console.log(`API on port ${port}`);
};
4 changes: 3 additions & 1 deletion JavaScript/c-commonjs/transport/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ module.exports = (routing, port, console) => {
console.log(`${ip} ${name}.${method}(${parameters})`);
try {
const result = await handler(...args);
connection.send(JSON.stringify(result), { binary: false });
connection.send(
JSON.stringify(result), { binary: false }
);
} catch (err) {
console.error(err);
connection.send('"Server error"', { binary: false });
Expand Down
6 changes: 3 additions & 3 deletions JavaScript/d-messenger/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const crud = (pool) => (table) => ({

async read(id, fields = ['*']) {
const names = fields.join(', ');
const sql = `SELECT ${names} FROM ${table}`;
const sql = `SELECT ${names} FROM "${table}"`;
if (!id) return pool.query(sql);
return pool.query(`${sql} WHERE id = $1`, [id]);
},
Expand Down Expand Up @@ -40,13 +40,13 @@ const crud = (pool) => (table) => ({
updates[i] = `${key} = $${++i}`;
}
const delta = updates.join(', ');
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`;
const sql = `UPDATE "${table}" SET ${delta} WHERE id = $${++i}`;
data.push(id);
return pool.query(sql, data);
},

async delete(id) {
const sql = 'DELETE FROM ${table} WHERE id = $1';
const sql = `DELETE FROM "${table}" WHERE id = $1`;
return pool.query(sql, [id]);
},
});
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/d-messenger/transport/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = (routing, port, console) => {
if (!handler) return void res.end('"Not found"');
const { args } = await receiveArgs(req);
console.log(`${socket.remoteAddress} ${method} ${url}`);
const result = await handler(args);
const result = await handler(...args);
res.end(JSON.stringify(result));
})
.listen(port);
Expand Down