فهرست منبع

Implement update description tests

sbkwgh 8 سال پیش
والد
کامیت
d4d0318a17
2فایلهای تغییر یافته به همراه53 افزوده شده و 0 حذف شده
  1. 1 0
      models/user.js
  2. 52 0
      routes/user.js

+ 1 - 0
models/user.js

@@ -6,6 +6,7 @@ module.exports = (sequelize, DataTypes) => {
 			type: DataTypes.STRING,
 			unique: true
 		},
+		description: DataTypes.TEXT,
 		color: {
 			type: DataTypes.STRING,
 			defaultValue () {

+ 52 - 0
routes/user.js

@@ -257,4 +257,56 @@ router.post('/:username/logout', async (req, res) => {
 	})
 })
 
+router.all('*', (req, res, next) => {
+	if(req.session.username) {
+		next()
+	} else {
+		res.status(401)
+		res.json({
+			errors: [Errors.requestNotAuthorized]
+		})
+	}
+})
+
+router.put('/:username', async (req, res) => {
+	let validationErrors = []
+
+	try {
+		if(req.session.username !== req.params.username) {
+			validationErrors.push(Errors.requestNotAuthorized)
+			throw validationErrors
+		}
+
+
+		if(req.body.description) {
+			if(typeof req.body.description !== 'string') {
+				validationErrors.push(Errors.invalidParameterType('description', 'string'))
+			} else if(req.body.description.length > 1024) {
+				validationErrors.push(Errors.parameterLengthTooLarge('description', 1024))
+			}
+
+			if(validationErrors.length) throw validationErrors
+
+			let user = await User.update({ description: req.body.description }, { where: {
+				username: req.session.username
+			}})
+
+			res.json({ success: true })
+
+		} else if(req.body.newPassword) {
+			res.json({})
+		}
+	} catch (e) {
+		if(validationErrors.length) {
+			res.status(400)
+			res.json({ errors: validationErrors })
+		} else {
+			console.log(e)
+
+			res.status(500)
+			res.json({errors: Errors.unknown })
+		}
+	}
+})
+
 module.exports = router