1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-26 15:21:29 +00:00
Files
homebrewery/server/brew.data.js
2017-01-22 12:49:22 -05:00

118 lines
2.7 KiB
JavaScript

const _ = require('lodash');
const shortid = require('shortid');
const mongoose = require('./db.js').instance;
const Error = require('./error.js');
const utils = require('./utils.js');
const BrewSchema = mongoose.Schema({
shareId : {type : String, default: shortid.generate, index: { unique: true }},
editId : {type : String, default: shortid.generate, index: { unique: true }},
text : {type : String, default : ""},
title : {type : String, default : ""},
description : {type : String, default : ""},
tags : {type : String, default : ""},
systems : [String],
authors : [String],
published : {type : Boolean, default : false},
createdAt : { type: Date, default: Date.now },
updatedAt : { type: Date, default: Date.now},
lastViewed : { type: Date, default: Date.now},
views : {type:Number, default:0}
}, {
versionKey: false,
toJSON : {
transform: (doc, ret, options) => {
delete ret._id;
return ret;
}
}
});
BrewSchema.methods.increaseView = function(){
this.views = this.views + 1;
return this.save();
};
const Brew = mongoose.model('Brew', BrewSchema);
const BrewData = {
schema : BrewSchema,
model : Brew,
get : (query) => {
return Brew.findOne(query).exec()
.then((brew) => {
if(!brew) throw Error.noBrew();
return brew;
});
},
create : (brew) => {
delete brew.shareId;
delete brew.editId;
if(!brew.title) brew.title = utils.getGoodBrewTitle(brew.text);
return (new Brew(brew)).save();
},
update : (editId, newBrew) => {
return BrewData.get({ editId })
.then((brew) => {
delete newBrew.shareId;
delete newBrew.editId;
brew = _.merge(brew, newBrew, { updatedAt : Date.now() });
return brew.save();
});
},
remove : (editId) => {
return Brew.find({ editId }).remove().exec();
},
removeAll : ()=>{
return Brew.find({}).remove().exec();
},
//////// Special
getByShare : (shareId) => {
return BrewData.get({ shareId : shareId})
.then((brew) => {
brew.increaseView();
const brewJSON = brew.toJSON();
delete brewJSON.editId;
return brewJSON;
});
},
getByEdit : (editId) => {
return BrewData.get({ editId });
},
//Removes all empty brews that are older than 3 days and that are shorter than a tweet
removeInvalid : (force = false) => {
const invalidBrewQuery = Brew.find({
'$where' : "this.text.length < 140",
createdAt: {
$lt: Moment().subtract(3, 'days').toDate()
}
});
if(force) return invalidBrewQuery.remove().exec();
return invalidBrewQuery.exec()
.then((objs) => {
return objs.length;
});
},
search : (query, req={}) => {
//defaults with page and count
//returns a non-text version of brews
//assume sanatized ?
return Promise.resolve([]);
},
};
module.exports = BrewData;