import db from '../db.js';

class UserService {
    async register(username, password) {
        await db.query("INSERT INTO users (username, password) VALUES ($1, $2)", [username, password]);
    }

    async exists(username) {
        return (await db.query("SELECT * FROM users WHERE username = $1", [username])).rowCount > 0;
    }
    
    async getPassword(username) {
        return (await db.query("SELECT password FROM users WHERE username = $1", [username])).rows[0].password;
    }

    async canHaveCloak(username) {
        return (await db.query("SELECT can_have_cloak FROM users WHERE username = $1", [username])).rows[0].can_have_cloak;
    }

    async changePassword(username, newPassword) {
        await db.query("UPDATE users SET password = $1 WHERE username = $2", [newPassword, username]);
    }
};

export default new UserService();