static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map( (chapter) => chapter.toObject(), ); return book; }
router.get('/vipuser', async (req, res) => { const userResult = await User.find({name: 'hua1995116'}, '_id name src').exec(); res.json({ errno: 0, data: userResult }) })
var find = function (data, callback){ roomModel.find(data, callback); }
router.post('/list', async (req, res) => { const {selfId} = req.body; if (!selfId) { global.logger.error('selfId can\'t find') res.json({ errno: 1 }); return; } const checkFriend = await Friend.find({selfId}).populate({ path: 'friendId', select: 'name src socketId' }).exec(); console.log(checkFriend); res.json({ errno: 0, data: checkFriend }) });
router.post('/stripe/fetch-checkout-session', async (req, res) => { try { const { bookId, redirectUrl } = req.body; const book = await Book.findById(bookId).select(['slug']).setOptions({ lean: true }); if (!book) { throw new Error('Book not found'); } const isPurchased = (await Purchase.find({ userId: req.user._id, bookId: book._id }).countDocuments()) > 0; if (isPurchased) { throw new Error('Already bought this book'); } const session = await createSession({ userId: req.user._id.toString(), userEmail: req.user.email, bookId, bookSlug: book.slug, redirectUrl, }); res.json({ sessionId: session.id }); } catch (err) { logger.error(err); res.json({ error: err.message || err.toString() }); } });
router.get('/getInfo', async (req, res) => { const { id } = req.query; if (!id) { global.logger.error('id can\'t find') res.json({ errno: 1 }); return; } const userResult = await User.find({name: id}).exec(); console.log(userResult); res.json({ errno: 0, data: { id: userResult[0]._id, name: userResult[0].name, src: userResult[0].src } }) })
router.get('/search', async (req, res) => { const { name } = req.query; if(!name) { global.logger.error('name can\'t find') res.json({ errno: 0, data: [] // msg: 'name can\'t find' }); return; } const result = await User.find({name: new RegExp(name)}, '_id name src').exec(); res.json({ errno: 0, data: result }) })
static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map( (chapter) => chapter.toObject(), ); return book; }
static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map( (chapter) => chapter.toObject(), ); return book; }
static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map( (chapter) => chapter.toObject(), ); return book; }
static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = ( await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 }) ).map((chapter) => chapter.toObject()); return book; }
static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = ( await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 }) ).map((chapter) => chapter.toObject()); return book; }
static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map( (chapter) => chapter.toObject(), ); return book; }
static async getBySlug({ slug }) { const bookDoc = await this.findOne({ slug }); if (!bookDoc) { throw new Error('Book not found'); } const book = bookDoc.toObject(); book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map( (chapter) => chapter.toObject(), ); return book; }
router.post('/stripe/fetch-checkout-session', async (req, res) => { try { const { bookId, redirectUrl } = req.body; const book = await Book.findById(bookId).select(['slug']).setOptions({ lean: true }); if (!book) { throw new Error('Book not found'); } const isPurchased = (await Purchase.find({ userId: req.user._id, bookId: book._id }).countDocuments()) > 0; if (isPurchased) { throw new Error('Already bought this book'); } const session = await createSession({ userId: req.user._id.toString(), userEmail: req.user.email, bookId, bookSlug: book.slug, redirectUrl, }); res.json({ sessionId: session.id }); } catch (err) { logger.error(err); res.json({ error: err.message || err.toString() }); } });