Ver código fonte

Add latest post as well as first post to category route

sbkwgh 8 anos atrás
pai
commit
b3c6645cad
3 arquivos alterados com 80 adições e 19 exclusões
  1. 2 2
      models/category.js
  2. 41 16
      routes/category.js
  3. 37 1
      test/category.js

+ 2 - 2
models/category.js

@@ -26,7 +26,7 @@ module.exports = (sequelize, DataTypes) => {
 			associate (models) {
 				Category.hasMany(models.Thread)
 			},
-			includeOptions (threadLimit) {
+			includeOptions (order, threadLimit) {
 				let models = sequelize.models
 				let options = {
 					model: models.Thread, 
@@ -34,7 +34,7 @@ module.exports = (sequelize, DataTypes) => {
 						models.Category,
 						{ model: models.User, attributes: ['username', 'createdAt', 'id'] }, 
 						{
-							model: models.Post, limit: 1, include:
+							model: models.Post, limit: 1, order: [['id', order]], include:
 							[{ model: models.User, attributes: ['username', 'id'] }]
 						}
 					]

+ 41 - 16
routes/category.js

@@ -2,14 +2,13 @@ let express = require('express')
 let router = express.Router()
 
 const Errors = require('../lib/errors')
-let { Category } = require('../models')
+let { Category, Post } = require('../models')
 
 router.get('/', async (req, res) => {
 	try {
 		let categories = await Category.findAll({
 			attributes: { exclude: ['id'] },
-			include: Category.includeOptions(1)/*,
-			order: ['updatedAt', 'DESC']*/
+			include: Category.includeOptions(1)
 		})
 
 		res.json(categories)
@@ -25,34 +24,60 @@ router.get('/', async (req, res) => {
 
 router.get('/:category', async (req, res) => {
 	try {
-		let threads
+		let threads, threadsLatestPost, resThreads
+
+		function concatenateThreads(threads) {
+			let processedThreads = []
+			
+			threads.forEach(category => {
+				let jsonCategory = category.toJSON()
+				processedThreads.push(...jsonCategory.Threads)
+			})
+
+			return processedThreads
+		}
 
 		if(req.params.category === 'ALL') {
-			threads = await Category.findAll({ include: Category.includeOptions() })
+			threads = await Category.findAll({ include: Category.includeOptions('ASC') })
+			threadsLatestPost = await Category.findAll({ include: Category.includeOptions('DESC') })
+
 		} else {
 			threads = await Category.findOne({
 				where: { name: req.params.category },
-				include: Category.includeOptions()
+				include: Category.includeOptions('ASC')
+			})
+
+			threadsLatestPost = await Category.findOne({
+				where: { name: req.params.category },
+				include: Category.includeOptions('DESC')
 			})
 		}
 
 		if(!threads) throw Errors.invalidParameter('id', 'thread does not exist')
 		
 		if(Array.isArray(threads)) {
-			let processedThreads = []
-			threads.forEach(category => {
-				let jsonCategory = category.toJSON()
-				processedThreads.push(...jsonCategory.Threads)
-			})
-
-			res.json({
+			resThreads = {
 				name: 'All',
 				value: 'ALL',
-				Threads: processedThreads
-			})
+				Threads: concatenateThreads(threads)
+			}
+
+			threadsLatestPost = { Threads: concatenateThreads(threadsLatestPost) }
 		} else {
-			res.json(threads.toJSON())
+			resThreads = threads.toJSON()
 		}
+
+		threadsLatestPost.Threads.forEach((thread, i) => {
+			let first = resThreads.Threads[i].Posts[0]
+			let latest = thread.Posts[0]
+
+			if(first.id === latest.id) return
+
+			resThreads.Threads[i].Posts.push(latest)
+		})
+
+		res.json(resThreads)
+
 	} catch (e) {
 		if(e.name === 'invalidParameter') {
 			res.status(400)

+ 37 - 1
test/category.js

@@ -174,23 +174,59 @@ describe('Category', () => {
 				.set('content-type', 'application/json')
 				.send({ content: 'content here', threadId: 1 })
 
+			await agent
+				.post('/api/v1/post')
+				.set('content-type', 'application/json')
+				.send({ content: 'content here 2', threadId: 1 })
+
+			await agent
+				.post('/api/v1/post')
+				.set('content-type', 'application/json')
+				.send({ content: 'content here 3', threadId: 1 })
+
 			await agent
 				.post('/api/v1/post')
 				.set('content-type', 'application/json')
 				.send({ content: 'content here', threadId: 2 })
+
+			await agent
+				.post('/api/v1/post')
+				.set('content-type', 'application/json')
+				.send({ content: 'content here 2', threadId: 2 })
+
+			await agent
+				.post('/api/v1/post')
+				.set('content-type', 'application/json')
+				.send({ content: 'content here 3', threadId: 2 })
 		})
 
 		it('should return all threads in a category', async () => {
 			let res = await chai.request(server)
 				.get('/api/v1/category/category')
-			
+
 			res.should.be.json
 			res.should.have.status(200)
 			res.body.should.have.property('name', 'category')
 			res.body.Threads.should.have.property('length', 2)
 			res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
 			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.content', '<p>content here</p>\n')
+			res.body.Threads.should.contain.an.item.with.deep.property('Posts.1.content', '<p>content here 3</p>\n')
+			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
+
+		})
+		it('should return all threads in all categories', async () => {
+			let res = await chai.request(server)
+				.get('/api/v1/category/ALL')
+
+			res.should.be.json
+			res.should.have.status(200)
+			res.body.should.have.property('name', 'All')
+			res.body.Threads.should.have.property('length', 2)
+			res.body.Threads.should.contain.an.item.with.deep.property('User.username', 'adminaccount')
+			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.content', '<p>content here</p>\n')
+			res.body.Threads.should.contain.an.item.with.deep.property('Posts.1.content', '<p>content here 3</p>\n')
 			res.body.Threads.should.contain.an.item.with.deep.property('Posts.0.User.username', 'adminaccount')
+
 		})
 		it('should return an error if category does not exist', async () => {
 			try {