标签:测试 更新 string 删除 alt and cat ext amp
需求:知乎中用户编辑资料的图片上传
分析:通过把图片上传到服务器中,再返回url
const KoaBody = require('koa-body')
app.use(KoaBody({
multipart: true,
formidable: {
// 设置上传地址
uploadDir: path.join(__dirname, '/public/uploads'),
// 保留图片后缀
keepExtensions: true
}
}))
const KoaStatic = require('koa-static')
app.use(KoaStatic(
path.join(__dirname, staticPath)
))
router.post('/upload', upload)
// home.js
upload (ctx) {
const file = ctx.request.files.file
const basename = path.basename(file.path)
ctx.body = {
url: `${ctx.origin}/uploads/${basename}`
}
}
需求:如图所示,实现编辑用户资料的接口
const UserSchema = new Schema({
...
avatar_url: {type: String},
gender: {type: String, enum: ['male', 'female'], default: 'male'},
headline: {type: String},
locations: {type: [{type: String}], select: false},
business: {type: String, select: false},
employments: {
type: [{
company: {type: String},
job: {type: String},
}],
select: false
},
educations: {
type: [{
school: {type: String},
major: {type: String},
diploma: {type: Number, enum: [1, 2, 3, 4, 5]},
enterance_year: {type: Number},
graduation_year: {type: Number}
}],
select: false
},
following: {
type: [{type: Schema.Types.ObjectId, ref: 'User'}],
select: false
}
})
async update(ctx) {
ctx.verifyParams({
name: {type: 'string', required: false},
password: {type: 'string', required: false},
avatar_url: {type: 'string', required: false},
gender: {type: 'string', required: false},
headline: {type: 'string', required: false},
locations: {type: 'array', itemType: 'string', required: false},
business: {type: 'string', required: false},
employments: {type: 'array', itemType: 'object', required: false},
educations: {type: 'array', itemType: 'object', required: false},
})
const user = await User.findByIdAndUpdate(ctx.params.id, ctx.request.body)
if (!user) {ctx.throw(404, '用户不存在')}
ctx.body = user
}
async findById(ctx) {
const {fields} = ctx.query
const selectFields = fields.split(';').filter(f => f).map(f => ' +' + f).join('')
const user = await User.findById(ctx.params.id).select(selectFields)
if (!user) {ctx.throw(404, '用户不存在')}
ctx.body = user
}
总结:
a. 定义数据模型Schema
b. 编写转发的路由
c. 使用数据模型编写控制器逻辑
e. 使用Postman测试
f. 编写单元测试和压测
2.更新或者删除用户的信息,是需要鉴权的过程的。
标签:测试 更新 string 删除 alt and cat ext amp
原文地址:https://www.cnblogs.com/Jomsou/p/12500180.html