重构代码, 将item相关接口独立出来
This commit is contained in:
parent
3f9641ad28
commit
14087f02db
@ -101,80 +101,6 @@ export default class AccountController extends BaseController {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@router('post /api/:accountid/items')
|
|
||||||
async itemList(req: any) {
|
|
||||||
let { accountid, type } = req.params
|
|
||||||
let queryData: any = { accountid }
|
|
||||||
if (type != undefined) {
|
|
||||||
queryData.itemtype = type << 0
|
|
||||||
}
|
|
||||||
let items = await BagItem.find(queryData)
|
|
||||||
return items.map(o => o.toJson())
|
|
||||||
}
|
|
||||||
|
|
||||||
@router('post /svr/:accountid/useitem')
|
|
||||||
@router('post /api/:accountid/useitem')
|
|
||||||
async userItem(req: any) {
|
|
||||||
let { accountid, itemid, count } = req.params
|
|
||||||
let record = await BagItem.findOne({
|
|
||||||
accountid,
|
|
||||||
itemid,
|
|
||||||
})
|
|
||||||
if (!record) {
|
|
||||||
throw new ZError(11, 'item not found')
|
|
||||||
}
|
|
||||||
if (record.count < count) {
|
|
||||||
throw new ZError(12, 'not enough item')
|
|
||||||
}
|
|
||||||
record.count -= count
|
|
||||||
let data: ItemInfo[] = ItemCtrl.useItem(itemid, count)
|
|
||||||
await BagItem.addItems(accountid, data)
|
|
||||||
await record.save()
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
@router('post /api/:accountid/buyitem')
|
|
||||||
async buyItem(req: any) {
|
|
||||||
let { itemid, count, accountid } = req.params
|
|
||||||
let pinfo = ItemCtrl.getItemPrice(itemid << 0)
|
|
||||||
if (pinfo.id == 0) {
|
|
||||||
throw new ZError(10, 'item can`t buy')
|
|
||||||
}
|
|
||||||
let record = await BagItem.findOne({
|
|
||||||
accountid,
|
|
||||||
itemid: pinfo.id
|
|
||||||
})
|
|
||||||
if (!record || record.count < pinfo.count * count) {
|
|
||||||
throw new ZError(11, 'not enough money')
|
|
||||||
}
|
|
||||||
record.count -= pinfo.count * count
|
|
||||||
let newItem = (await BagItem.findOrCreate({ accountid, itemid }))
|
|
||||||
.doc
|
|
||||||
newItem.count += count
|
|
||||||
await newItem.save()
|
|
||||||
await record.save()
|
|
||||||
return {
|
|
||||||
itemid,
|
|
||||||
buynum: count,
|
|
||||||
totalnum: newItem.count,
|
|
||||||
priceid: pinfo.id,
|
|
||||||
pricenum: pinfo.count
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@router('post /svr/:accountid/additem')
|
|
||||||
async addItem(req: any) {
|
|
||||||
let { itemid, count, accountid } = req.params
|
|
||||||
let itemInfo = ItemCtrl.findItem(itemid)
|
|
||||||
if (!itemInfo) {
|
|
||||||
throw new ZError(10, 'no item found')
|
|
||||||
}
|
|
||||||
let record = (await BagItem.findOrCreate({ accountid, itemid }))
|
|
||||||
.doc
|
|
||||||
record.count += count
|
|
||||||
record.itemtype = itemInfo._type
|
|
||||||
await record.save()
|
|
||||||
return record.toJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
84
src/controllers/ItemController.ts
Normal file
84
src/controllers/ItemController.ts
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import BaseController from '../common/base.controller'
|
||||||
|
import { router } from '../decorators/router'
|
||||||
|
import { BagItem } from '../models/BagItem'
|
||||||
|
import { ZError } from '../common/ZError'
|
||||||
|
import { ItemInfo } from '../logic/ItemDef'
|
||||||
|
import ItemCtrl from '../logic/ItemCtrl'
|
||||||
|
|
||||||
|
export default class ItemController extends BaseController {
|
||||||
|
@router('post /api/:accountid/items')
|
||||||
|
async itemList(req: any) {
|
||||||
|
let { accountid, type } = req.params
|
||||||
|
let queryData: any = { accountid }
|
||||||
|
if (type != undefined) {
|
||||||
|
queryData.itemtype = type << 0
|
||||||
|
}
|
||||||
|
let items = await BagItem.find(queryData)
|
||||||
|
return items.map(o => o.toJson())
|
||||||
|
}
|
||||||
|
|
||||||
|
@router('post /svr/:accountid/useitem')
|
||||||
|
@router('post /api/:accountid/useitem')
|
||||||
|
async userItem(req: any) {
|
||||||
|
let { accountid, itemid, count } = req.params
|
||||||
|
let record = await BagItem.findOne({
|
||||||
|
accountid,
|
||||||
|
itemid,
|
||||||
|
})
|
||||||
|
if (!record) {
|
||||||
|
throw new ZError(11, 'item not found')
|
||||||
|
}
|
||||||
|
if (record.count < count) {
|
||||||
|
throw new ZError(12, 'not enough item')
|
||||||
|
}
|
||||||
|
record.count -= count
|
||||||
|
let data: ItemInfo[] = ItemCtrl.useItem(itemid, count)
|
||||||
|
await BagItem.addItems(accountid, data)
|
||||||
|
await record.save()
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
@router('post /api/:accountid/buyitem')
|
||||||
|
async buyItem(req: any) {
|
||||||
|
let { itemid, count, accountid } = req.params
|
||||||
|
let pinfo = ItemCtrl.getItemPrice(itemid << 0)
|
||||||
|
if (pinfo.id == 0) {
|
||||||
|
throw new ZError(10, 'item can`t buy')
|
||||||
|
}
|
||||||
|
let record = await BagItem.findOne({
|
||||||
|
accountid,
|
||||||
|
itemid: pinfo.id
|
||||||
|
})
|
||||||
|
if (!record || record.count < pinfo.count * count) {
|
||||||
|
throw new ZError(11, 'not enough money')
|
||||||
|
}
|
||||||
|
record.count -= pinfo.count * count
|
||||||
|
let newItem = (await BagItem.findOrCreate({ accountid, itemid }))
|
||||||
|
.doc
|
||||||
|
newItem.count += count
|
||||||
|
await newItem.save()
|
||||||
|
await record.save()
|
||||||
|
return {
|
||||||
|
itemid,
|
||||||
|
buynum: count,
|
||||||
|
totalnum: newItem.count,
|
||||||
|
priceid: pinfo.id,
|
||||||
|
pricenum: pinfo.count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@router('post /svr/:accountid/additem')
|
||||||
|
async addItem(req: any) {
|
||||||
|
let { itemid, count, accountid } = req.params
|
||||||
|
let itemInfo = ItemCtrl.findItem(itemid)
|
||||||
|
if (!itemInfo) {
|
||||||
|
throw new ZError(10, 'no item found')
|
||||||
|
}
|
||||||
|
let record = (await BagItem.findOrCreate({ accountid, itemid }))
|
||||||
|
.doc
|
||||||
|
record.count += count
|
||||||
|
record.itemtype = itemInfo._type
|
||||||
|
await record.save()
|
||||||
|
return record.toJson()
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user