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 {
rpc createCharacter(CreateCharacterRequest) returns (Character);
rpc getCharacter(GetCharacterRequest) returns (Character);
rpc listCharacters(ListCharacterRequest) returns (ListCharacterResponse);
}
message Empty {}
@@ -11,12 +12,22 @@ message Character {
string PlayerName = 1;
string CharacterName = 2;
repeated string CharacterAlias = 3;
string Version = 4;
string SourceTable = 5;
string Json = 6;
}
message CreateCharacterRequest {
Character characterData = 1;
}
message GetCharacterRequest {
Empty empty = 1;
message GetCharacterRequest {}
message ListCharacterRequest {
string PlayerName = 1;
string CharacterName = 2;
}
message ListCharacterResponse {
repeated Character characters = 1;
}

View File

@@ -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,
})
}
}

View File

@@ -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,
}
}