added users and groups
This commit is contained in:
		
							
								
								
									
										34
									
								
								src/services/group.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/services/group.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
import db from '../db.js';
 | 
			
		||||
import status from '../utils/status.js';
 | 
			
		||||
 | 
			
		||||
class GroupService {
 | 
			
		||||
    async create(name, creatorId) {
 | 
			
		||||
        let res = await db.query("INSERT INTO groups (name, admin_id) VALUES ($1, $2) RETURNING ID", [name, creatorId]).catch (e => {
 | 
			
		||||
            if (e.code == 23505) { // already exists
 | 
			
		||||
                return status.duplicate;
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        return res.rows[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getById(id) {
 | 
			
		||||
        let res = (await db.query("SELECT * FROM groups WHERE id = $1", [id]))
 | 
			
		||||
        if (res.rowCount == 0) return status.not_found;
 | 
			
		||||
        return res.rows[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getAdminId(id) {
 | 
			
		||||
        return (await db.query("SELECT admin_id FROM groups WHERE ID = $1", [id])).rows[0].admin_id
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async updatePassword(id, password) {
 | 
			
		||||
        return (await db.query("UPDATE groups SET password = $1 WHERE id = $2", [password, id]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getPassword(id) {
 | 
			
		||||
        return (await db.query("SELECT password FROM groups WHERE id = $1", [id])).rows[0].password;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export default new GroupService();
 | 
			
		||||
							
								
								
									
										36
									
								
								src/services/user.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/services/user.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
import db from '../db.js'
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
import bcrypt from 'bcrypt';
 | 
			
		||||
 | 
			
		||||
class UserService {
 | 
			
		||||
    async create (username, password) {
 | 
			
		||||
        await db.query("INSERT INTO users (username, password) VALUES ($1, $2)", [username, bcrypt.hashSync(password, 12)]).catch (e => {
 | 
			
		||||
            if (e.code == 23505) { // already exists
 | 
			
		||||
                return statuses.duplicate
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        return statuses.ok
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getByUsername(username) {
 | 
			
		||||
        let user = (await db.query("SELECT * FROM Users WHERE username = $1", [username])).rows
 | 
			
		||||
        if (user == undefined) return statuses.not_found
 | 
			
		||||
        return (user[0]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getAll() {
 | 
			
		||||
        return (await db.query("SELECT * FROM Users")).rows;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async isInGroup(userId, groupId) {
 | 
			
		||||
        return (await db.query("SELECT * FROM Users WHERE ID = $1 AND $2 = ANY(groups)", [userId, groupId])).rowCount > 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async joinGroup(userId, groupId) {
 | 
			
		||||
        if (await (this.isInGroup(userId, groupId))) return statuses.duplicate;
 | 
			
		||||
        await db.query("UPDATE Users SET groups = array_append(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
 | 
			
		||||
        return statuses.ok;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default new UserService();
 | 
			
		||||
		Reference in New Issue
	
	Block a user