Add sessions endpoints with queries

This commit is contained in:
2019-08-01 16:25:44 +02:00
parent ccc7a0bfb0
commit 03a9c793d8
12 changed files with 167 additions and 36 deletions

View File

@@ -9,7 +9,7 @@ router.post('/register', (req, res) => {
const hash = bcrypt.hashSync(user.password, 10);
user.password = hash;
Users.add(user)
Users.addUser(user)
.then(saved => {
res.status(201).json(saved);
})
@@ -21,7 +21,7 @@ router.post('/register', (req, res) => {
router.post('/login', (req, res) => {
let { username, password } = req.body;
Users.findBy({ username })
Users.findUserBy({ username })
.first()
.then(user => {
if (user && bcrypt.compareSync(password, user.password)) {

View File

@@ -0,0 +1,39 @@
const db = require('../../data/dbConfig');
module.exports = {
addSession,
findUserSessions,
updateSession,
removeSession
};
async function addSession(session) {
const [id] = await db('sessions')
.insert(session);
return findSessionById(id);
}
function findUserSessions(user_id) {
return db
.from("sessions")
.where({ user_id });
}
function findSessionById(id) {
return db('sessions')
.where({ id })
.first();
}
async function updateSession(id, changes) {
await db('sessions')
.where({ id })
.update(changes, '*');
return findSessionById(id);
}
async function removeSession(id) {
await db('sessions')
.where({ id })
.del();
}

View File

@@ -0,0 +1,43 @@
const router = require('express').Router();
const { authenticate } = require('../auth/authenticate');
const Sessions = require('../sessions/sessionsModel');
router.post('/sessions', authenticate, (req, res) => {
Sessions.addSession(req.body)
.then(response => {
res.status(200).json(response);
})
.catch(err => res.send(err));
});
router.get('/:id/sessions', authenticate, (req, res) => {
Sessions.findUserSessions(req.params.id)
.then(users => {
res.status(200).json(users);
})
.catch(err => res.send(err));
});
router.put('/sessions/:id', authenticate, async (req, res) => {
Sessions.updateSession(req.params.id, req.body)
.then(session => {
if (session) {
res.status(200).json(session);
} else {
res.status(404).json({ message: 'The session could not be found' });
}
})
.catch(err => {
res.status(500).json(err);
});
});
router.delete("/sessions/:id", authenticate, async (req, res) => {
Sessions.removeSession(req.params.id)
.then(() => res.status(200).json({ message: 'The session has been successfully deleted' }))
.catch(err => res.json(err));
});
module.exports = router;

View File

@@ -1,39 +1,39 @@
const db = require('../../data/dbConfig');
module.exports = {
add,
findBy,
findById,
update,
remove
addUser,
findUserBy,
findUserById,
updateUser,
removeUser
};
function findBy(filter) {
function findUserBy(filter) {
return db('users')
.where(filter);
}
async function add(user) {
async function addUser(user) {
const [id] = await db('users')
.insert(user);
return findById(id);
return findUserById(id);
}
function findById(id) {
function findUserById(id) {
return db('users')
.where({ id })
.first()
.select('id', 'email', 'username');
}
async function update(id, changes) {
async function updateUser(id, changes) {
await db('users')
.where({ id })
.update(changes, '*');
return findById(id);
return findUserById(id);
}
function remove(id) {
function removeUser(id) {
return db('users')
.where({ id })
.del();

View File

@@ -4,7 +4,7 @@ const { authenticate } = require('../auth/authenticate');
const Users = require('../users/usersModel');
router.get('/:id', authenticate, (req, res) => {
Users.findById(req.params.id)
Users.findUserById(req.params.id)
.then(users => {
res.status(200).json(users);
})
@@ -12,8 +12,8 @@ router.get('/:id', authenticate, (req, res) => {
});
router.put('/:id', authenticate, async (req, res) => {
Users.update(req.params.id, req.body)
.then( user => {
Users.updateUser(req.params.id, req.body)
.then(user => {
if (user) {
res.status(200).json(user);
} else {
@@ -24,7 +24,7 @@ router.put('/:id', authenticate, async (req, res) => {
});
router.delete("/:id", (req, res) => {
Users.remove(req.params.id)
Users.removeUser(req.params.id)
.then(data => res.status(200).json(data))
.catch(err => res.json(err));
});