0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-28 02:42:39 +00:00
Add fields to Mongoose brew query
This commit is contained in:
Trevor Buckner
2022-04-29 16:25:28 -04:00
committed by GitHub
2 changed files with 18 additions and 5 deletions

View File

@@ -194,7 +194,20 @@ app.get('/download/:id', asyncHandler(async (req, res)=>{
app.get('/user/:username', async (req, res, next)=>{
const ownAccount = req.account && (req.account.username == req.params.username);
let brews = await HomebrewModel.getByUser(req.params.username, ownAccount)
const fields = [
'title',
'pageCount',
'description',
'authors',
'views',
'shareId',
'editId',
'createdAt',
'updatedAt',
'lastViewed'
];
let brews = await HomebrewModel.getByUser(req.params.username, ownAccount, fields)
.catch((err)=>{
console.log(err);
});

View File

@@ -37,9 +37,9 @@ HomebrewSchema.statics.increaseView = async function(query) {
return brew;
};
HomebrewSchema.statics.get = function(query){
HomebrewSchema.statics.get = function(query, fields=null){
return new Promise((resolve, reject)=>{
Homebrew.find(query, (err, brews)=>{
Homebrew.find(query, fields, null, (err, brews)=>{
if(err || !brews.length) return reject('Can not find brew');
if(!_.isNil(brews[0].textBin)) { // Uncompress zipped text field
unzipped = zlib.inflateRawSync(brews[0].textBin);
@@ -52,13 +52,13 @@ HomebrewSchema.statics.get = function(query){
});
};
HomebrewSchema.statics.getByUser = function(username, allowAccess=false){
HomebrewSchema.statics.getByUser = function(username, allowAccess=false, fields=null){
return new Promise((resolve, reject)=>{
const query = { authors: username, published: true };
if(allowAccess){
delete query.published;
}
Homebrew.find(query).lean().exec((err, brews)=>{ //lean() converts results to JSObjects
Homebrew.find(query, fields).lean().exec((err, brews)=>{ //lean() converts results to JSObjects
if(err) return reject('Can not find brew');
return resolve(brews);
});