Skip to content

Commit

Permalink
add models
Browse files Browse the repository at this point in the history
  • Loading branch information
mudroljub committed Mar 12, 2018
1 parent 19ad4e0 commit 11cede8
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,7 @@
npm-debug.log
.DS_Store
/*.env

yarn-debug.log*
yarn-error.log*
package-lock.json
6 changes: 6 additions & 0 deletions index.js
@@ -1,6 +1,9 @@
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')

const mongoUri = require('./config').mongoUri
const router = require('./routes/router')

const port = process.env.PORT || 5000
Expand All @@ -12,6 +15,9 @@ app.use(cors())
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

mongoose.connect(mongoUri)
mongoose.Promise = global.Promise

/* ROUTES */

app.get('/', (req, res) => res.send('Baza podataka u izgradnji.'))
Expand Down
13 changes: 13 additions & 0 deletions models/Author.js
@@ -0,0 +1,13 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema

const authorSchema = Schema({
name: {
type: String,
required: true,
trim: true,
unique: true
}
})

module.exports = mongoose.model('Author', authorSchema)
39 changes: 39 additions & 0 deletions models/Quote.js
@@ -0,0 +1,39 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema

const quotesSchema = Schema({
author: {
type: String, // Schema.Types.ObjectId,
ref: 'Author',
required: true
},
en: {
type: String,
sparse: true,
unique: true,
trim: true
},
sr: {
type: String,
trim: true,
sparse: true,
unique: true,
},
source: {
type: String,
trim: true
},
numberOfVotes: {
type: Number,
min: 0,
default: 0
},
rating: {
type: Number,
min: 0,
max: 5,
default: 0
}
})

module.exports = mongoose.model('Quote', quotesSchema)
103 changes: 103 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -20,6 +20,7 @@
"cors": "^2.8.4",
"express": "^4.16.2",
"mongodb": "^2.2.34",
"mongoose": "^5.0.9",
"ws": "^3.3.3"
},
"engines": {
Expand Down
18 changes: 9 additions & 9 deletions routes/quotes/create.js
@@ -1,18 +1,18 @@
const mongodb = require('mongodb')
const mongoUri = require('../../config.js').mongoUri
const Quote = require('../../models/Quote')
const lozinka = process.env.LOZINKA

module.exports = (req, res) => {
const {sr, author, source, en, password} = req.body
module.exports = (req, res) => {
const {en, sr, author, source, password} = req.body
const condition = (en || sr) && author
if (!condition) return res.send('ARGUMENTS_ERROR')
if (password !== lozinka) return res.send('LOGIN_REQUIRED')

mongodb.MongoClient.connect(mongoUri, (err, db) => {
if(err) throw err
db.collection('quotes').insert(
{sr, author, source, en, rating: 0, numberOfVotes: 0}
)
Quote.find({$or: [{sr}, {en}]}, (err, results) => {
if (results.length) return res.send('ALREADY_EXISTS')
})

Quote.create({en, sr, author, source}, (err, quote) => {
if (err) return console.error(err)
res.send('SUCCESS_SAVED')
})
}
11 changes: 4 additions & 7 deletions routes/quotes/delete.js
@@ -1,15 +1,12 @@
const mongodb = require('mongodb')
const mongoUri = require('../../config.js').mongoUri
const Quote = require('../../models/Quote')
const lozinka = process.env.LOZINKA

module.exports = (req, res) => {
const {_id, password} = req.body
if (password !== lozinka) return res.send('LOGIN_REQUIRED')

mongodb.MongoClient.connect(mongoUri, (err, db) => {
db.collection('quotes').deleteOne({'_id': new mongodb.ObjectId(_id)}, (err, obj) => {
if (err) throw err
if (obj.deletedCount) res.send('QUOTE_DELETED')
})
Quote.findOneAndRemove({_id}, (err) => {
if (err) throw err
res.send('QUOTE_DELETED')
})
}
33 changes: 12 additions & 21 deletions routes/quotes/rate.js
@@ -1,30 +1,21 @@
const mongodb = require('mongodb')
const ObjectId = require('mongodb').ObjectId
const mongoUri = require('../../config.js').mongoUri
const Quote = require('../../models/Quote')

module.exports = (req, res) => {
const _id = req.body._id
const newRating = Number(req.body.newRating)
if (!_id || !newRating) return res.send('ARGUMENTS_ERROR')

mongodb.MongoClient.connect(mongoUri, (err, db) => {
if(err) throw err

db.collection('quotes').findOne({_id: new ObjectId(_id)}, (err, quote) => {
if (err) throw err
const {numberOfVotes, rating} = quote
const newAverage = (numberOfVotes * rating + newRating) / (numberOfVotes + 1)
db.collection('quotes').update(
{_id: new ObjectId(_id)},
{
$set: {
rating: newAverage.toFixed(1),
numberOfVotes: numberOfVotes + 1
}
}
)
res.send(newAverage.toFixed(1))
db.close()
Quote.findById(_id, (err, quote) => {
if (err) return console.error(err)
const {numberOfVotes, rating} = quote
const newAverage = ((numberOfVotes * rating + newRating) / (numberOfVotes + 1)).toFixed(1)
quote.set({
rating: newAverage,
numberOfVotes: numberOfVotes + 1
})
quote.save(err => {
if (err) return console.error(err)
res.send(newAverage)
})
})
}
13 changes: 5 additions & 8 deletions routes/quotes/read.js
@@ -1,11 +1,8 @@
const mongodb = require('mongodb')
const mongoUri = require('../../config.js').mongoUri
const Quote = require('../../models/Quote')

module.exports = (req, res) => {
mongodb.MongoClient.connect(mongoUri, (err, db) => {
if (err) throw err
db.collection('quotes')
.find()
.toArray((err, data) => res.send(data.sort(() => .5 - Math.random())))
})
Quote
.find()
.then(quotes => res.send(quotes.sort(() => .5 - Math.random())))
.catch(e => res.send('SERVER_ERROR'))
}
20 changes: 9 additions & 11 deletions routes/quotes/update.js
@@ -1,20 +1,18 @@
const mongodb = require('mongodb')
const mongoUri = require('../../config.js').mongoUri
const ObjectId = require('mongodb').ObjectId
const Quote = require('../../models/Quote')
const lozinka = process.env.LOZINKA

module.exports = (req, res) => {
const {_id, sr, author, source, en, password} = req.body
const {_id, en, sr, author, source, password} = req.body
const condition = (en || sr) && author
if (!condition) return res.send('ARGUMENTS_ERROR')
if (password !== lozinka) return res.send('LOGIN_REQUIRED')

mongodb.MongoClient.connect(mongoUri, (err, db) => {
if(err) throw err
db.collection('quotes').update(
{_id: new ObjectId(_id)},
{$set: {sr, author, source, en}}
)
res.send('SUCCESS_SAVED')
Quote.findById(_id, (err, quote) => {
if (err) return console.error(err)
quote.set({ en, sr, author, source })
quote.save(err => {
if (err) return console.error(err)
res.send('SUCCESS_SAVED')
})
})
}

0 comments on commit 11cede8

Please sign in to comment.