sales_walk/api.ts

60 lines
1.8 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
2023-09-01 08:29:11 -04:00
const query = await Deno.readTextFile("sql/write_note.sql");
2023-08-30 08:30:04 -04:00
// Define a route to retrieve values from the database using parameters
2023-09-01 08:29:11 -04:00
router.get('/sales_walk/write_note/:ship_cust/:bucket/:notes', async (ctx) => {
2023-08-30 08:30:04 -04:00
2023-09-01 08:29:11 -04:00
const bucket = ctx.params.bucket; // Extract the bucket parameter from the route
const notes = ctx.params.notes; // Extract the bucket parameter from the route
const ship_cust = ctx.params.ship_cust; // Extract the ship_cust parameter from the route
2023-08-30 08:30:04 -04:00
2023-09-01 08:29:11 -04:00
//console.log(bucket)
//console.log(ship_cust)
2023-08-30 08:30:04 -04:00
2023-09-01 08:29:11 -04:00
const result = await client.queryObject({args: [ship_cust, bucket, notes], text: query} );
const currentTime = Date.now();
const dateObject = new Date(currentTime);
const currentDateTime = dateObject.toLocaleString(); //Output: 2/20/2023, 7:41:42 AM
console.log("posted: ", currentDateTime);
//ctx.response.body = result.rows;
ctx.response.body = currentDateTime;
2023-08-30 08:30:04 -04:00
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 });