Once enabled, we will need to create a "space" to deploy our application.
const promise = require('bluebird')
const xsenv = require('@sap/xsenv')
//const env = require('../default-env.json')
const optionsDbPromise = {
promiseLib: promise
}
const pgp = require('pg-promise')(optionsDbPromise)
var conn_service = {}
try {
//let envCredentials = env.VCAP_SERVICES['my-postgre-db'][0].credentials
//xsenv.loadEnv() Only required for local testing and requires default-env.json file to be configured in root
const envCredentials = xsenv.getServices({
"my-post-db-02": { "tag": "my-post-db-02" }
})["my-post-db-02"]
conn_service.host = envCredentials.hostname
conn_service.port = envCredentials.port
conn_service.database = envCredentials.dbname
conn_service.user = envCredentials.username
conn_service.password = envCredentials.password
conn_service.ssl = {
rejectUnauthorized: false,
ca: envCredentials.sslrootcert
}
} catch (error) {
console.log('error')
}
console.log(conn_service)
const db = pgp(conn_service)
db.func('version')
.timeout(2000)
.then((data) => {
console.log('DB connection success ', data)
}).catch((error) => {
console.log(error)
})
module.exports = {
db,
pgp
}
const { db } = require("./dbConn")
const setupDB = (req, res, db, pgp) => {
const QueryFile = pgp.QueryFile
db.any(new QueryFile('./lib/sqlSetup.sql'), [])
.then(data => {
res.status(200).send({ "message": "DB setup successful" })
}).catch(error => {
res.status(500).send({ "message": error })
})
}
const getAllData = (req, res, db) => {
return db.any("select * from ORGANIZATION_MGT.USER_LIST")
.then(data => {
res.status(200).send({ "message": data })
}).catch(error => {
res.status(500).send({ "message": error })
})
}
const updateUserData = (req, res, db) => {
const id = req.params.id.toString()
const location = req.query.location
console.log('-------------------------------------------Request----------------------------------------')
console.log(req)
return db.any(`update ORGANIZATION_MGT.USER_LIST set baselocation='${location}' where id='${id}'`)
.then(data => {
res.status(200).send({ "message": `User ${id} base location updated successfully!` })
}).catch(error => {
res.status(500).send({ "message": error })
})
}
module.exports = {
setupDB,
getAllData,
updateUserData
}
CREATE SCHEMA IF NOT EXISTS ORGANIZATION_MGT;
DROP TABLE IF EXISTS ORGANIZATION_MGT.USER_LIST;
CREATE TABLE ORGANIZATION_MGT.USER_LIST( id VARCHAR(10) NOT NULL PRIMARY KEY, username VARCHAR(40), designation VARCHAR(20), baselocation VARCHAR(20) );
INSERT INTO ORGANIZATION_MGT.USER_LIST VALUES ( '835825', 'Archisman', 'SCON', 'Pune' ), ( '835826', 'Anuj', 'LCON', 'Nagpur' ), ( '835827', 'Vaishali', 'LCON', 'Pune' ), ( '835828', 'Ritwika', 'LCON', 'Pune' );
const express = require('express')
const cors = require('cors')
const { setupDB, getAllData, updateUserData } = require('./lib/apis')
const { db, pgp } = require('./lib/dbConn')
const app = express()
app.use(cors(), (req, res, next) => {
next()
})
app.use(express.json())
const port = process.env.PORT || 3000
app.post('/setup', (req, res) => {
setupDB(req, res, db, pgp)
})
app.get('/users', (req, res) => {
getAllData(req, res, db)
})
app.post('/users/:id', (req, res) => {
updateUserData(req, res, db)
})
app.listen(process.env.PORT || 3000, () => {
console.log(`App is running on port ${port}`)
})
applications:
- name: demo-node-app-01
memory: 512M
path: ./
buildpack: nodejs_buildpack
health-check-type: port
services:
- my-post-db-02
sap.ui.define([
'sap/ui/core/mvc/Controller',
'sap/ui/model/json/JSONModel',
'sap/m/MessageBox'
], function (Controller, JSONModel, MessageBox) {
"use strict";
var oController
return Controller.extend("Sample.Quickstart.controller.View1", {
onInit: async function () {
oController = this
const oUserModel = new JSONModel()
oController.getView().setModel(oUserModel, 'oUserModel')
const sUrl = `https://demo-node-app-01.cfapps.us10-001.hana.ondemand.com/users`
await oUserModel.loadData(sUrl, {}, true, 'GET')
},
_onUpdateLocation: async (oEvent) => {
const sPath = oEvent.getSource().getBindingContext('oUserModel').sPath
const oUserModel = oController.getView().getModel('oUserModel')
const id = oUserModel.getProperty(`${sPath}/id`)
const location = oUserModel.getProperty(`${sPath}/baselocation`)
try {
const oUserUpdateModel = new JSONModel()
const sUrl = `https://demo-node-app-01.cfapps.us10-001.hana.ondemand.com/users/${id}?location=${location}`
const oParams = {
location
}
await oUserUpdateModel.loadData(sUrl, oParams, true, 'POST')
MessageBox.success(oUserUpdateModel.getData()['message'])
} catch (error) {
MessageBox.error(error.message)
}
}
});
});
<mvc:View controllerName="Sample.Quickstart.controller.View1"
xmlns:mvc="sap.ui.core.mvc" displayBlock="true"
xmlns="sap.m">
<App id="app">
<pages>
<Page id="page" title="Org Data" titleAlignment="Center">
<content>
<Table items="{oUserModel>/message}">
<columns>
<Column>
<Label text="User Id" design="Bold"/>
</Column>
<Column>
<Label text="Name" design="Bold"/>
</Column>
<Column>
<Label text="Designation" design="Bold"/>
</Column>
<Column>
<Label text="Location" design="Bold"/>
</Column>
</columns>
<ColumnListItem>
<cells>
<Text text="{oUserModel>id}"/>
<Text text="{oUserModel>username}"/>
<Text text="{oUserModel>designation}"/>
<HBox>
<Input width="80%" value="{oUserModel>baselocation}"/>
<Button type="Emphasized" text="Udpdate" press="_onUpdateLocation"/>
</HBox>
</cells>
</ColumnListItem>
</Table>
</content>
</Page>
</pages>
</App>
</mvc:View>
applications:
- name: demo-ui5-app-01
memory: 512M
path: ./
buildpack: staticfile_buildpack
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
6 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |