yapi/server/controllers/follow.js

130 lines
3.6 KiB
JavaScript
Raw Normal View History

const yapi = require('../yapi.js');
const baseController = require('./base.js');
const followModel = require('../models/follow');
const projectModel = require('../models/project');
2017-08-10 12:13:32 +08:00
class followController extends baseController {
constructor(ctx) {
super(ctx);
this.Model = yapi.getInst(followModel);
2017-08-24 11:00:10 +08:00
this.projectModel = yapi.getInst(projectModel);
2017-08-10 12:13:32 +08:00
}
/**
2017-08-10 15:47:33 +08:00
* 获取关注项目列表
* @interface /follow/list
2017-08-10 12:13:32 +08:00
* @method GET
2017-08-10 15:47:33 +08:00
* @category follow
2017-08-10 12:13:32 +08:00
* @foldnumber 10
* @param {Number} [page] 分页页码
* @param {Number} [limit] 分页大小
* @returns {Object}
2017-08-10 15:47:33 +08:00
* @example /follow/list
2017-08-10 12:13:32 +08:00
*/
2017-08-11 16:35:53 +08:00
2017-08-10 12:13:32 +08:00
async list(ctx) {
2017-09-08 11:18:30 +08:00
let uid = this.getUid();
// 关注列表暂时不分页 page & limit 为分页配置
// page = ctx.request.query.page || 1,
// limit = ctx.request.query.limit || 10;
2017-08-10 12:13:32 +08:00
if (!uid) {
return ctx.body = yapi.commons.resReturn(null, 400, '用户id不能为空');
}
try {
2017-08-22 17:00:27 +08:00
let result = await this.Model.list(uid);
2017-08-11 16:35:53 +08:00
2017-08-10 12:13:32 +08:00
ctx.body = yapi.commons.resReturn({
list: result
});
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 402, err.message);
}
}
2017-08-10 15:47:33 +08:00
/**
* 取消关注
2017-08-11 16:35:53 +08:00
* @interface /follow/del
2017-08-10 15:47:33 +08:00
* @method POST
* @category follow
* @foldnumber 10
2017-09-08 11:18:30 +08:00
* @param {Number} projectid
2017-08-10 15:47:33 +08:00
* @returns {Object}
* @example /follow/del
*/
2017-08-10 12:13:32 +08:00
async del(ctx) {
2017-08-22 17:00:27 +08:00
let params = ctx.request.body, uid = this.getUid();
if(!params.projectid){
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
let checkRepeat = await this.Model.checkProjectRepeat(uid,params.projectid);
2017-08-10 12:13:32 +08:00
2017-08-22 17:00:27 +08:00
if (checkRepeat == 0) {
return ctx.body = yapi.commons.resReturn(null, 401, '项目未关注');
2017-08-10 12:13:32 +08:00
}
try {
2017-08-22 17:00:27 +08:00
let result = await this.Model.del(params.projectid, this.getUid());
2017-08-10 12:13:32 +08:00
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
2017-08-10 15:47:33 +08:00
/**
* 添加关注
2017-08-11 16:35:53 +08:00
* @interface /follow/add
2017-08-11 17:49:47 +08:00
* @method GET
2017-08-10 15:47:33 +08:00
* @category follow
* @foldnumber 10
* @param {Number} projectid 项目id
* @param {String} projectname 项目名
* @param {String} icon 项目icon
* @returns {Object}
* @example /follow/add
*/
2017-08-10 12:13:32 +08:00
async add(ctx) {
let params = ctx.request.body;
params = yapi.commons.handleParams(params, {
2017-08-24 11:00:10 +08:00
projectid: 'number'
2017-08-10 12:13:32 +08:00
});
2017-08-24 11:00:10 +08:00
let uid = this.getUid();
2017-08-10 12:13:32 +08:00
if (!params.projectid) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
2017-08-22 17:00:27 +08:00
let checkRepeat = await this.Model.checkProjectRepeat(uid,params.projectid);
2017-08-10 12:13:32 +08:00
if (checkRepeat) {
return ctx.body = yapi.commons.resReturn(null, 401, '项目已关注');
}
try {
2017-08-24 11:00:10 +08:00
let project = await this.projectModel.get(params.projectid);
let data = {
uid: uid,
projectid: params.projectid,
projectname: project.name,
icon: project.icon,
color: project.color
};
2017-08-10 12:13:32 +08:00
let result = await this.Model.save(data);
2017-08-11 17:49:47 +08:00
result = yapi.commons.fieldSelect(result, ['_id', 'uid', 'projectid', 'projectname', 'icon', 'color']);
2017-08-10 12:13:32 +08:00
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
}
2017-08-11 16:35:53 +08:00
module.exports = followController;