sbkwgh 8 éve
szülő
commit
c5fd80e14a
4 módosított fájl, 17 hozzáadás és 41 törlés
  1. 6 2
      models/category.js
  2. 8 7
      routes/category.js
  3. 1 32
      routes/user.js
  4. 2 0
      test/user.js

+ 6 - 2
models/category.js

@@ -26,10 +26,14 @@ module.exports = (sequelize, DataTypes) => {
 			associate (models) {
 				Category.hasMany(models.Thread)
 			},
-			includeOptions (order, threadLimit) {
+			includeOptions (order, threadLimit, where, from) {
 				let models = sequelize.models
 				let options = {
-					model: models.Thread, 
+					model: models.Thread,
+					where: {
+						id: { $gt: from || -1 },
+						userId: where.userId
+					},
 					include: [
 						models.Category,
 						{ model: models.User, attributes: ['username', 'createdAt', 'id', 'color'] }, 

+ 8 - 7
routes/category.js

@@ -3,7 +3,7 @@ let router = express.Router()
 
 const Errors = require('../lib/errors')
 let pagination = require('../lib/pagination')
-let { Category, Post, Thread } = require('../models')
+let { Category, Post, Thread, User } = require('../models')
 
 router.get('/', async (req, res) => {
 	try {
@@ -29,7 +29,9 @@ router.get('/:category', async (req, res) => {
 		let { from, limit } = pagination.getPaginationProps(req.query)
 		let where = {}
 		
-		if(req.query.username) where.username = req.query.username
+		if(req.query.username) {
+			where.userId = await User.findOne({ where: { 'username': req.query.username } }).userId
+		}
 
 		function concatenateThreads(threads) {
 			let processedThreads = []
@@ -43,18 +45,17 @@ router.get('/:category', async (req, res) => {
 		}
 
 		if(req.params.category === 'ALL') {
-			threads = await Category.findAll({ where, include: Category.includeOptions('ASC') })
-			threadsLatestPost = await Category.findAll({ where, include: Category.includeOptions('DESC') })
-
+			threads = await Category.findAll({ include: Category.includeOptions('ASC', limit, where, from) })
+			threadsLatestPost = await Category.findAll({ include: Category.includeOptions('DESC', limit, where, from) })
 		} else {
 			threads = await Category.findOne({
 				where: { name: req.params.category },
-				include: Category.includeOptions('ASC')
+				include: Category.includeOptions('ASC', limit, where, from)
 			})
 
 			threadsLatestPost = await Category.findOne({
 				where: { name: req.params.category },
-				include: Category.includeOptions('DESC')
+				include: Category.includeOptions('DESC', limit, where, from)
 			})
 		}
 

+ 1 - 32
routes/user.js

@@ -150,38 +150,7 @@ router.get('/:username', async (req, res) => {
 
 			res.json(resUser)
 		} else if(req.query.threads) {
-			let { from, limit } = pagination.getPaginationProps(req.query)
-
-			queryObj.include = [{
-				model: Thread,
-				include: [Category],
-				limit,
-				where: { id: { $gt: from } },
-				order: [['id', 'ASC']]
-			}]
-
-			let user = await User.findOne(queryObj)
-			if(!user) throw Errors.accountDoesNotExist
-
-			let resUser = user.toJSON()
-			resUser.meta = {}
-
-			let nextId = await pagination.getNextId(Thread, { userId: user.id }, resUser.Threads)
-
-			if(nextId) {
-				resUser.meta.nextURL =
-					`/api/v1/user/${user.username}?threads=true&limit=${limit}&from=${nextId}`
-
-				resUser.meta.nextThreadsCount = await pagination.getNextCount(
-					Thread, resUser.Threads, limit,
-					{ UserId: user.id }
-				)
-			} else {
-				resUser.meta.nextURL = null
-				resUser.meta.nextThreadsCount = 0
-			}
-
-			res.json(resUser)
+			res.redirect('/api/v1/category/ALL?username=' + req.params.username)
 		} else {
 			let user = await User.findOne(queryObj)
 			if(!user) throw Errors.accountDoesNotExist

+ 2 - 0
test/user.js

@@ -398,6 +398,8 @@ describe('User', () => {
 			}
 
 			let pageOne = await agent.get('/api/v1/user/threadaccount?threads=true')
+			console.log(pageOne.body)
+
 			let pageTwo = await agent.get(pageOne.body.meta.nextURL)
 			let pageInvalid = await agent.get('/api/v1/user/threadaccount?threads=true&from=100')