forked from andyoram/ballerina_report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.bal
73 lines (68 loc) · 2.57 KB
/
inventory.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import ballerina/http;
import ballerina/log;
import ballerinax/java.jdbc;
import ballerina/io;
import ballerina/'lang\.int as integer;
type Inventory record {
int productId;
int stock;
};
@http:ServiceConfig {
basePath: "/InventoryService"
}
service InventoryService on new http:Listener(9093) {
@http:ResourceConfig {
methods: ["GET"],
path: "/checkInventory"
}
resource function getProduct(http:Caller outboundEP, http:Request req) returns error? {
string? qParam = req.getQueryParamValue("productId");
if (qParam is ()) {
log:printError("exected query parameter productId.");
respond(outboundEP, "exected query parameter productId.", statusCode = 400);
return;
}
int | error productId = integer:fromString(<string>qParam);
if (productId is int && productId > 0) {
var inventory = checkInventoryForStock(productId);
if (inventory is error) {
log:printError("error in retrieving inventory details.", err = inventory);
respond(outboundEP, "error in retrieving inventory details.", statusCode = 500);
return;
} else {
json payload = check json.constructFrom(inventory);
respond(outboundEP, <@untainted> payload);
}
} else {
log:printError("invalid input query parameter. expected a positive integer.");
respond(outboundEP, "invalid input query parameter. expected a positive integer.", statusCode = 400);
}
}
}
jdbc:Client clientDB = new({
url: "jdbc:mysql://localhost:3306/testdb",
username: "root",
password: "root",
poolOptions: { maximumPoolSize: 5 },
dbOptions: { useSSL: false }
});
function checkInventoryForStock(int id) returns Inventory | error {
jdbc:Parameter param = {
sqlType: jdbc:TYPE_INTEGER,
value: id
};
var result = clientDB->select("SELECT productId, stock FROM INVENTORY WHERE productId = ?", Inventory, param);
table<Inventory> dataTable = check result;
Inventory inventory = <Inventory> dataTable.getNext();
dataTable.close();
return <@untainted> inventory;
}
function respond(http:Caller outboundEP, json | string payload, int statusCode = 200) {
http:Response res = new;
res.statusCode = statusCode;
res.setJsonPayload(payload, contentType = "application/json");
error? responseStatus = outboundEP->respond(res);
if (responseStatus is error) {
log:printError("error in sending response.", err = responseStatus);
}
}