2020-01-17 08:23:18 +00:00
|
|
|
const knex = require('../db')
|
|
|
|
|
2020-01-18 00:32:40 +00:00
|
|
|
const insertUser = async (user) => {
|
2020-01-17 08:23:18 +00:00
|
|
|
return await knex('user')
|
2020-01-24 05:25:08 +00:00
|
|
|
.returning(['username', 'email', 'id'])
|
2020-01-17 08:23:18 +00:00
|
|
|
.insert(user)
|
|
|
|
.then(queryResults => {
|
|
|
|
newUser = queryResults[0];
|
|
|
|
return newUser
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const findUserByNameOrEmail = async (user) => {
|
2020-01-18 00:32:40 +00:00
|
|
|
if (!user.email && !user.username) return [];
|
2020-01-17 08:23:18 +00:00
|
|
|
if (!user.email) user.email = '';
|
|
|
|
if (!user.username) user.username = '';
|
|
|
|
|
|
|
|
return await knex('user')
|
|
|
|
.where({'username': user.username})
|
|
|
|
.orWhere({'email': user.email})
|
2020-01-24 05:25:08 +00:00
|
|
|
.select(['username', 'email', 'password', 'id'])
|
2020-01-17 08:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
insertUser,
|
|
|
|
findUserByNameOrEmail
|
|
|
|
}
|