Service
With Netuno you can create Web Services and build any kind of complex APIs REST.
Services (Web Services) are used to perform online data integration, where one web system can exchange information with another.
It also provide data to the frontend (web pages) asynchronously, allowing the frontend to load the visual part of the web page faster and then get the dynamic information through integration with services (Web Services).
As will not wait for the whole page to load instead the server will quickly send the structure of the page that will be presented and processed dynamic data gradually, delivering the best User Experience.
The Editorโ
To create a service on Netuno you will need a code editor.
Is recommended Visual Studio Code as is a freeware source-code editor, fast, quite simple and intuitive.
However, if you choose any other code editor it will work in a similar way.
How to Open Demo Applicationโ
In Visual Studio Code by clicking on the left on the first file where you have the option Open Folder.
Browse the file system to the folder where you have Netuno installed.
Inside the Netuno root folder find the folder:
๐ /apps/demo
And when then inside the folder demo click on Open.
Note that the folder and file structure of the application demo has been loaded on the left-hand side.
Creating a Serviceโ
The services are processed by the server, so to create a service you must expand the folders and go to the folder:
๐ server/services
Now just right-click on the ๐ services
folder and choose the New File option.
The name of the new file should be:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
server/services/workers.js
server/services/workers.py
server/services/workers.rb
server/services/workers.kts
server/services/workers.groovy
All you need to do now is to Code our service, which will be as below:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
/**
*** Transforms query data into JSON output.
*/
const dbRecords = _db.query(`
SELECT DISTINCT
worker.name, SUM(DATEDIFF(HOUR, record.start, record.end)) AS total
FROM worker INNER JOIN record
ON worker.id = record.worker_id
WHERE worker.active = true AND record.active = true
GROUP BY worker.name
ORDER BY total ASC
`);
const list = _val.list();
for (const dbRecord of dbRecords) {
list.add(
_val.map()
.set("name", dbRecord.getString("name"))
.set("total", dbRecord.getInt("total"))
);
}
_out.json(list);
#
# Transforms query data into JSON output.
#
dbRecords = _db.query(
"""SELECT DISTINCT
worker.name, SUM(DATEDIFF(HOUR, record.start, record.end)) AS total
FROM worker INNER JOIN record
ON worker.id = record.worker_id
WHERE worker.active = true AND record.active = true
GROUP BY worker.name
ORDER BY total ASC"""
)
list = _val.list()
for dbRecord in dbRecords:
list.add(
_val.map()
.set("name", dbRecord.getString("name"))
.set("total", dbRecord.getInt("total"))
)
_out.json(list)
#
# Transforms query data into JSON output.
#
dbRecords = _db.query(%{
SELECT DISTINCT
worker.name, SUM(DATEDIFF(HOUR, record.start, record.end)) AS total
FROM worker INNER JOIN record
ON worker.id = record.worker_id
WHERE worker.active = true AND record.active = true
GROUP BY worker.name
ORDER BY total ASC
})
list = _val.list()
dbRecords.each do |dbRecord|
list.add(
_val.map()
.set("name", dbRecord.getString("name"))
.set("total", dbRecord.getInt("total"))
)
end
_out.json(list)
/**
*** Transforms query data into JSON output.
*/
val dbRecords = _db.query(
"""SELECT DISTINCT
worker.name, SUM(DATEDIFF(HOUR, record.start, record.end)) AS total
FROM worker INNER JOIN record
ON worker.id = record.worker_id
WHERE worker.active = true AND record.active = true
GROUP BY worker.name
ORDER BY total ASC"""
);
val list = _val.list();
dbRecords.forEach {
list.add(
_val.map()
.set("name", it.getString("name"))
.set("total", it.getInt("total"))
);
}
_out.json(list);
/**
*** Transforms query data into JSON output.
*/
def dbRecords = _db.query("""\
SELECT DISTINCT
worker.name, SUM(DATEDIFF(HOUR, record.start, record.end)) AS total
FROM worker INNER JOIN record
ON worker.id = record.worker_id
WHERE worker.active = true AND record.active = true
GROUP BY worker.name
ORDER BY total ASC
""");
def list = _val.list();
for (dbRecord in dbRecords) {
list.add(
_val.map()
.set("name", dbRecord.getString("name"))
.set("total", dbRecord.getInt("total"))
);
}
_out.json(list);
Make sure there is data loaded in the Tasks and that they are related to the Registry, otherwise the result will be empty.
The _db.query
receives a string with a database query and performs its execution by transforming the obtained data into a list of objects of type key/value, i.e. column/data.
The _out.json
performs the output (data output) for the browser type ContentType: application/json
and formats the past object to be structured as an object JSON.
Run the Serviceโ
To run the service just open in the browser the address of the service that was created which will be:
In this case we recommend the Firefox browser because it allows a better visualization of the JSON object.