-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
200 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,5 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
config.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const config = { | ||
db: { | ||
host: "127.0.0.1", | ||
user: "username", | ||
password: "hunter2", | ||
database: "ircbot", | ||
connectTimeout: 60000 | ||
}, | ||
listPerPage: 100, | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function getOffset(currentPage = 1, listPerPage) { | ||
return (currentPage - 1) * [listPerPage]; | ||
} | ||
|
||
function emptyOrRows(rows) { | ||
if (!rows) { | ||
return []; | ||
} | ||
return rows; | ||
} | ||
|
||
module.exports = { | ||
getOffset, | ||
emptyOrRows | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
"supertest": "^6.3.4" | ||
}, | ||
"dependencies": { | ||
"express": "^4.19.2" | ||
"express": "^4.19.2", | ||
"mysql2": "^3.11.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const users = require('../services/users'); | ||
|
||
/* GET users */ | ||
router.get('/', async function(req, res, next) { | ||
try { | ||
res.json(await users.getMultiple(req.query.page)); | ||
} catch (err) { | ||
console.error(`Error while getting users `, err.message); | ||
next(err); | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const mysql = require('mysql2/promise'); | ||
const config = require('../config'); | ||
|
||
async function query(sql, params) { | ||
const connection = await mysql.createConnection(config.db); | ||
const [results, ] = await connection.execute(sql, params); | ||
|
||
return results; | ||
} | ||
|
||
module.exports = { | ||
query | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const db = require('./db'); | ||
const helper = require('../helper'); | ||
const config = require('../config'); | ||
|
||
async function getMultiple(page = 1){ | ||
const offset = helper.getOffset(page, config.listPerPage); | ||
const rows = await db.query( | ||
`SELECT userId, nickname | ||
FROM user LIMIT ${offset},${config.listPerPage}` | ||
); | ||
const data = helper.emptyOrRows(rows); | ||
const meta = {page}; | ||
|
||
return { | ||
data, | ||
meta | ||
} | ||
} | ||
|
||
module.exports = { | ||
getMultiple | ||
} |