增加店铺保存游戏的功能

This commit is contained in:
zhl 2021-04-22 18:53:02 +08:00
parent 7593295128
commit 5fdf04b5b5
3 changed files with 73 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import BaseController from '../../common/base.controller'
import { permission, role, router } from '../../decorators/router'
import { Shop } from '../../models/shop/Shop'
import { ZError } from '../../common/ZError'
import { Game } from '../../models/content/Game'
class ShopController extends BaseController {
@ -51,6 +52,10 @@ class ShopController extends BaseController {
record = new Shop(req.params)
record.createdBy = user.id
record.show = true
let game = await Game.findAvaOne()
if (game) {
record.gameInfo = {gameid: game.id, versionid: game.versions[0]._id+'' }
}
} else {
record = await Shop.findById(_id)
record.updateFromReq(req.params)
@ -74,4 +79,44 @@ class ShopController extends BaseController {
})
return {}
}
@permission('shop:edit')
@router('post /shop/gameinfo/save')
async updateGameInfo(req: any) {
let { shopid, gameid, versionid } = req.params
if (!shopid || !gameid || !versionid) {
throw new ZError(11, 'params mismatch')
}
let shop = await Shop.findById(shopid)
if (!shop) {
throw new ZError(12, 'shop not found')
}
shop.gameInfo = { gameid, versionid }
await shop.save()
return {}
}
@permission('shop:edit')
@router('post /shop/gameinfo')
async getGameInfo(req: any) {
let { shopid } = req.params
if (!shopid) {
throw new ZError(11, 'params mismatch')
}
let shop = await Shop.findById(shopid)
if (!shop) {
throw new ZError(12, 'shop not found')
}
if (!shop.gameInfo) {
let game = await Game.findAvaOne()
if (game) {
shop.gameInfo = {gameid: game.id, versionid: game.versions[0]._id+'' }
await shop.save()
}
}
return {
gameid: shop.gameInfo.gameid,
versionid: shop.gameInfo.versionid
}
}
}

View File

@ -1,9 +1,15 @@
import { getModelForClass, modelOptions, prop } from '@typegoose/typegoose'
import {
getModelForClass,
modelOptions,
prop,
ReturnModelType
} from '@typegoose/typegoose'
import { dbconn } from '../../decorators/dbconn'
import { BaseModule } from '../Base'
import { noJson } from '../../decorators/nojson'
import { Base } from '@typegoose/typegoose/lib/defaultClasses'
export class GameVersion {
export class GameVersion extends Base{
/**
*
* 0: 网页
@ -55,7 +61,7 @@ class GameClass extends BaseModule {
public createdBy: string
public static parseQueryParam(params) {
let {key, timeBegin, timeEnd, shop} = params
let {key, timeBegin, timeEnd, shop, hasVersion} = params
let opt: any = {deleted: false}
if (key) {
opt.name = {$regex: key, $options: 'i'}
@ -70,10 +76,18 @@ class GameClass extends BaseModule {
} else if (!timeBegin && timeEnd) {
opt.createdAt = {$lte: timeEnd};
}
if (hasVersion) {
Object.assign(opt, {'versions.0': {$exists: true}})
}
let sort = {_id: -1}
let sort = {_id: 1}
return { opt, sort }
}
public static async findAvaOne(this: ReturnModelType<typeof GameClass>) {
return this.findOne({deleted: false, 'versions.0': {$exists: true} }).exec()
}
}
export const Game = getModelForClass(GameClass, { existingConnection: GameClass.db })

View File

@ -3,6 +3,13 @@ import { dbconn } from '../../decorators/dbconn'
import { noJson } from '../../decorators/nojson'
import { BaseModule } from '../Base'
class GameInfo {
@prop()
public gameid: string
@prop()
public versionid: string
}
@dbconn()
@modelOptions({ schemaOptions: { collection: 'shop', timestamps: true } })
@ -80,6 +87,9 @@ class ShopClass extends BaseModule {
@prop()
public extData: string
@prop()
public gameInfo?: GameInfo
public static parseQueryParam(params) {
let {key, timeBegin, timeEnd} = params