price_api/api.ts

60 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";
const app = new Application();
const router = new Router();
2023-10-04 08:37:51 -04:00
//---------dotenv info-------------
import { load } from "https://deno.land/std/dotenv/mod.ts";
2023-08-25 13:51:57 -04:00
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"];
2023-10-04 08:37:51 -04:00
2023-08-25 13:51:57 -04:00
// Configure database connection
const client = new Client({
hostname:hostname
,port: port
,user: user
,password:password
,database:database
2023-10-04 08:37:51 -04:00
,applicationName: "pricing guidance"
2023-08-25 13:51:57 -04:00
});
await client.connect();
2023-08-30 08:30:04 -04:00
// Load SQL from file
2023-11-03 14:34:27 -04:00
const query = await Deno.readTextFile("sql/get.pg.sql");
2023-08-30 08:30:04 -04:00
2023-11-03 21:16:26 -04:00
function apply_guidance(doc: any) {
return doc;
}
2023-08-30 08:30:04 -04:00
// Define a route to retrieve values from the database using parameters
2023-11-03 16:11:32 -04:00
router.get('/code_price/:billcode/:shipcode/:partcode/:qty', async (ctx) => {
2023-08-30 08:30:04 -04:00
2023-11-03 16:11:32 -04:00
const partcode = ctx.params.partcode;
const billcode = ctx.params.billcode;
const shipcode = ctx.params.shipcode;
const qty = ctx.params.qty;
2023-08-30 08:30:04 -04:00
//console.log(partcode)
//console.log(customer)
2023-11-03 16:11:32 -04:00
const result = await client.queryObject({args: [billcode, shipcode, partcode, qty], text: query} );
2023-11-03 21:16:26 -04:00
const procd = apply_guidance(result.rows[0]["doc"])
ctx.response.body = procd
2023-08-25 13:51:57 -04:00
});
app.use(router.routes());
app.use(router.allowedMethods());
// Start the server
2023-10-04 08:37:51 -04:00
console.log('Server is running on http://usmidsap02:8090');
await app.listen({ port: 8090 });
2023-08-25 13:51:57 -04:00