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
11 changed files
with
205 additions
and
56 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 |
---|---|---|
|
@@ -2,3 +2,7 @@ | |
npm-debug.log | ||
.DS_Store | ||
/*.env | ||
|
||
yarn-debug.log* | ||
yarn-error.log* | ||
package-lock.json |
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 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) |
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,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) |
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
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,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') | ||
}) | ||
} |
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,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') | ||
}) | ||
} |
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,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) | ||
}) | ||
}) | ||
} |
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,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')) | ||
} |
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,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') | ||
}) | ||
}) | ||
} |