mirror of
https://github.com/wassname/talk.git
synced 2026-07-19 11:28:50 +08:00
Implement asset upsert and find
This commit is contained in:
@@ -4,6 +4,10 @@
|
||||
const express = require('express');
|
||||
|
||||
const app = express();
|
||||
const bodyParser = require('body-parser');
|
||||
|
||||
app.use(bodyParser.json()); // for parsing application/json
|
||||
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
|
||||
|
||||
app.use('/api/v1', require('./routes/api'));
|
||||
app.use('/client/', express.static('./dist'));
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
const mongoose = require('../mongoose');
|
||||
const uuid = require('uuid');
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const AssetSchema = new Schema({
|
||||
|
||||
id: {
|
||||
type: String,
|
||||
default: uuid.v4,
|
||||
unique: true,
|
||||
index: true
|
||||
},
|
||||
url: {
|
||||
type: String,
|
||||
unique: true,
|
||||
index: true
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
default: 'article'
|
||||
},
|
||||
headline: String,
|
||||
summary: String,
|
||||
section: String,
|
||||
subsection: String,
|
||||
authors: [String],
|
||||
publication_date: Date
|
||||
},{
|
||||
_id: false,
|
||||
timestamps: {
|
||||
createdAt: 'created_at',
|
||||
updatedAt: 'updated_at'
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Finds an asset by its id.
|
||||
* @param {String} id identifier of the asset (uuid)
|
||||
*/
|
||||
AssetSchema.statics.findById = function(id) {
|
||||
|
||||
return Asset.findOne({id});
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds a asset by its url.
|
||||
* @param {String} url identifier of the asset (uuid)
|
||||
*/
|
||||
AssetSchema.statics.findByUrl = function(url) {
|
||||
|
||||
return Asset.findOne({url: url});
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Upserts an asset.
|
||||
*/
|
||||
AssetSchema.statics.upsert = function(data) {
|
||||
|
||||
// If an id is not sent, create one.
|
||||
if (typeof data.id === 'undefined') {
|
||||
data.id = uuid.v4();
|
||||
}
|
||||
|
||||
return Asset.update({id: data.id}, data, {upsert: true});
|
||||
|
||||
};
|
||||
|
||||
const Asset = mongoose.model('Asset', AssetSchema);
|
||||
|
||||
module.exports = Asset;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
const mongoose = require('mongoose');
|
||||
const enabled = require('debug').enabled;
|
||||
const url = process.env.TALK_MONGO_URL || 'mongodb://localhost';
|
||||
const url = process.env.TALK_MONGO_URL || 'mongodb://localhost/coral-talk';
|
||||
|
||||
// Use native promises
|
||||
mongoose.Promise = global.Promise;
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/coralproject/talk#readme",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.15.2",
|
||||
"debug": "^2.2.0",
|
||||
"express": "^4.14.0",
|
||||
"mongoose": "^4.6.5"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Asset = require('../../../models/asset');
|
||||
|
||||
// Get an asset by url
|
||||
router.get('/url/:url', (req, res) => {
|
||||
|
||||
Asset.findByUrl(req.params.url)
|
||||
.then((asset) => {
|
||||
res.json(asset);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// Upsert an asset
|
||||
router.put('/', (req, res) => {
|
||||
|
||||
Asset.upsert(req.body)
|
||||
.then((asset) => {
|
||||
res.json(asset);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.json(err);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
+3
-2
@@ -2,9 +2,10 @@ const express = require('express');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/asset', require('./asset'));
|
||||
router.use('/comments', require('./comments'));
|
||||
router.use('/stream', require('./stream'));
|
||||
router.use('/settings', require('./settings'));
|
||||
router.use('/queue', require('./queue'));
|
||||
router.use('/settings', require('./settings'));
|
||||
router.use('/stream', require('./stream'));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user