Backend update renamed controllers and models to be singular

This commit is contained in:
Max G
2019-07-30 19:21:12 +00:00
parent 7806a206b2
commit 0d86aa4ff9
11 changed files with 41 additions and 48 deletions

View File

@@ -37,16 +37,16 @@ app.use(function(req, res, next){
app.get(prefix, (req, res) => res.send('The api is running'))
//Init user endpoint
var user = require('@routes/user')
var user = require('@routes/userController')
app.use(prefix+'/user', user)
//Init notes endpoint
var notes = require('@routes/notesController')
app.use(prefix+'/notes', notes)
var notes = require('@routes/noteController')
app.use(prefix+'/note', notes)
//Init tags endpoint
var tags = require('@routes/tagsController')
app.use(prefix+'/tags', tags)
var tags = require('@routes/tagController')
app.use(prefix+'/tag', tags)
//Output running status
app.listen(port, () => console.log(`Listening on port ${port}!`))

View File

@@ -1,12 +1,12 @@
let db = require('@config/database')
let Tags = require('@models/Tags')
let Tags = require('@models/Tag')
let Attachment = require('@models/Attachment')
var rp = require('request-promise');
var SolrNode = require('solr-node');
let Notes = module.exports = {}
let Note = module.exports = {}
// Create client
var client = new SolrNode({
@@ -16,7 +16,7 @@ var client = new SolrNode({
protocol: 'http'
});
Notes.create = (userId, noteText) => {
Note.create = (userId, noteText) => {
return new Promise((resolve, reject) => {
if(userId == null || userId < 10){ reject('User Id required to create note') }
@@ -32,7 +32,7 @@ Notes.create = (userId, noteText) => {
})
}
Notes.update = (userId, noteId, noteText, fancyInput, color) => {
Note.update = (userId, noteId, noteText, fancyInput, color) => {
return new Promise((resolve, reject) => {
const now = Math.round((+new Date)/1000)
@@ -73,7 +73,7 @@ Notes.update = (userId, noteId, noteText, fancyInput, color) => {
})
}
Notes.delete = (userId, noteId) => {
Note.delete = (userId, noteId) => {
return new Promise((resolve, reject) => {
// DELETE FROM notes WHERE notes.id = 290 AND notes.user = 61;
// DELETE FROM attachment WHERE attachment.note_id = 290 AND attachment.user_id = 61;
@@ -92,7 +92,7 @@ Notes.delete = (userId, noteId) => {
})
}
Notes.get = (userId, noteId) => {
Note.get = (userId, noteId) => {
return new Promise((resolve, reject) => {
db.promise()
.query('SELECT text, updated, raw_input, color FROM notes WHERE user = ? AND id = ? LIMIT 1', [userId,noteId])
@@ -103,7 +103,7 @@ Notes.get = (userId, noteId) => {
})
}
Notes.getLatest = (userId) => {
Note.getLatest = (userId) => {
return new Promise((resolve, reject) => {
db.promise()
.query('SELECT id, SUBSTRING(text, 1, 100) as text FROM notes WHERE user = ? ORDER BY updated DESC, created DESC', [userId])
@@ -114,7 +114,7 @@ Notes.getLatest = (userId) => {
})
}
Notes.solrQuery = (userId, searchQuery, searchTags) => {
Note.solrQuery = (userId, searchQuery, searchTags) => {
return new Promise((resolve, reject) => {
if(searchQuery != '' && searchQuery != null){
@@ -140,7 +140,7 @@ Notes.solrQuery = (userId, searchQuery, searchTags) => {
})
}
Notes.search = (userId, searchQuery, searchTags) => {
Note.search = (userId, searchQuery, searchTags) => {
return new Promise((resolve, reject) => {
@@ -152,7 +152,7 @@ Notes.search = (userId, searchQuery, searchTags) => {
}
Notes.solrQuery(userId, searchQuery, searchTags).then( solrResult => {
Note.solrQuery(userId, searchQuery, searchTags).then( solrResult => {
let highlights = solrResult.highlighting

View File

@@ -1,9 +1,9 @@
let db = require('@config/database')
let Tags = module.exports = {}
let Tag = module.exports = {}
Tags.removeTagFromNote = (userId, tagId) => {
Tag.removeTagFromNote = (userId, tagId) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`DELETE FROM notes_tags WHERE id = ? AND user_id = ? LIMIT 1;`, [tagId, userId])
@@ -14,19 +14,19 @@ Tags.removeTagFromNote = (userId, tagId) => {
})
}
Tags.addToNote = (userId, noteId, tagText) => {
Tag.addToNote = (userId, noteId, tagText) => {
return new Promise((resolve, reject) => {
//Lookup tag
Tags.lookup(tagText)
Tag.lookup(tagText)
.then( lookup => {
//Tag does not exist, insert new tag, then associate it with a note
if(lookup.length == 0){
//Insert new tag
Tags.add(tagText)
Tag.add(tagText)
.then( newTagId => {
Tags.associateWithNote(userId, noteId, newTagId)
Tag.associateWithNote(userId, noteId, newTagId)
.then( result => {
resolve(result)
})
@@ -35,7 +35,7 @@ Tags.addToNote = (userId, noteId, tagText) => {
//Tag already exists, associate it with a note
if(lookup.length > 0){
Tags.associateWithNote(userId, noteId, lookup[0].id)
Tag.associateWithNote(userId, noteId, lookup[0].id)
.then( result => {
resolve(result)
})
@@ -45,7 +45,7 @@ Tags.addToNote = (userId, noteId, tagText) => {
})
}
Tags.associateWithNote = (userId, noteId, tagId) => {
Tag.associateWithNote = (userId, noteId, tagId) => {
return new Promise((resolve, reject) => {
//Check if tag already exists on note before adding note
@@ -72,7 +72,7 @@ Tags.associateWithNote = (userId, noteId, tagId) => {
})
}
Tags.add = (tagText) => {
Tag.add = (tagText) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`INSERT INTO tags (text, hash) VALUES (?,?);`, [tagText,0])
@@ -83,7 +83,7 @@ Tags.add = (tagText) => {
})
}
Tags.get = (userId, noteId) => {
Tag.get = (userId, noteId) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`SELECT notes_tags.id, tags.text FROM notes_tags
@@ -96,9 +96,9 @@ Tags.get = (userId, noteId) => {
})
}
Tags.string = (userId, noteId) => {
Tag.string = (userId, noteId) => {
return new Promise((resolve, reject) => {
Tags.get(userId, noteId).then(tagArray => {
Tag.get(userId, noteId).then(tagArray => {
let tagString = ''
tagArray.forEach( (tag, i) => {
@@ -112,7 +112,7 @@ Tags.string = (userId, noteId) => {
})
}
Tags.lookup = (tagText) => {
Tag.lookup = (tagText) => {
return new Promise((resolve, reject) => {
db.promise()
.query(`SELECT * FROM tags WHERE text = ?;`, [tagText])
@@ -124,7 +124,7 @@ Tags.lookup = (tagText) => {
}
//Suggest note tags - don't suggest tags already on note
Tags.suggest = (userId, noteId, tagText) => {
Tag.suggest = (userId, noteId, tagText) => {
tagText += '%'

View File

@@ -1,4 +1,3 @@
var crypto = require('crypto')
let db = require('@config/database')

View File

@@ -1,7 +1,7 @@
var express = require('express')
var router = express.Router()
let Notes = require('@models/Notes');
let Notes = require('@models/Note');
let userId = null
// middleware that is specific to this router
@@ -13,12 +13,6 @@ router.use(function setUserId (req, res, next) {
next()
})
//Get the latest notes the user has created
router.post('/latest', function (req, res) {
Notes.getLatest(userId)
.then( data => res.send(data) )
})
router.post('/get', function (req, res) {
Notes.get(userId, req.body.noteId)
.then( data => res.send(data) )

View File

@@ -1,7 +1,7 @@
var express = require('express')
var router = express.Router()
let Tags = require('@models/Tags');
let Tags = require('@models/Tag');
let userId = null
// middleware that is specific to this router

View File

@@ -1,7 +1,7 @@
var express = require('express')
var router = express.Router()
let User = require('@models/Users');
let User = require('@models/User');
// middleware that is specific to this router
router.use(function timeLog (req, res, next) {