1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 00:25:17 +00:00

Can update following state of a conversation

This commit is contained in:
2019-11-30 12:24:19 +01:00
parent bd19411a48
commit 4406b8c882
5 changed files with 119 additions and 1 deletions

View File

@ -146,6 +146,26 @@ export class ConversationsHelper {
}) == 1;
}
/**
* Update following state of the conversation
*
* @param userID User to update
* @param convID Target conversation ID
* @param following New status
*/
public static async SetFollowing(userID: number, convID: number, following: boolean) {
await DatabaseHelper.UpdateRows({
table: USERS_TABLE,
set: {
"following": following ? 1 : 0
},
where: {
"conv_id": convID,
"user_id": userID
}
});
}
/**
* Get the list of members of a conversation
*

View File

@ -23,6 +23,12 @@ export interface QueryInformation {
limit ?: number,
}
export interface UpdateInformation {
table: string,
set: Object,
where ?: Object,
}
export interface CountQueryInformation {
table: string,
where ?: Object
@ -177,6 +183,66 @@ export class DatabaseHelper {
});
}
/**
* Perform update on the database
*
* @param info Information about the request
* @returns The number of affected rows
*/
static async UpdateRows(info : UpdateInformation) : Promise<number> {
let sql = "UPDATE " + info.table + " SET ";
let args = [];
// Process updates
let isFirst = true;
for (const key in info.set) {
if (info.set.hasOwnProperty(key)) {
const value = info.set[key];
if(!isFirst)
sql += ", ";
else
isFirst = false;
sql += key + " = ? "
args.push(value);
}
}
// Process conditions
isFirst = true;
if(info.where) {
sql += " WHERE ";
for (const key in info.where) {
if (info.where.hasOwnProperty(key)) {
const value = info.where[key];
if(!isFirst)
sql += " AND ";
else
isFirst = false;
sql += key + " = ? "
args.push(value);
}
}
}
else
throw Error("Error : Updates without conditions are blocked for security!");
// Execute request
return await new Promise((resolve, reject) => {
this.connection.query(sql, args, (err, results, f) => {
if(err){
reject(err);
return;
}
resolve(results.affectedRows);
})
});
}
/**
* Delete entries from a table
*