work on the list call
This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
||||
Character,
|
||||
GetCharacterRequest,
|
||||
CreateCharacterRequest,
|
||||
ListCharacterRequest,
|
||||
ListCharacterResponse,
|
||||
} from './proto/character'
|
||||
|
||||
import {
|
||||
@@ -15,6 +17,15 @@ import {
|
||||
ICharacterManager,
|
||||
} from './proto/character.grpc-server'
|
||||
|
||||
const testCharacter: Character = {
|
||||
playerName: 'test player',
|
||||
characterName: 'test character',
|
||||
characterAlias: ['test alias'],
|
||||
version: 'test version',
|
||||
sourceTable: 'source table',
|
||||
json: 'test json',
|
||||
}
|
||||
|
||||
export class CharacterService {
|
||||
reflectionService: ReflectionService
|
||||
characterManager: ICharacterManager
|
||||
@@ -28,6 +39,7 @@ export class CharacterService {
|
||||
this.characterManager = {
|
||||
createCharacter: this.createCharacterCall.bind(this),
|
||||
getCharacter: this.getCharacterCall.bind(this),
|
||||
listCharacters: this.listCharactersCall.bind(this),
|
||||
}
|
||||
|
||||
this.databaseService = databaseService
|
||||
@@ -49,21 +61,27 @@ export class CharacterService {
|
||||
|
||||
console.log(newCharacter)
|
||||
|
||||
callback(null, {
|
||||
playerName: newCharacter.playerName,
|
||||
characterName: newCharacter.characterName,
|
||||
characterAlias: newCharacter.characterAlias,
|
||||
})
|
||||
callback(null, newCharacter)
|
||||
}
|
||||
|
||||
getCharacterCall(
|
||||
call: ServerUnaryCall<GetCharacterRequest, Character>,
|
||||
callback: sendUnaryData<Character>,
|
||||
) {
|
||||
callback(null, testCharacter)
|
||||
}
|
||||
|
||||
async listCharactersCall(
|
||||
call: ServerUnaryCall<ListCharacterRequest, ListCharacterResponse>,
|
||||
callback: sendUnaryData<ListCharacterResponse>,
|
||||
) {
|
||||
let characters = await this.databaseService.listCharacters(
|
||||
call.request.playerName,
|
||||
call.request.characterName,
|
||||
)
|
||||
|
||||
callback(null, {
|
||||
playerName: 'test player',
|
||||
characterName: 'test character',
|
||||
characterAlias: ['test alias'],
|
||||
characters,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,26 +16,47 @@ export class DatabaseService {
|
||||
return this.characterCollection
|
||||
.insertOne(character)
|
||||
.then((insertResult) => {
|
||||
console.log(insertResult)
|
||||
console.log("Adding new character record:", insertResult)
|
||||
return this.characterCollection.findOne({
|
||||
_id: insertResult.insertedId,
|
||||
})
|
||||
})
|
||||
.then(recordToCharacter)
|
||||
.then(record => {
|
||||
const character = recordToCharacter(record)
|
||||
console.log(`New character result: \n _id: ${record._id}`, character)
|
||||
return character
|
||||
})
|
||||
}
|
||||
|
||||
fetchCharactersForPlayer(playerName: string): Promise<Array<Character>> {
|
||||
return this.characterCollection
|
||||
.find({ playerName: playerName })
|
||||
.toArray()
|
||||
.then((results) =>
|
||||
results.map(recordToCharacter),
|
||||
)
|
||||
.then((results) => results.map(recordToCharacter))
|
||||
}
|
||||
|
||||
fetchCharactersForCharacterName(playerName: string): Array<Character> {
|
||||
return []
|
||||
}
|
||||
|
||||
listCharacters(
|
||||
playerName: string,
|
||||
characterName: string,
|
||||
): Promise<Array<Character>> {
|
||||
let query = {}
|
||||
|
||||
if (playerName != '') query['playerName'] = playerName
|
||||
if (characterName != '')
|
||||
query['$or'] = [
|
||||
{ characterName: characterName },
|
||||
{ characterAlias: characterName},
|
||||
]
|
||||
|
||||
return this.characterCollection
|
||||
.find(query)
|
||||
.toArray()
|
||||
.then((results) => results.map(recordToCharacter))
|
||||
}
|
||||
}
|
||||
|
||||
function recordToCharacter(record): Character {
|
||||
@@ -43,5 +64,8 @@ function recordToCharacter(record): Character {
|
||||
playerName: record.playerName,
|
||||
characterName: record.characterName,
|
||||
characterAlias: record.characterAlias,
|
||||
version: record.version,
|
||||
sourceTable: record.sourceTable,
|
||||
json: record.json,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user