From 3ecc971e8f89a31e2c067a0081962a246a1b4b2f Mon Sep 17 00:00:00 2001 From: code002lover Date: Tue, 2 Dec 2025 17:05:10 +0100 Subject: [PATCH] refactor personlist --- backend/src/main.rs | 10 +--- frontend/items.ts | 108 +++++++++++++++++++++++++++++++++++++++---- frontend/src/App.tsx | 3 +- 3 files changed, 101 insertions(+), 20 deletions(-) diff --git a/backend/src/main.rs b/backend/src/main.rs index 4f01f35..4e28c71 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -41,15 +41,7 @@ fn get_user( #[get("/")] fn get_users(_token: auth::Token, user_list: &rocket::State>) -> items::PersonList { items::PersonList { - person: user_list - .inner() - .iter() - .map(|u| { - let mut p = u.person.clone(); - p.opinion.clear(); - p - }) - .collect(), + person: user_list.inner().iter().map(|u| u.person.clone()).collect(), } } diff --git a/frontend/items.ts b/frontend/items.ts index 772557c..128ec85 100644 --- a/frontend/items.ts +++ b/frontend/items.ts @@ -48,7 +48,7 @@ export interface Person { } export interface Opinion { - game: Game | undefined; + title: string; wouldPlay: boolean; } @@ -97,6 +97,10 @@ export interface AuthStatusResponse { message: string; } +export interface GameRequest { + title: string; +} + function createBasePerson(): Person { return { name: "", opinion: [] }; } @@ -174,13 +178,13 @@ export const Person: MessageFns = { }; function createBaseOpinion(): Opinion { - return { game: undefined, wouldPlay: false }; + return { title: "", wouldPlay: false }; } export const Opinion: MessageFns = { encode(message: Opinion, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { - if (message.game !== undefined) { - Game.encode(message.game, writer.uint32(10).fork()).join(); + if (message.title !== "") { + writer.uint32(10).string(message.title); } if (message.wouldPlay !== false) { writer.uint32(16).bool(message.wouldPlay); @@ -200,7 +204,7 @@ export const Opinion: MessageFns = { break; } - message.game = Game.decode(reader, reader.uint32()); + message.title = reader.string(); continue; } case 2: { @@ -222,15 +226,15 @@ export const Opinion: MessageFns = { fromJSON(object: any): Opinion { return { - game: isSet(object.game) ? Game.fromJSON(object.game) : undefined, + title: isSet(object.title) ? globalThis.String(object.title) : "", wouldPlay: isSet(object.wouldPlay) ? globalThis.Boolean(object.wouldPlay) : false, }; }, toJSON(message: Opinion): unknown { const obj: any = {}; - if (message.game !== undefined) { - obj.game = Game.toJSON(message.game); + if (message.title !== "") { + obj.title = message.title; } if (message.wouldPlay !== false) { obj.wouldPlay = message.wouldPlay; @@ -243,7 +247,7 @@ export const Opinion: MessageFns = { }, fromPartial, I>>(object: I): Opinion { const message = createBaseOpinion(); - message.game = (object.game !== undefined && object.game !== null) ? Game.fromPartial(object.game) : undefined; + message.title = object.title ?? ""; message.wouldPlay = object.wouldPlay ?? false; return message; }, @@ -917,6 +921,64 @@ export const AuthStatusResponse: MessageFns = { }, }; +function createBaseGameRequest(): GameRequest { + return { title: "" }; +} + +export const GameRequest: MessageFns = { + encode(message: GameRequest, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { + if (message.title !== "") { + writer.uint32(10).string(message.title); + } + return writer; + }, + + decode(input: BinaryReader | Uint8Array, length?: number): GameRequest { + const reader = input instanceof BinaryReader ? input : new BinaryReader(input); + const end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseGameRequest(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: { + if (tag !== 10) { + break; + } + + message.title = reader.string(); + continue; + } + } + if ((tag & 7) === 4 || tag === 0) { + break; + } + reader.skip(tag & 7); + } + return message; + }, + + fromJSON(object: any): GameRequest { + return { title: isSet(object.title) ? globalThis.String(object.title) : "" }; + }, + + toJSON(message: GameRequest): unknown { + const obj: any = {}; + if (message.title !== "") { + obj.title = message.title; + } + return obj; + }, + + create, I>>(base?: I): GameRequest { + return GameRequest.fromPartial(base ?? ({} as any)); + }, + fromPartial, I>>(object: I): GameRequest { + const message = createBaseGameRequest(); + message.title = object.title ?? ""; + return message; + }, +}; + /** Authentication service */ export interface AuthService { Login(request: LoginRequest): Promise; @@ -954,6 +1016,34 @@ export class AuthServiceClientImpl implements AuthService { } } +export interface MainService { + GetGame(request: GameRequest): Promise; + AddOpinion(request: GameRequest): Promise; +} + +export const MainServiceServiceName = "items.MainService"; +export class MainServiceClientImpl implements MainService { + private readonly rpc: Rpc; + private readonly service: string; + constructor(rpc: Rpc, opts?: { service?: string }) { + this.service = opts?.service || MainServiceServiceName; + this.rpc = rpc; + this.GetGame = this.GetGame.bind(this); + this.AddOpinion = this.AddOpinion.bind(this); + } + GetGame(request: GameRequest): Promise { + const data = GameRequest.encode(request).finish(); + const promise = this.rpc.request(this.service, "GetGame", data); + return promise.then((data) => Game.decode(new BinaryReader(data))); + } + + AddOpinion(request: GameRequest): Promise { + const data = GameRequest.encode(request).finish(); + const promise = this.rpc.request(this.service, "AddOpinion", data); + return promise.then((data) => Person.decode(new BinaryReader(data))); + } +} + interface Rpc { request(service: string, method: string, data: Uint8Array): Promise; } diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 5456315..e9da0ab 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -60,8 +60,7 @@ function App() {
    {person.opinion.map((op, i) => (
  • - {op.game?.title} -{" "} - {op.wouldPlay ? "Would Play" : "Would Not Play"} + {op.title} - {op.wouldPlay ? "Would Play" : "Would Not Play"}
  • ))}