Skip to content

Commit

Permalink
Merge pull request #19 from atian25/egg-fix
Browse files Browse the repository at this point in the history
feat: egg style && upgrade to 2
  • Loading branch information
wuomzfx committed Dec 1, 2017
2 parents c6ed9a1 + a79eea0 commit 8a8dba5
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 91 deletions.
3 changes: 2 additions & 1 deletion server/.autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
Expand All @@ -14,8 +15,8 @@ module.exports = {
'egg-bin',
'autod',
'eslint',
'eslint-config-egg',
'supertest',
'autod-egg',
'webstorm-disable-index',
],
exclude: [
Expand Down
6 changes: 3 additions & 3 deletions server/app/controller/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = app => {
}
}
const resources = yield this.service.api.getRichList(condition, page, limit, order)
const count = yield app.model.api.find(condition).count().exec()
const count = yield app.model.Api.find(condition).count().exec()
this.ctx.body = { resources, pages: { limit, page, count } }
this.ctx.status = 200
}
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = app => {
assert(mongoose.Types.ObjectId.isValid(groupId), 403, 'invalid groupId')
assert(mongoose.Types.ObjectId.isValid(apiId), 403, 'invalid apiId')

const resources = (yield app.model.api.findOne({ _id: apiId, isDeleted: false })).toObject()
const resources = (yield app.model.Api.findOne({ _id: apiId, isDeleted: false })).toObject()
resources.history = yield this.service.apiHistory.get(resources)

this.ctx.body = { resources }
Expand Down Expand Up @@ -194,7 +194,7 @@ module.exports = app => {
msg: '无权删除'
})
}
yield app.model.group.update({ _id: groupId }, { modifiedTime: Date.now() }, { new: true }).exec()
yield app.model.Group.update({ _id: groupId }, { modifiedTime: Date.now() }, { new: true }).exec()
this.ctx.logger.info('deleteApi')
this.ctx.status = 204
}
Expand Down
2 changes: 1 addition & 1 deletion server/app/controller/authority.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = app => {
}
* getApi () {
const { apiId } = this.ctx.params
const authority = (yield this.service.apiAuthority.get(apiId)) || app.model.apiAuthority()
const authority = (yield this.service.apiAuthority.get(apiId)) || app.model.ApiAuthority()
authority.apiId = apiId
this.success(authority)
}
Expand Down
4 changes: 2 additions & 2 deletions server/app/controller/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ module.exports = app => {
if (id.length < 5) {
// hack方法,兼容老的存下url信息的api
const url = `/client/${id}`
return yield app.model.api.findOne({ url, 'options.method': method }).exec()
return yield app.model.Api.findOne({ url, 'options.method': method }).exec()
}
return yield app.model.api.findOne({ _id: id, 'options.method': method }).exec()
return yield app.model.Api.findOne({ _id: id, 'options.method': method }).exec()
}
* real () {
const { _apiRealUrl, _apiMethod } = this.ctx.request.body
Expand Down
14 changes: 7 additions & 7 deletions server/app/controller/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ module.exports = app => {
isDeleted: false,
name: reg
}
const resources = yield app.model.group
.find(cond)
.sort({ modifiedTime: -1, createTime: -1 })
.skip((page - 1) * limit)
.limit(limit)
.exec()
const count = yield app.model.group.find(cond).count().exec()
const resources = yield app.model.Group
.find(cond)
.sort({ modifiedTime: -1, createTime: -1 })
.skip((page - 1) * limit)
.limit(limit)
.exec()
const count = yield app.model.Group.find(cond).count().exec()
this.ctx.body = { resources, pages: { limit, page, count } }
this.ctx.status = 200
}
Expand Down
3 changes: 2 additions & 1 deletion server/app/model/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = mongoose => {
module.exports = app => {
const mongoose = app.mongoose
const { ObjectId } = mongoose.Schema.Types
const ApiSchema = mongoose.Schema({
group: {
Expand Down
5 changes: 3 additions & 2 deletions server/app/model/api_authority.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = mongoose => {
module.exports = app => {
const mongoose = app.mongoose
const { ObjectId } = mongoose.Schema.Types
const ApiAuthoritySchema = mongoose.Schema({
apiId: {
Expand All @@ -9,7 +10,7 @@ module.exports = mongoose => {
operation: { // 编辑权限
mode: {
type: Number,
default: 0 // 0 - 所有人, 1 - 组内人员 2 - 指定人员
default: 0 // 0 - 所有人, 1 - 组内人员 2 - 指定人员
},
operator: {
type: [ ObjectId ],
Expand Down
3 changes: 2 additions & 1 deletion server/app/model/api_history.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = mongoose => {
module.exports = app => {
const mongoose = app.mongoose
const { ObjectId } = mongoose.Schema.Types
const ApiHistorySchema = mongoose.Schema({
apiId: {
Expand Down
3 changes: 2 additions & 1 deletion server/app/model/api_stat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const moment = require('moment')
module.exports = mongoose => {
module.exports = app => {
const mongoose = app.mongoose
const { ObjectId } = mongoose.Schema.Types
const ApiStatSchema = mongoose.Schema({
apiId: {
Expand Down
3 changes: 2 additions & 1 deletion server/app/model/group.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = mongoose => {
module.exports = app => {
const mongoose = app.mongoose
const { ObjectId } = mongoose.Schema.Types
const GroupSchema = new mongoose.Schema({
teamId: {
Expand Down
3 changes: 2 additions & 1 deletion server/app/model/team.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* 团队;目前无用,可设计为分组的外层
*/
module.exports = mongoose => {
module.exports = app => {
const mongoose = app.mongoose
const { ObjectId } = mongoose.Schema.Types
const TeamSchema = new mongoose.Schema({
name: {
Expand Down
3 changes: 2 additions & 1 deletion server/app/model/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = mongoose => {
module.exports = app => {
const mongoose = app.mongoose
const { ObjectId } = mongoose.Schema.Types
const UserSchema = new mongoose.Schema({
email: {
Expand Down
24 changes: 12 additions & 12 deletions server/app/service/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = app => {
class Api extends app.Service {
getById (apiId) {
return app.model.api.findOne({
return app.model.Api.findOne({
_id: apiId,
isDeleted: false
})
Expand All @@ -14,29 +14,29 @@ module.exports = app => {
api.follower = [ authId ]
return api
})
return app.model.api.insertMany(apis)
return app.model.Api.insertMany(apis)
}
create (api) {
const authId = this.ctx.authUser._id
api.creator = authId
api.manager = authId
api.follower = [ authId ]
return app.model.api(api).save()
return app.model.Api(api).save()
}
update (apiId, api) {
api.modifiedTime = Date.now()
return app.model.api.findOneAndUpdate({
return app.model.Api.findOneAndUpdate({
_id: apiId
}, api, { new: true })
}
* isManager (apiId) {
return !!(yield app.model.api.findOne({
return !!(yield app.model.Api.findOne({
_id: apiId,
manager: this.ctx.authUser._id
}))
}
delete (apiId) {
return app.model.api.findOneAndUpdate({
return app.model.Api.findOneAndUpdate({
_id: apiId,
manager: this.ctx.authUser._id
}, {
Expand All @@ -45,19 +45,19 @@ module.exports = app => {
})
}
deleteGroupApis (groupId) {
return app.model.api.update({
return app.model.Api.update({
group: groupId
}, {
modifiedTime: Date.now(),
isDeleted: true
}, { multi: true })
}
getList (cond, page, limit, order = {}) {
return app.model.api
.find(cond)
.sort(Object.assign(order, { modifiedTime: -1, createTime: -1 }))
.skip((page - 1) * limit)
.limit(limit)
return app.model.Api
.find(cond)
.sort(Object.assign(order, { modifiedTime: -1, createTime: -1 }))
.skip((page - 1) * limit)
.limit(limit)
}
getManageList (page, limit) {
const cond = {
Expand Down
10 changes: 5 additions & 5 deletions server/app/service/api_authority.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { authority } = require('../../constants')
const {
OPERATION_ALL,
OPERATION_MEMBER,
OPERATION_DESIGNEE
OPERATION_ALL,
OPERATION_MEMBER,
OPERATION_DESIGNEE
} = authority

module.exports = app => {
class ApiAuthority extends app.Service {
update (apiId, authority) {
authority = (typeof authority === 'object') ? authority : {}
authority.modifiedTime = Date.now()
return app.model.apiAuthority.findOneAndUpdate({
return app.model.ApiAuthority.findOneAndUpdate({
apiId
}, authority, {
setDefaultsOnInsert: true,
Expand All @@ -19,7 +19,7 @@ module.exports = app => {
})
}
get (apiId) {
return app.model.apiAuthority.findOne({
return app.model.ApiAuthority.findOne({
apiId
})
}
Expand Down
6 changes: 3 additions & 3 deletions server/app/service/api_history.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module.exports = app => {
class ApiHistory extends app.Service {
get (apiId) {
return app.model.apiHistory.findOne({
return app.model.ApiHistory.findOne({
apiId
})
}
create (api) {
return app.model.apiHistory({
return app.model.ApiHistory({
apiId: api._id,
data: api
}).save()
Expand All @@ -18,7 +18,7 @@ module.exports = app => {
operator: _id,
operatorName: name
}
return app.model.apiHistory.findOneAndUpdate({
return app.model.ApiHistory.findOneAndUpdate({
apiId: api._id
}, {
updateTime: Date.now(),
Expand Down
26 changes: 13 additions & 13 deletions server/app/service/group.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { authority } = require('../../constants')
const {
PRIVACY_ALL,
PRIVACY_MEMBER,
PRIVACY_SELF,
OPERATION_ALL,
OPERATION_MEMBER
PRIVACY_ALL,
PRIVACY_MEMBER,
PRIVACY_SELF,
OPERATION_ALL,
OPERATION_MEMBER
} = authority

module.exports = app => {
Expand Down Expand Up @@ -38,17 +38,17 @@ module.exports = app => {
manager: authId
}]
}
return app.model.group.find(cond).sort({ modifiedTime: -1, createTime: -1 })
return app.model.Group.find(cond).sort({ modifiedTime: -1, createTime: -1 })
}
update (groupId, group) {
return app.model.group.findOneAndUpdate({
return app.model.Group.findOneAndUpdate({
_id: groupId,
manager: this.ctx.authUser._id
}, Object.assign(group, { modifiedTime: Date.now() }), { new: true })
}
updateTime (groupId) {
// 此方法允许异步执行
return app.model.group.update({ _id: groupId }, { modifiedTime: Date.now() }, { new: true }).exec()
return app.model.Group.update({ _id: groupId }, { modifiedTime: Date.now() }, { new: true }).exec()
}
create (group) {
const authId = this.ctx.authUser._id
Expand All @@ -57,24 +57,24 @@ module.exports = app => {
creator: authId,
manager: authId
}
return app.model.group(_group).save()
return app.model.Group(_group).save()
}
getUserGroups (user, rights) {
return app.model.group.find({
return app.model.Group.find({
[rights]: user,
isDeleted: false
}).sort({
createTime: -1
})
}
getById (groupId) {
return app.model.group.findOne({
return app.model.Group.findOne({
_id: groupId
})
}
* delete (groupId) {
const group = yield this.getById(groupId)
return app.model.group.findOneAndUpdate({
return app.model.Group.findOneAndUpdate({
_id: groupId,
manager: this.ctx.authUser._id
}, {
Expand All @@ -90,7 +90,7 @@ module.exports = app => {
return this.getUserGroups(null, 'manager')
}
claim (groupId) {
return app.model.group.findOneAndUpdate({
return app.model.Group.findOneAndUpdate({
_id: groupId,
manager: null
}, {
Expand Down
4 changes: 2 additions & 2 deletions server/app/service/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = app => {
class Stat extends app.Service {
// 保存数据方法异步执行
saveApiStat (apiId, behavior, result) {
return app.model.apiStat({
return app.model.ApiStat({
apiId,
behavior,
result
Expand All @@ -17,7 +17,7 @@ module.exports = app => {
})
}
getMockStat (start, end) {
return app.model.apiStat.aggregate([
return app.model.ApiStat.aggregate([
{
$match: {
behavior: API_BEHAVIOR_MOCK,
Expand Down
Loading

0 comments on commit 8a8dba5

Please sign in to comment.