price_api/api.ts

57 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-08-25 13:51:57 -04:00
import { Application, Router } from 'https://deno.land/x/oak/mod.ts';
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
2023-08-30 09:10:39 -04:00
import { load } from "https://deno.land/std/dotenv/mod.ts";
2023-08-25 13:51:57 -04:00
const app = new Application();
const router = new Router();
const env = await load();
const hostname = env["HOSTNAME"];
const port = env["PORT"];
const user = env["USER"];
const password = env["PASSWORD"];
const database = env["DATABASE"];
const app_port = env["APP_PORT"];
// Configure database connection
const client = new Client({
hostname:hostname
,port: port
,user: user
,password:password
,database:database
});
await client.connect();
2023-08-30 08:30:04 -04:00
// Load SQL from file
const query = await Deno.readTextFile("sql/hist.sql");
// Define a route to retrieve values from the database using parameters
router.get('/price_info/part_cust/:partcode/:customer', async (ctx) => {
const partcode = ctx.params.partcode; // Extract the partcode parameter from the route
const customer = ctx.params.customer; // Extract the customer parameter from the route
//console.log(partcode)
//console.log(customer)
const result = await client.queryObject({args: [partcode, customer], text: query} );
2023-08-30 09:10:39 -04:00
for (const row of result.rows) {
if (typeof row.season === 'object' && row.season !== null) {
for (const year in row.season) {
console.log(row.season[year].price_usd)
}
}
}
2023-08-25 13:51:57 -04:00
});
app.use(router.routes());
app.use(router.allowedMethods());
// Start the server
console.log('Server is running on http://localhost:8085');
await app.listen({ port: 8085 });