重构代码, 将item相关接口独立出来

This commit is contained in:
zhl 2021-01-22 13:22:15 +08:00
parent 3f9641ad28
commit 14087f02db
2 changed files with 84 additions and 74 deletions

View File

@ -101,80 +101,6 @@ export default class AccountController extends BaseController {
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()
}
}

View 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()
}
}