work on the list call

This commit is contained in:
iamBadgers
2025-06-16 00:20:23 -07:00
parent 1f43ff468b
commit a62fe1aba9
3 changed files with 69 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ syntax = "proto3";
service CharacterManager { service CharacterManager {
rpc createCharacter(CreateCharacterRequest) returns (Character); rpc createCharacter(CreateCharacterRequest) returns (Character);
rpc getCharacter(GetCharacterRequest) returns (Character); rpc getCharacter(GetCharacterRequest) returns (Character);
rpc listCharacters(ListCharacterRequest) returns (ListCharacterResponse);
} }
message Empty {} message Empty {}
@@ -11,12 +12,22 @@ message Character {
string PlayerName = 1; string PlayerName = 1;
string CharacterName = 2; string CharacterName = 2;
repeated string CharacterAlias = 3; repeated string CharacterAlias = 3;
string Version = 4;
string SourceTable = 5;
string Json = 6;
} }
message CreateCharacterRequest { message CreateCharacterRequest {
Character characterData = 1; Character characterData = 1;
} }
message GetCharacterRequest { message GetCharacterRequest {}
Empty empty = 1;
} message ListCharacterRequest {
string PlayerName = 1;
string CharacterName = 2;
}
message ListCharacterResponse {
repeated Character characters = 1;
}

View File

@@ -8,6 +8,8 @@ import {
Character, Character,
GetCharacterRequest, GetCharacterRequest,
CreateCharacterRequest, CreateCharacterRequest,
ListCharacterRequest,
ListCharacterResponse,
} from './proto/character' } from './proto/character'
import { import {
@@ -15,6 +17,15 @@ import {
ICharacterManager, ICharacterManager,
} from './proto/character.grpc-server' } 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 { export class CharacterService {
reflectionService: ReflectionService reflectionService: ReflectionService
characterManager: ICharacterManager characterManager: ICharacterManager
@@ -28,6 +39,7 @@ export class CharacterService {
this.characterManager = { this.characterManager = {
createCharacter: this.createCharacterCall.bind(this), createCharacter: this.createCharacterCall.bind(this),
getCharacter: this.getCharacterCall.bind(this), getCharacter: this.getCharacterCall.bind(this),
listCharacters: this.listCharactersCall.bind(this),
} }
this.databaseService = databaseService this.databaseService = databaseService
@@ -49,21 +61,27 @@ export class CharacterService {
console.log(newCharacter) console.log(newCharacter)
callback(null, { callback(null, newCharacter)
playerName: newCharacter.playerName,
characterName: newCharacter.characterName,
characterAlias: newCharacter.characterAlias,
})
} }
getCharacterCall( getCharacterCall(
call: ServerUnaryCall<GetCharacterRequest, Character>, call: ServerUnaryCall<GetCharacterRequest, Character>,
callback: sendUnaryData<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, { callback(null, {
playerName: 'test player', characters,
characterName: 'test character',
characterAlias: ['test alias'],
}) })
} }
} }

View File

@@ -16,26 +16,47 @@ export class DatabaseService {
return this.characterCollection return this.characterCollection
.insertOne(character) .insertOne(character)
.then((insertResult) => { .then((insertResult) => {
console.log(insertResult) console.log("Adding new character record:", insertResult)
return this.characterCollection.findOne({ return this.characterCollection.findOne({
_id: insertResult.insertedId, _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>> { fetchCharactersForPlayer(playerName: string): Promise<Array<Character>> {
return this.characterCollection return this.characterCollection
.find({ playerName: playerName }) .find({ playerName: playerName })
.toArray() .toArray()
.then((results) => .then((results) => results.map(recordToCharacter))
results.map(recordToCharacter),
)
} }
fetchCharactersForCharacterName(playerName: string): Array<Character> { fetchCharactersForCharacterName(playerName: string): Array<Character> {
return [] 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 { function recordToCharacter(record): Character {
@@ -43,5 +64,8 @@ function recordToCharacter(record): Character {
playerName: record.playerName, playerName: record.playerName,
characterName: record.characterName, characterName: record.characterName,
characterAlias: record.characterAlias, characterAlias: record.characterAlias,
version: record.version,
sourceTable: record.sourceTable,
json: record.json,
} }
} }