51 lines
1.9 KiB
JavaScript
51 lines
1.9 KiB
JavaScript
import {executarSQL} from "../config/database.js";
|
|
|
|
export async function findAll() {
|
|
return executarSQL(`select usuario.id, usuario.nome, usuario.email, perfil.nome
|
|
from usuario inner join perfil on
|
|
usuario.perfil_id = perfil.id
|
|
where usuario.ativo = true`)
|
|
}
|
|
|
|
export async function findById(id) {
|
|
return executarSQL(`select usuario.id, usuario.nome, usuario.email, perfil.nome
|
|
from usuario inner join perfil on
|
|
usuario.perfil_id = perfil.id
|
|
where usuario.ativo = true and usuario.id = ?`,
|
|
[id])
|
|
}
|
|
|
|
export async function findByEmail(email) {
|
|
return executarSQL(`select usuario.id, usuario.nome, usuario.email, perfil.nome
|
|
from usuario inner join perfil on
|
|
usuario.perfil_id = perfil.id
|
|
where usuario.ativo = true and usuario.email = ?`,
|
|
[email])
|
|
}
|
|
|
|
export async function create(usuario){
|
|
const resultado = await executarSQL(`insert into usuario
|
|
(nome, email, senha, perfil_id)
|
|
values (?,?,?,?)`,
|
|
[usuario.nome,usuario.email,usuario.senha,
|
|
usuario.perfil_id]);
|
|
return findById(resultado.insertId);
|
|
}
|
|
|
|
export async function update(id, usuario){
|
|
const resultado = await executarSQL(`update usuario set nome = ?, email = ?,
|
|
senha = ?, perfil_id = ? where id = ?`,
|
|
[usuario.nome,usuario.email,usuario.senha,
|
|
usuario.perfil_id, id]);
|
|
return findById(resultado.insertId);
|
|
}
|
|
|
|
export async function deactivate(id){
|
|
await executarSQL(`update usuario set usuario.ativo = false where id = ?`,
|
|
[id]);
|
|
return findById(id);
|
|
}
|
|
|
|
|
|
|