Browse Source

moving files and functions, adding test-data

master
Brandon Nolet 1 year ago
parent
commit
d9865bfede
9 changed files with 334 additions and 49 deletions
  1. +1
    -2
      .gitignore
  2. +17
    -1
      README.md
  3. +25
    -0
      dev/dev.js
  4. +32
    -0
      dev/test-data/categories.json
  5. +26
    -0
      dev/test-data/labels.json
  6. +157
    -0
      dev/test-data/tasks.json
  7. +3
    -1
      package.json
  8. +6
    -45
      src/app.js
  9. +67
    -0
      src/routes/tasks.js

+ 1
- 2
.gitignore View File

@@ -1,2 +1 @@
node_modules/
test-data/
node_modules/

+ 17
- 1
README.md View File

@@ -6,9 +6,25 @@ GeneraList is a simple yet powerful todo list webapp using MongoDB for persisten

npm install

## How to run
## Usage

### Production

To use GeneraList in a production environment, run the following:

```
npm run prod
```

### Development

To run Generalist in a development environment use `npm run dev`

#### Testing

For testing purposes, some `test-data` is provided in the folder named as such. To populate the database for testing and development purposes, use `npm run populate`

You can also drop the database by running `npm run drop-tables`

# Mirror


+ 25
- 0
dev/dev.js View File

@@ -0,0 +1,25 @@
const MongoClient = require('mongodb').MongoClient

const dbUrl = 'mongodb://localhost:27017/generalist'

if (process.env.NODE_ENV === 'populate') {
const fs = require('fs')
MongoClient.connect(dbUrl, {useNewUrlParser: true} , async function(err, db) {
const dbo = await db.db('generalist')
var files = await fs.readdirSync('./dev/test-data/', {withFileTypes: false})
await Promise.all(files.map(async (element) => {
var json = JSON.parse(fs.readFileSync('./dev/test-data/' + element, 'UTF-8'))
dbo.collection(element.substring(0, element.indexOf(".json"))).insertMany(json)
}))
db.close()
})
}

if (process.env.NODE_ENV === 'drop-tables') {
MongoClient.connect(dbUrl, { useNewUrlParser: true},async function(err, db){
const dbo = db.db('generalist')
await dbo.dropDatabase()
console.log("GeneraList db dropped\n\nRun `npm run populate` to populate the database again")
db.close()
})
}

+ 32
- 0
dev/test-data/categories.json View File

@@ -0,0 +1,32 @@
[
{
"parent": null,
"name": "Errands",
"color": "green",
"tasks" : []
},
{
"parent": null,
"name": "Exercise",
"color": "orange",
"tasks": []
},
{
"parent": null,
"name": "shopping",
"color": "red",
"tasks" : []
},
{
"parent": null,
"name": "Projects",
"color": "blue",
"tasks" : []
},
{
"parent": "Projects",
"name": "Essay",
"color": "pink",
"tasks" : []
}
]

+ 26
- 0
dev/test-data/labels.json View File

@@ -0,0 +1,26 @@
[
{
"name": "outdoor",
"color": "green"
},
{
"name": "tomorrow",
"color": "blue"
},
{
"name": "Monday",
"color": "red"
},
{
"name": "indoor",
"color": "pink"
},
{
"name": "repeating",
"color": "yellow"
},
{
"name": "Friday",
"color": "orange"
}
]

+ 157
- 0
dev/test-data/tasks.json View File

@@ -0,0 +1,157 @@
[
{
"task": "Mow the lawn",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "Errands",
"label": ["outdoor", "tomorrow"],
"comment": "ugh",
"dateCreated": 1555463219536,
"milestone1": 1555464553215
},
{
"task": "Take out the garbage",
"inbox": true,
"done": false,
"priority": 1,
"categories_id": "Errands",
"label": ["outdoor", "Monday"],
"comment": "ugh",
"dateCreated": 1555436219536
},
{
"task": "20 pushups",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "Exercise",
"label": ["indoor", "repeating"],
"comment": "I decided to make this a very long comment. This way I can test how text will overflow once I start building the front-end",
"dateCreated": 1555463219536
},
{
"task": "bananas",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "shopping",
"label": ["Friday"],
"comment": "Make me happy",
"dateCreated": 1555463291536
},
{
"task": "pork",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "shopping",
"label": ["Friday"],
"comment": "I'll eat my candy with it",
"dateCreated": 1555433339536
},
{
"task": "beans",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "Errands",
"label": ["Friday"],
"comment": "they make you fart",
"dateCreated": 1555464519536
},
{
"task": "bread",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "shopping",
"label": ["Friday"],
"comment": "brown bread only plz",
"dateCreated": 1555466219536
},
{
"task": "tomatoes",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "shopping",
"label": ["outdoor", "tomorrow"],
"comment": "Get the big red juicy ones",
"dateCreated": 1555463217536
},
{
"task": "finish this app",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "Projects",
"label": ["indoor", "repeating"],
"comment": "This might take a while",
"dateCreated": 1555463619536
},
{
"task": "make blueprint",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "Project",
"label": ["indoor", "repeating"],
"comment": "ugh",
"dateCreated": 1555463215536
},
{
"task": "Call mom",
"inbox": true,
"done": false,
"priority": 1,
"categories_id": "",
"label": ["outdoor", "tomorrow"],
"comment": "ugh",
"dateCreated": 1555463214536
},
{
"task": "",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "Essay",
"label": [""],
"comment": "ugh",
"dateCreated": 1555423219536
},
{
"task": "incomplete ",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "",
"label": ["", ""],
"comment": "",
"dateCreated": 1555483213536
},
{
"task": "no cat no lab",
"inbox": false,
"done": false,
"priority": 1,
"comment": "ugh",
"dateCreated": 1555464519536
},
{
"task": "no comment",
"inbox": false,
"done": false,
"priority": 1,
"categories_id": "Errands",
"label": ["outdoor", "tomorrow"],
"dateCreated": 1555464519536
},
{
"task": "only inbox, done, dateCreated",
"inbox": false,
"done": false,
"dateCreated": 1555456219536
}
]

+ 3
- 1
package.json View File

@@ -5,7 +5,9 @@
"main": "./src/app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "NODE_ENV='dev' node-dev ./src/app.js"
"dev": "NODE_ENV='dev' node-dev ./src/app.js",
"populate": "NODE_ENV='populate' node-dev dev/dev.js",
"drop-tables": "NODE_ENV='drop-tables' node-dev dev/dev.js"
},
"repository": {
"type": "git",


+ 6
- 45
src/app.js View File

@@ -3,7 +3,8 @@ const app = express()
const bodyParser = require('body-parser')
const logger = require('morgan')
const MongoClient = require('mongodb').MongoClient
const singleLine = require('single-line-log').stdout

const tasks = require('./routes/tasks.js')

app.use(bodyParser.json())

@@ -15,23 +16,10 @@ MongoClient.connect(dbUrl, { useNewUrlParser: true}, function(err, db) {
dbo = db.db('generalist')
})

// console.log(Date.now().toString())
app.get('/tasks', tasks.getAll)
app.get('/tasks/query', tasks.getAllByQuery)
app.get('/tasks/:id', tasks.getOneByID)

app.get('/tasks', async (req, res) => {
try {
await dbo.collection('tasks').find({}, (err, result) => {
result.forEach(element => {
res.write(JSON.stringify(element))
})
})
res.send()
} catch (error) {
res.sendStatus(500)
return console.error(error)
} finally {
}
})

app.post('/tasks/', (req, res) => {
dbo.collection('tasks').insertOne(req.body, function(err, res) {
@@ -43,35 +31,8 @@ app.post('/tasks/', (req, res) => {
app.listen(3000, function() {
console.log('Listening on port: ' + this.address().port)
if (process.env.NODE_ENV === 'dev') {
const fs = require('fs')
app.use(logger('dev'))
console.log('Caution: running in development mode')
console.log('If you wish to keep all collections in the GeneraList database, please press Ctrl+C (10 seconds)')
setTimeout(function() {
MongoClient.connect(dbUrl, { useNewUrlParser: true},function(err, db){
const dbo = db.db('generalist')
dbo.dropDatabase()
fs.readdir('./test-data/', (err, files) => {
files.forEach(file => {
fs.readFile('./test-data/' + file, 'UTF-8', (err, data) => {
var json = JSON.parse(data)
json.forEach(element => {
console.log(file.substring(0, file.indexOf(".json")))
dbo.collection(file.substring(0, file.indexOf(".json"))).insertOne(element)
})
})
})

})
})
}, 10020)
var i = 10
setInterval(function() {
if (i > 0 ) {i--; singleLine(i)}
if (i === 0) {
console.log()
clearInterval(this)
}
}, 1000)
}
})

+ 67
- 0
src/routes/tasks.js View File

@@ -0,0 +1,67 @@
const mongo = require('mongodb')
module.exports = {
getAll(req, res) {
try {
var chunk = []
dbo.collection('tasks').find({}, async (err, result) => {
await result.forEach(element => {
chunk.push(element)
})
res.send(chunk)
})
} catch (error) {
res.sendStatus(500)
return console.error(error)
}
},

async getOneByID(req, res) {
try {
var id = require('mongodb').ObjectID(req.params.id)

dbo.collection('tasks').findOne({'_id': id}, (err, result) => {
res.send(result)
})
} catch (error) {
res.sendStatus(500)
return console.error(error)
}
},
async getAllByQuery(req, res) {
try {
var queries = {}
await Object.entries(req.query).map(element => {
if (element[0] === 'inbox' || element[0] === 'done')
if (element[1] === 'true')
queries[element[0]] = true
else if(element[1] === 'false')
queries[element[0]] = false
else return console.error(`${element[1]} is not a valid boolean value, omitting query parameter`)

else if (element[0] === 'priority') {
if (isNaN(element[1])){
return console.log(`${element[1]} is not a number, omitting query parameter`)
} else if (Number(element[1]) > 5)
return console.error(`${element[0]} must be 5 or less`)
queries[element[0]] = Number(element[1])
} else
queries[element[0]] = element[1]
})
console.log(queries)
dbo.collection('tasks').find(queries).toArray(async (err, result) => {
var chunk = []
await result.map(element => {
chunk.push(element)
})
res.send(chunk)
})
} catch (error) {
// res.sendStatus(500)
console.error(error)
}
}
}


Loading…
Cancel
Save