-
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
1 parent
861d3f7
commit b356f0a
Showing
15 changed files
with
3,477 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
const db = require('../database'); | ||
|
||
module.exports = async (id) => { | ||
let image; | ||
async function getImage(id) { | ||
return !isNaN(parseInt(id)) ? db('images').where({ id }).first() : null; | ||
} | ||
|
||
if (id) { | ||
image = await db('images').where({ id }).first(); | ||
} else { | ||
const { rows } = await db.raw('SELECT * FROM images TABLESAMPLE SYSTEM_ROWS(1)'); | ||
image = rows[0]; | ||
} | ||
async function getImages(currentPage, sort) { | ||
return db('images').orderBy('created_at', sort).paginate({ currentPage }); | ||
} | ||
|
||
return image; | ||
async function getRandomImage() { | ||
const { rows } = await db.raw('SELECT * FROM images TABLESAMPLE SYSTEM_ROWS(1)'); | ||
return rows[0]; | ||
} | ||
|
||
async function getLatestImage() { | ||
return db('images').whereNotNull('taken_at').orderBy('taken_at', 'desc').first(); | ||
} | ||
|
||
async function getLatestImages(currentPage, sort) { | ||
return db('images').whereNotNull('taken_at').orderBy('taken_at', sort).paginate({ currentPage }); | ||
} | ||
|
||
module.exports = { | ||
getImage, | ||
getImages, | ||
getLatestImage, | ||
getLatestImages, | ||
getRandomImage, | ||
} |
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
const getImage = require('./get-image'); | ||
const { getImage, getImages, getLatestImage, getLatestImages, getRandomImage } = require('./get-image'); | ||
const putImage = require('./put-image'); | ||
|
||
module.exports = { | ||
getImage, | ||
getImages, | ||
getLatestImage, | ||
getLatestImages, | ||
getRandomImage, | ||
putImage, | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
const environment = process.env.ENVIRONMENT || 'development'; | ||
const config = require('../knexfile')[environment]; | ||
const knex = require('knex')(config); | ||
const { attachPaginate } = require('knex-paginate'); | ||
attachPaginate(); | ||
|
||
module.exports = require('knex')(config); | ||
module.exports = knex; |
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 @@ | ||
|
||
exports.up = (knex) => { | ||
return knex.schema.createTable('users', (table) => { | ||
table.increments(); | ||
table.string('email').notNullable(); | ||
table.string('password').notNullable(); | ||
table.string('token'); | ||
table.timestamp('created_at').defaultTo(knex.fn.now()); | ||
table.timestamp('updated_at').defaultTo(knex.fn.now()); | ||
}) | ||
}; | ||
|
||
exports.down = (knex) => { | ||
return knex.schema.dropTable('users'); | ||
}; |
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,14 @@ | ||
require('dotenv').config(); | ||
|
||
const { getHash } = require('../../util/password'); | ||
|
||
exports.seed = async (knex) => { | ||
// Deletes ALL existing entries | ||
await knex('users').del(); | ||
|
||
// Inserts seed entries | ||
await knex('users').insert([ | ||
{ id: 1, email: '[email protected]', password: getHash('yeet') }, | ||
]); | ||
|
||
}; |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
module.exports = client => { | ||
module.exports = () => { | ||
console.log('Logged in to Discord'); | ||
console.log(client); | ||
} |
This file was deleted.
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 |
---|---|---|
@@ -1,38 +1,51 @@ | ||
require('dotenv').config(); | ||
|
||
const express = require('express'); | ||
const { ENVIRONMENT_DOMAIN, DISCORD_TOKEN, PORT, SECRET_TOKEN } = process.env; | ||
|
||
const bodyParser = require('body-parser'); | ||
const cors = require('cors'); | ||
const Discord = require('discord.js'); | ||
const { getRandomImageId, getImageName } = require('./get-image'); | ||
const { DISCORD_TOKEN, PORT } = process.env; | ||
const client = new Discord.Client(); | ||
const app = express(); | ||
const express = require('express'); | ||
const expressSession = require('express-session')({ | ||
secret: SECRET_TOKEN, | ||
cookie: { | ||
maxAge: 24 * 60 * 60 * 1000 | ||
}, | ||
resave: false, | ||
saveUninitialized: false, | ||
unset: 'destroy', | ||
}); | ||
const morgan = require('morgan'); | ||
const passport = require('./util/passport'); | ||
const router = require('./routes'); | ||
const db = require('./database'); | ||
|
||
const { discordMessage, discordReady } = require('./events'); | ||
|
||
// Register Discord events | ||
client.on('message', discordMessage); | ||
client.on('ready', discordReady); | ||
|
||
// Login to Discord client | ||
client.login(DISCORD_TOKEN); | ||
const app = express(); | ||
const corsOptions = { | ||
origin: ENVIRONMENT_DOMAIN, | ||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], | ||
credentials: true, | ||
exposedHeaders: ['set-cookie'], | ||
}; | ||
|
||
app.use(morgan('dev')); | ||
app.use(expressSession); | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
app.use(cors(corsOptions)); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(router); | ||
// app.get('/', (req, res) => { | ||
// const id = getRandomImageId(); | ||
// res.redirect(`/${id}`); | ||
// }); | ||
// | ||
// app.get('/:id', (req, res) => { | ||
// const image = getImageName(req.params.id); | ||
// if (image) { | ||
// res.sendFile(`${__dirname}/images/${image}`); | ||
// } else { | ||
// res.sendStatus(404); | ||
// } | ||
// }); | ||
|
||
app.listen(PORT || 6969, () => { | ||
console.log(`Example app listening at http://localhost:${PORT}`) | ||
console.log(`App started. Listening on http://localhost:${PORT}`); | ||
}); | ||
|
||
const client = new Discord.Client(); | ||
|
||
// Register Discord events | ||
client.on('message', discordMessage); | ||
client.on('ready', discordReady); | ||
|
||
// Login to Discord client | ||
// client.login(DISCORD_TOKEN); |
Oops, something went wrong.