Skip to content

Commit

Permalink
added async to methods in api and await in return for async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RohovAlex committed Aug 1, 2023
1 parent 3557121 commit 2641c0d
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 114 deletions.
12 changes: 6 additions & 6 deletions JavaScript/2-controller/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ const pool = new pg.Pool({

const routing = {
user: {
get(id) {
async get(id) {
if (!id) return pool.query('SELECT id, login FROM users');
const sql = 'SELECT id, login FROM users WHERE id = $1';
return pool.query(sql, [id]);
return await pool.query(sql, [id]);
},

async post({ login, password }) {
const sql = 'INSERT INTO users (login, password) VALUES ($1, $2)';
const passwordHash = await hash(password);
return pool.query(sql, [login, passwordHash]);
return await pool.query(sql, [login, passwordHash]);
},

async put(id, { login, password }) {
const sql = 'UPDATE users SET login = $1, password = $2 WHERE id = $3';
const passwordHash = await hash(password);
return pool.query(sql, [login, passwordHash, id]);
return await pool.query(sql, [login, passwordHash, id]);
},

delete(id) {
async delete(id) {
const sql = 'DELETE FROM users WHERE id = $1';
return pool.query(sql, [id]);
return await pool.query(sql, [id]);
},
},
};
Expand Down
16 changes: 8 additions & 8 deletions JavaScript/3-crud/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const pool = new pg.Pool({
});

module.exports = (table) => ({
query(sql, args) {
return pool.query(sql, args);
async query(sql, args) {
return await pool.query(sql, args);
},

read(id, fields = ['*']) {
async read(id, fields = ['*']) {
const names = fields.join(', ');
const sql = `SELECT ${names} FROM ${table}`;
if (!id) return pool.query(sql);
return pool.query(`${sql} WHERE id = $1`, [id]);
return await pool.query(`${sql} WHERE id = $1`, [id]);
},

async create({ ...record }) {
Expand All @@ -34,7 +34,7 @@ module.exports = (table) => ({
const fields = '"' + keys.join('", "') + '"';
const params = nums.join(', ');
const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`;
return pool.query(sql, data);
return await pool.query(sql, data);
},

async update(id, { ...record }) {
Expand All @@ -49,11 +49,11 @@ module.exports = (table) => ({
const delta = updates.join(', ');
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`;
data.push(id);
return pool.query(sql, data);
return await pool.query(sql, data);
},

delete(id) {
async delete(id) {
const sql = `DELETE FROM ${table} WHERE id = $1`;
return pool.query(sql, [id]);
return await pool.query(sql, [id]);
},
});
16 changes: 8 additions & 8 deletions JavaScript/4-service/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const pool = new pg.Pool({
});

module.exports = (table) => ({
query(sql, args) {
return pool.query(sql, args);
async query(sql, args) {
return await pool.query(sql, args);
},

read(id, fields = ['*']) {
async read(id, fields = ['*']) {
const names = fields.join(', ');
const sql = `SELECT ${names} FROM ${table}`;
if (!id) return pool.query(sql);
return pool.query(`${sql} WHERE id = $1`, [id]);
return await pool.query(`${sql} WHERE id = $1`, [id]);
},

async create({ ...record }) {
Expand All @@ -34,7 +34,7 @@ module.exports = (table) => ({
const fields = '"' + keys.join('", "') + '"';
const params = nums.join(', ');
const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`;
return pool.query(sql, data);
return await pool.query(sql, data);
},

async update(id, { ...record }) {
Expand All @@ -49,11 +49,11 @@ module.exports = (table) => ({
const delta = updates.join(', ');
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`;
data.push(id);
return pool.query(sql, data);
return await pool.query(sql, data);
},

delete(id) {
async delete(id) {
const sql = `DELETE FROM ${table} WHERE id = $1`;
return pool.query(sql, [id]);
return await pool.query(sql, [id]);
},
});
12 changes: 6 additions & 6 deletions JavaScript/4-service/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ const hash = require('./hash.js');
const users = db('users');

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

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

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

delete(id) {
return users.delete(id);
async delete(id) {
return await users.delete(id);
},
};
16 changes: 8 additions & 8 deletions JavaScript/5-agnostic/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const pool = new pg.Pool({
});

module.exports = (table) => ({
query(sql, args) {
return pool.query(sql, args);
async query(sql, args) {
return await pool.query(sql, args);
},

read(id, fields = ['*']) {
async read(id, fields = ['*']) {
const names = fields.join(', ');
const sql = `SELECT ${names} FROM ${table}`;
if (!id) return pool.query(sql);
return pool.query(`${sql} WHERE id = $1`, [id]);
return await pool.query(`${sql} WHERE id = $1`, [id]);
},

async create({ ...record }) {
Expand All @@ -34,7 +34,7 @@ module.exports = (table) => ({
const fields = '"' + keys.join('", "') + '"';
const params = nums.join(', ');
const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`;
return pool.query(sql, data);
return await pool.query(sql, data);
},

async update(id, { ...record }) {
Expand All @@ -49,11 +49,11 @@ module.exports = (table) => ({
const delta = updates.join(', ');
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`;
data.push(id);
return pool.query(sql, data);
return await pool.query(sql, data);
},

delete(id) {
async delete(id) {
const sql = `DELETE FROM ${table} WHERE id = $1`;
return pool.query(sql, [id]);
return await pool.query(sql, [id]);
},
});
12 changes: 6 additions & 6 deletions JavaScript/5-agnostic/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ const hash = require('./hash.js');
const users = db('users');

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

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

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

delete(id) {
return users.delete(id);
async delete(id) {
return await users.delete(id);
},
};
16 changes: 8 additions & 8 deletions JavaScript/6-ws/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const pool = new pg.Pool({
});

module.exports = (table) => ({
query(sql, args) {
return pool.query(sql, args);
async query(sql, args) {
return await pool.query(sql, args);
},

read(id, fields = ['*']) {
async read(id, fields = ['*']) {
const names = fields.join(', ');
const sql = `SELECT ${names} FROM ${table}`;
if (!id) return pool.query(sql);
return pool.query(`${sql} WHERE id = $1`, [id]);
return await pool.query(`${sql} WHERE id = $1`, [id]);
},

async create({ ...record }) {
Expand All @@ -34,7 +34,7 @@ module.exports = (table) => ({
const fields = '"' + keys.join('", "') + '"';
const params = nums.join(', ');
const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`;
return pool.query(sql, data);
return await pool.query(sql, data);
},

async update(id, { ...record }) {
Expand All @@ -49,11 +49,11 @@ module.exports = (table) => ({
const delta = updates.join(', ');
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`;
data.push(id);
return pool.query(sql, data);
return await pool.query(sql, data);
},

delete(id) {
async delete(id) {
const sql = `DELETE FROM ${table} WHERE id = $1`;
return pool.query(sql, [id]);
return await pool.query(sql, [id]);
},
});
16 changes: 8 additions & 8 deletions JavaScript/6-ws/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ const hash = require('./hash.js');
const users = db('users');

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

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

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

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

find(mask) {
async find(mask) {
const sql = 'SELECT login from users where login like $1';
return users.query(sql, [mask]);
return await users.query(sql, [mask]);
},
};
16 changes: 8 additions & 8 deletions JavaScript/7-fs/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ const hash = require('../hash.js');
const users = db('users');

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

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

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

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

find(mask) {
async find(mask) {
const sql = 'SELECT login from users where login like $1';
return users.query(sql, [mask]);
return await users.query(sql, [mask]);
},
};
16 changes: 8 additions & 8 deletions JavaScript/7-fs/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const pool = new pg.Pool({
});

module.exports = (table) => ({
query(sql, args) {
return pool.query(sql, args);
async query(sql, args) {
return await pool.query(sql, args);
},

read(id, fields = ['*']) {
async read(id, fields = ['*']) {
const names = fields.join(', ');
const sql = `SELECT ${names} FROM ${table}`;
if (!id) return pool.query(sql);
return pool.query(`${sql} WHERE id = $1`, [id]);
return await pool.query(`${sql} WHERE id = $1`, [id]);
},

async create({ ...record }) {
Expand All @@ -34,7 +34,7 @@ module.exports = (table) => ({
const fields = '"' + keys.join('", "') + '"';
const params = nums.join(', ');
const sql = `INSERT INTO "${table}" (${fields}) VALUES (${params})`;
return pool.query(sql, data);
return await pool.query(sql, data);
},

async update(id, { ...record }) {
Expand All @@ -49,11 +49,11 @@ module.exports = (table) => ({
const delta = updates.join(', ');
const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`;
data.push(id);
return pool.query(sql, data);
return await pool.query(sql, data);
},

delete(id) {
async delete(id) {
const sql = `DELETE FROM ${table} WHERE id = $1`;
return pool.query(sql, [id]);
return await pool.query(sql, [id]);
},
});
8 changes: 4 additions & 4 deletions JavaScript/8-vm/api/country.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const country = db('country');

({
read(id) {
async read(id) {
console.log({ db });
return country.read(id);
return await country.read(id);
},

find(mask) {
async find(mask) {
const sql = 'SELECT * from country where name like $1';
return country.query(sql, [mask]);
return await country.query(sql, [mask]);
},
});
Loading

0 comments on commit 2641c0d

Please sign in to comment.