Get game config endpoint

This commit is contained in:
Joseph Ferano 2023-04-14 20:17:24 +07:00
parent 46580c800e
commit 2f809bf889
3 changed files with 67 additions and 0 deletions

View File

@ -119,6 +119,47 @@ begin
end; end;
$$ language plpgsql; $$ language plpgsql;
create or replace function get_game_config()
returns table (
resources json,
"gameConstants" json,
"stakingSources" json,
"storeItems" json
)
as $$
begin
select json_agg(name) into resources from resource;
select json_object_agg(key, value) into "gameConstants" from game_constants;
select json_agg(staking_source_item) into "stakingSources" from staking_source_item;
select json_agg(
json_build_object(
'id', store_item.id,
'name', store_item.name,
'description', store_item.description,
'price', store_item.price,
'image', store_item.image_name,
'claimAmount', store_item.claim_amount,
'completionTimeInMins', store_item.completion_time_in_mins,
'upgrades', upgrades
) order by store_item.id
) into "storeItems"
from store_item
join (
select store_item_id, json_agg(
json_build_object(
'tier', tier,
'price', upgrade_item.price,
'claimBoost', claim_boost
) order by upgrade_item.tier
) as upgrades
from upgrade_item
group by store_item_id
) u on u.store_item_id = store_item.id;
return next;
end;
$$ language plpgsql;
create or replace function get_staking_sources(user_id uuid) create or replace function get_staking_sources(user_id uuid)
returns table ( returns table (
id uuid, id uuid,

View File

@ -0,0 +1,22 @@
import { postgresConnection } from "db";
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
if (req.method === "GET") {
const db = postgresConnection;
const result = await db.query("select * from get_game_config()");
if (result.rowCount > 0) {
return res.status(200).json(result.rows[0]);
} else {
return res.status(404).json({ message: "Something went wrong" });
}
}
} catch (error) {
res.status(500).json(error);
}
}

View File

@ -44,6 +44,10 @@ POST http://localhost:3000/api/user/:user_id/clear-data
GET http://localhost:3000/api/leaderboards GET http://localhost:3000/api/leaderboards
:headers :headers
# Get game config
GET http://localhost:3000/api/get-game-config
:headers
# Get Staking Sources # Get Staking Sources
GET http://localhost:3000/api/user/:user_id/staking-sources GET http://localhost:3000/api/user/:user_id/staking-sources
:headers :headers