Browse Source

Add route to category to get all threads in a category

sbkwgh 8 năm trước cách đây
mục cha
commit
eec16d5755
3 tập tin đã thay đổi với 105 bổ sung0 xóa
  1. 20 0
      models/category.js
  2. 27 0
      routes/category.js
  3. 58 0
      test/category.js

+ 20 - 0
models/category.js

@@ -13,6 +13,26 @@ module.exports = (sequelize, DataTypes) => {
 			type: DataTypes.STRING,
 			unique: true
 		}
+	}, {
+		classMethods: {
+			associate (models) {
+				Category.hasMany(models.Thread)
+			},
+			includeOptions () {
+				let models = sequelize.models
+
+				return [{
+					model: models.Thread, 
+					include: [
+						{ model: models.User, attributes: ['username', 'createdAt', 'id'] }, 
+						{
+							model: models.Post, limit: 1, include:
+							[{ model: models.User, attributes: ['username', 'id'] }]
+						}
+					]
+				}]
+			}
+		}
 	})
 
 	return Category

+ 27 - 0
routes/category.js

@@ -20,6 +20,33 @@ router.get('/', async (req, res) => {
 	
 })
 
+
+router.get('/:category', async (req, res) => {
+	try {
+		let threads = await Category.findOne({
+			where: { name: req.params.category },
+			include: Category.includeOptions()
+		})
+
+		if(!threads) throw Errors.invalidParameter('id', 'thread does not exist')
+			
+		res.json(threads.toJSON())
+	} catch (e) {
+		if(e.name === 'invalidParameter') {
+			res.status(400)
+			res.json({
+				errors: [e]
+			})
+		} else {
+			console.log(e)
+			res.status(500)
+			res.json({
+				errors: [Errors.unknown]
+			})
+		}
+	}
+})
+
 router.all('*', (req, res, next) => {
 	if(!req.session.loggedIn || !req.session.admin) {
 		res.status(401)

+ 58 - 0
test/category.js

@@ -148,4 +148,62 @@ describe('Category', () => {
 			res.body.should.contain.an.item.with.property('name', 'another_category')
 		})
 	})
+
+	describe('GET /category/:category', () => {
+		before(async () => {
+			let agent = chai.request.agent(server)
+
+			await agent
+				.post('/api/v1/user/adminaccount/login')
+				.set('content-type', 'application/json')
+				.send({ password: 'password' })
+
+			await agent
+				.post('/api/v1/thread')
+				.set('content-type', 'application/json')
+				.send({ name: 'thread', category: 'category' })
+
+			await agent
+				.post('/api/v1/thread')
+				.set('content-type', 'application/json')
+				.send({ name: 'another_thread', category: 'category' })
+
+			await agent
+				.post('/api/v1/post')
+				.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', 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.0.User.username', 'adminaccount')
+		})
+		it('should return an error if category does not exist', async () => {
+			try {
+				let res = await chai.request(server)
+					.get('/api/v1/category/not_real')
+
+				res.should.be.json
+				res.should.have.status(400)
+				res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
+			} catch (res) {
+				let body = JSON.parse(res.response.text)
+				res.should.have.status(400)
+				body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'thread does not exist'))
+			}
+		})
+	})
 })