generated from azures04/Base-REST-API
Renamed licenceRepository.js to licenseRepository.js and updated its API to use status instead of userId for licenses. Moved permission checking logic from userRepository to permissionsRepository. Added new service layers for license, permissions, and user management, implementing error handling and business logic. Removed the old register.js service and cleaned up test.js. Updated database schema to remove userId from licenses.
68 lines
1.5 KiB
JavaScript
68 lines
1.5 KiB
JavaScript
const database = require("../modules/database")
|
|
|
|
function register(firstName = null, lastName = null, username, hashedPassword) {
|
|
const query = `
|
|
INSERT INTO accounts (
|
|
firstName,
|
|
lastName,
|
|
username,
|
|
password
|
|
)
|
|
|
|
VALUES (
|
|
?,
|
|
?,
|
|
?,
|
|
?
|
|
)
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(firstName, lastName, username, hashedPassword)
|
|
}
|
|
|
|
function remove(id) {
|
|
const query = `
|
|
DELETE FROM accounts
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(id)
|
|
}
|
|
|
|
function changePassword(id, hashedPassword) {
|
|
const query = `
|
|
UPDATE accounts
|
|
SET password = ?
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.run(id, hashedPassword)
|
|
}
|
|
|
|
function findByUsername(identifier) {
|
|
const query = `
|
|
SELECT id, username, password
|
|
FROM accounts
|
|
WHERE username = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.get(identifier)
|
|
}
|
|
|
|
function findById(identifier) {
|
|
const query = `
|
|
SELECT id, firstName, lastName, username, createdAt
|
|
FROM accounts
|
|
WHERE id = ?
|
|
`
|
|
const statement = database.prepare(query)
|
|
return statement.get(identifier)
|
|
}
|
|
|
|
module.exports = {
|
|
remove,
|
|
register,
|
|
findById,
|
|
findByUsername,
|
|
changePassword,
|
|
} |