增加点赞和收藏相关api

This commit is contained in:
zhl 2021-12-15 13:48:25 +08:00
parent fb82166f64
commit d84a3c183d
3 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,112 @@
import BaseController from '../common/base.controller'
import { role, router } from '../decorators/router'
import { ZanInfo } from '../models/ZanInfo'
import { ZError } from '../common/ZError'
import { AccountCollect } from '../models/AccountCollect'
class GameExtController extends BaseController {
/**
*
*/
@role('anon')
@router('post /api/svr/zan/info')
async reqZanInfosList(req: any) {
let { accountId, games, type } = req.params
type = type || 'game'
let records = await ZanInfo.find({ recordId: { $in: games }, type })
let results: any = {}
for (let record of records) {
let data = record.toJson()
let did = 0
if (accountId) {
did = data.accounts.findIndex(o => o === accountId) > -1 ? 1 : 0
}
results[record.recordId] = { count: record.count, did }
}
return results
}
/**
*
*/
@role('anon')
@router('post /api/svr/zan/update')
async updateGameZan(req: any) {
let { accountId, game, type, act } = req.params
type = type || 'game'
if (!accountId) {
throw new ZError(11, 'accountId needed')
}
if (!game) {
throw new ZError(12, 'gameId needed')
}
let record = await ZanInfo.insertOrUpdate({ recordId: game, type }, {})
const index = record.accounts.indexOf(accountId)
if (act) {
if (index > -1) {
throw new ZError(13, 'already had')
}
record.count += 1
record.accounts.push(accountId)
await record.save()
} else {
if (index === -1) {
throw new ZError(13, 'not had')
}
record.count -= 1
record.accounts.splice(index, 1)
await record.save()
}
return { count: record.count }
}
/**
*
*/
@role('anon')
@router('post /api/svr/collect/list')
async collectList(req: any) {
let { accountId, type } = req.params
type = type || 'game'
let record = await AccountCollect.insertOrUpdate({ accountId }, {})
let results = []
for (let _d of record.games) {
if (_d.type === type) {
results.push(_d.recordId)
}
}
return results
}
/**
*
*/
@role('anon')
@router('post /api/svr/collect/update')
async updateCollect(req: any) {
let { accountId, game, type, act } = req.params
type = type || 'game'
if (!accountId) {
throw new ZError(11, 'accountId needed')
}
if (!game) {
throw new ZError(12, 'gameId needed')
}
let record = await AccountCollect.insertOrUpdate({ accountId }, {})
let index = record.games.findIndex(o => o.recordId === game && o.type === type)
let exists = index > -1
if (act) {
if (exists) {
throw new ZError(13, 'already add')
}
record.games.push({ recordId: game, type })
} else {
if (!exists) {
throw new ZError(13, 'not add')
}
record.games.splice(index, 1)
}
await record.save()
return {}
}
}

View File

@ -0,0 +1,41 @@
import { dbconn } from '../decorators/dbconn'
import { getModelForClass, index, modelOptions, prop, ReturnModelType } from '@typegoose/typegoose'
import { BaseModule } from './Base'
import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('2345678abcdefghjkmnpqrstwxy', 32)
const jsonExcludeKeys = ['updatedAt', '__v', '_id', 'createdAt', 'isBot', 'type']
class RecordInfo {
@prop()
public recordId: string
@prop()
public type: string
}
/**
*
*/
@dbconn()
@index({ accountId: 1 }, { unique: true })
@modelOptions({
schemaOptions: { collection: 'account_collect', timestamps: true },
})
class AccountCollectClass extends BaseModule {
@prop()
accountId: string
@prop({ type: () => [RecordInfo], default: [] })
games: RecordInfo[]
public toJson(): any {
let result: any = {}
// @ts-ignore
for (let key in this._doc) {
if (jsonExcludeKeys.indexOf(key) == -1) {
result[key] = this[key]
}
}
return result
}
}
export const AccountCollect = getModelForClass(AccountCollectClass, { existingConnection: AccountCollectClass.db })

40
src/models/ZanInfo.ts Normal file
View File

@ -0,0 +1,40 @@
import { dbconn } from '../decorators/dbconn'
import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose'
import { BaseModule } from './Base'
const jsonExcludeKeys = ['updatedAt', '__v', '_id', 'createdAt', 'isBot', 'type']
/**
* ,
*/
@dbconn()
@index({ recordId: 1, type: 1 }, { unique: true })
@modelOptions({
schemaOptions: { collection: 'zan_info', timestamps: true },
})
class ZanInfoClass extends BaseModule {
@prop()
recordId: string
@prop()
type: string
@prop({ default: 0 })
count: number
@prop({ type: () => [String], default: [] })
accounts: string[]
public toJson(): any {
let result: any = {}
// @ts-ignore
for (let key in this._doc) {
if (jsonExcludeKeys.indexOf(key) == -1) {
result[key] = this[key]
}
}
return result
}
}
export const ZanInfo = getModelForClass(ZanInfoClass, { existingConnection: ZanInfoClass.db })