Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Pure VPN - Up to 27 Months
vgp
Updated:
Friday at 8:10 AM
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
May 30, 2026
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Code Error eka hadaganna help..
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="SL MULTIMEDIA TUTORIAL" data-source="post: 28218787" data-attributes="member: 575597"><p>මේකට තමයි Code කරන්න ඕනේ</p><p></p><p><strong><u>Use case</u></strong></p><p></p><p>A retail shop needs to maintain a product stock and purchase orders through a web</p><p></p><p>application. The customers can submit their orders via the web application. As a backend developer, you</p><p></p><p>need to develop an API to register a customer in the system.</p><p></p><p>Below, please find a sample request and response JSON data for registering a customer.</p><p></p><p><strong>Sample Request JSON for registering a customer</strong></p><p></p><p><img src="https://open.uom.lk/pluginfile.php/86395/question/questiontext/356995/1/3547/use%20case.png" alt="request" class="fr-fic fr-dii fr-draggable " style="width: 475px; height: 309.429px" /></p><p></p><p><strong> Sample Response JSON for a successful registration</strong></p><p><img src="https://open.uom.lk/pluginfile.php/86395/question/questiontext/356995/1/3547/image%20%283%29.png" alt="response" class="fr-fic fr-dii fr-draggable " style="width: 423px; height: 180.692px" /></p><p><strong>customerId: This should be the generated id from the database</strong></p><p><strong></strong></p><p><strong>1. Update the database.js file with the required data definition (CREATE TABLE statement) to create the</strong></p><p></p><p>data table to store the data of customers</p><p>Table name: customer</p><p><strong>2. Implement a new API in the server.js file to register a customer and add the data record to the table.</strong></p><p></p><p>The method of the API should be POST</p><p>Include the proper error handling</p><p>HTTP error codes</p><p>Success: 201 Created</p><p>An Error: 400 Bad Request</p><p>Reference for HTTP error codes - <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" target="_blank">Link</a></p><p><strong>3. Implement validation to validate POST data to the same API as below</strong></p><p></p><p>Email address validation</p><p>Credit card number validation (12 digits)</p><p><strong>Resources:</strong></p><p>To test the expected outcome, you can pull the codebase from the below repository and edit the relevant files(database.js and server.js)</p><p>.</p><p></p><p>Git repository: <a href="https://github.com/opencourse5tutor/products-api" target="_blank">https://github.com/opencourse5tutor/products-api</a></p><p></p><p><u><strong>Answer Submission Instructions</strong></u></p><p></p><p>Students should submit the following files.</p><p></p><p></p><p></p><ol> <li data-xf-list-type="ol">A screenshot of a successful customer registration using the Postman tool.</li> <li data-xf-list-type="ol">database.js file</li> <li data-xf-list-type="ol">server.js file</li> </ol><p>mn code karapu Files tika</p><p></p><p>server.js</p><p></p><p>[CODE=javascript]var express = require("express");</p><p>var app = express();</p><p>var db = require("./database1.js");</p><p>var bodyParser = require("body-parser");</p><p>const { request, response } = require("express");</p><p>const res = require("express/lib/response");</p><p>app.use(bodyParser.json());</p><p></p><p>let HTTP_PORT = 8080;</p><p></p><p>app.listen(HTTP_PORT, () => {</p><p> console.log("Server is running on %PORT%".replace("%PORT%", HTTP_PORT))</p><p>});</p><p></p><p>app.post("/api/customers/", (req, res, next) => {</p><p></p><p> try {</p><p> var errors = []</p><p></p><p> if (!req.body) {</p><p> errors.push("An invalid input");</p><p> }</p><p></p><p> const {</p><p> name ,</p><p> address,</p><p> email ,</p><p> dateOfBirth ,</p><p> gender,</p><p> age ,</p><p> cardHolderName ,</p><p> cardNumber,</p><p> expiryDate,</p><p> cvv ,</p><p> timeStamp</p><p> } = req.body;</p><p> var sql = 'INSERT INTO customers (name,address,email,dateOfBirth,gender,age,cardHolderName,cardNumber,expiryDate,cvv,timeStamp) VALUES (?,?,?,?,?,?,?,?,?,?,?)'</p><p> var params = [name,address,email,dateOfBirth,gender,age,cardHolderName,cardNumber,expiryDate,cvv,timeStamp]</p><p> </p><p></p><p> db.run(sql, params, function (err, result) {</p><p></p><p> if (err) {</p><p> return;</p><p> } else {</p><p></p><p> res.json({</p><p> "message": "customer " + req.body.name +" registered",</p><p> "customerid": this.lastID</p><p> })</p><p> }</p><p></p><p> });</p><p> </p><p> }catch (E) {</p><p> res.status(400).send(E);</p><p> }</p><p> });</p><p></p><p>app.get("/api/customers/:customerid", (req, res, next) => {</p><p></p><p> try {</p><p> var sql = "select * from customers WHERE customerid =?"</p><p> var params = [req.params.customerid]</p><p> db.all(sql, params, (err, rows) => {</p><p> if (err) {</p><p> res.status(400).json({ "error": err.message });</p><p> return;</p><p> }</p><p> res.json({</p><p> "message": "success",</p><p> "data": rows</p><p> })</p><p> });</p><p> } catch (E) {</p><p> res.status(400).send(E);</p><p> }</p><p>});</p><p></p><p></p><p>app.get("/api/customers", (req, res, next) => {</p><p></p><p> try {</p><p> var sql = "select * from customers"</p><p> var params = []</p><p> db.all(sql, params, (err, rows) => {</p><p> if (err) {</p><p> res.status(400).json({ "error": err.message });</p><p> return;</p><p> }</p><p> res.json({</p><p> "message": "success",</p><p> "data": rows</p><p> })</p><p> });</p><p> } catch (E) {</p><p> res.status(400).send(E);</p><p> }</p><p>});</p><p></p><p>app.patch("/api/customers/:customerid", (req, res, next) => {</p><p> const {</p><p> name,</p><p> address,</p><p> email ,</p><p> dateOfBirth ,</p><p> gender,</p><p> age ,</p><p> cardHolderName ,</p><p> cardNumber,</p><p> expiryDate,</p><p> cvv ,</p><p> timeStamp</p><p> } = req.body;</p><p> db.run(`UPDATE customers set name =?,address=?,email=?,dateOfBirth=?,gender=?,age=?,cardHolderName=?,cardNumber=?,expiryDate=?,cvv=?,timeStamp=? WHERE customerid = ?`,</p><p> [name,address,email,dateOfBirth,gender,age,cardHolderName,cardNumber,expiryDate,cvv,timeStamp,req.params.customerid], </p><p> function (err, result) {</p><p> if (err) {</p><p> res.status(400).json({ "error": res.message })</p><p> return;</p><p> }</p><p> res.status(200).json({ "updated customerid": req.params.customerid })</p><p> });</p><p>});</p><p></p><p>app.delete("/api/customers/:customerid", (req, res, next) => {</p><p> try {</p><p> db.run('DELETE FROM customers WHERE customerid = ?',</p><p> req.params.customerid,</p><p> function (err, result) {</p><p> if (err) {</p><p> res.status(400).json({ "error": res.message })</p><p> return;</p><p> }</p><p> res.json({ "message": "deleted customer id "+ req.params.customerid})</p><p> });</p><p> } catch (E) {</p><p> res.status(400).send(E)</p><p> }</p><p></p><p>}); [/CODE]</p><p></p><p></p><p>database.js </p><p></p><p>[CODE=javascript]var sqlite3 = require('sqlite3').verbose();</p><p>var md5 = require('md5');</p><p></p><p>const DBSOURCE = 'db.sqlite';</p><p></p><p>let db = new sqlite3.Database(DBSOURCE, (err) => {</p><p> if (err) {</p><p> // Cannot open database</p><p> console.error(err.message);</p><p> throw err;</p><p> } else {</p><p> console.log('Connected to the SQlite database.');</p><p> db.run(</p><p> `CREATE TABLE customers (</p><p> id INTEGER PRIMARY KEY AUTOINCREMENT,</p><p> customerName text,</p><p> address text,</p><p> email VARCHAR,</p><p> dateofBirth VARCHAR,</p><p> gender VARCHAR,</p><p> age INTEGER,</p><p> cardHolderName text,</p><p> expirytdate VARCHAR,</p><p> cvv INTEGER,</p><p> timpStamp text</p><p> )`,</p><p> (err) => {</p><p> if (err) {</p><p> // Table already created</p><p> } else {</p><p> // Table just created, creating some rows</p><p> var insert =</p><p> ' INSERT INTO customers (customerName,address, email, dateofBirth, gender, age,cardHolderName,expirytdate,cvv , timpStamp ) VALUES (?,?,?,?,?,?,?,?,?,?)';</p><p> db.run(insert, [</p><p> </p><p> 'Jems Watson',</p><p> 'No 324/A ra de Mel Road , kadawataha',</p><p> 'watsin34@gamail.com',</p><p> '2002.02.25',</p><p> 'male',</p><p> '20',</p><p> 'J.watson',</p><p> '425678588956',</p><p> '12/2026',</p><p> '245',</p><p> '2022-12-31 23:59:59',</p><p> ]);</p><p> }</p><p> }</p><p> );</p><p> }</p><p>});</p><p>[/CODE]</p><p></p><p></p><p>mn request damme Insomnia eken mehema error ekak enawa . <img src="/styles/default/xenforo/smilies/default/sorry.gif" class="smilie" loading="lazy" alt=":sorry:" title="Sorry :sorry:" data-shortname=":sorry:" /></p><p></p><p><img src="https://telegra.ph/file/6b49d8883ec8f892f5efa.jpg" alt="" class="fr-fic fr-dii fr-draggable " style="width: 758px" /></p></blockquote><p></p>
[QUOTE="SL MULTIMEDIA TUTORIAL, post: 28218787, member: 575597"] මේකට තමයි Code කරන්න ඕනේ [B][U]Use case[/U][/B] A retail shop needs to maintain a product stock and purchase orders through a web application. The customers can submit their orders via the web application. As a backend developer, you need to develop an API to register a customer in the system. Below, please find a sample request and response JSON data for registering a customer. [B]Sample Request JSON for registering a customer[/B] [IMG width="475px" height="309.429px" alt="request"]https://open.uom.lk/pluginfile.php/86395/question/questiontext/356995/1/3547/use%20case.png[/IMG] [B] Sample Response JSON for a successful registration[/B] [IMG width="423px" height="180.692px" alt="response"]https://open.uom.lk/pluginfile.php/86395/question/questiontext/356995/1/3547/image%20%283%29.png[/IMG] [B]customerId: This should be the generated id from the database 1. Update the database.js file with the required data definition (CREATE TABLE statement) to create the[/B] data table to store the data of customers Table name: customer [B]2. Implement a new API in the server.js file to register a customer and add the data record to the table.[/B] The method of the API should be POST Include the proper error handling HTTP error codes Success: 201 Created An Error: 400 Bad Request Reference for HTTP error codes - [URL='https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html']Link[/URL] [B]3. Implement validation to validate POST data to the same API as below[/B] Email address validation Credit card number validation (12 digits) [B]Resources:[/B] To test the expected outcome, you can pull the codebase from the below repository and edit the relevant files(database.js and server.js) . Git repository: [URL]https://github.com/opencourse5tutor/products-api[/URL] [U][B]Answer Submission Instructions[/B][/U] Students should submit the following files. [LIST=1] [*]A screenshot of a successful customer registration using the Postman tool. [*]database.js file [*]server.js file [/LIST] mn code karapu Files tika server.js [CODE=javascript]var express = require("express"); var app = express(); var db = require("./database1.js"); var bodyParser = require("body-parser"); const { request, response } = require("express"); const res = require("express/lib/response"); app.use(bodyParser.json()); let HTTP_PORT = 8080; app.listen(HTTP_PORT, () => { console.log("Server is running on %PORT%".replace("%PORT%", HTTP_PORT)) }); app.post("/api/customers/", (req, res, next) => { try { var errors = [] if (!req.body) { errors.push("An invalid input"); } const { name , address, email , dateOfBirth , gender, age , cardHolderName , cardNumber, expiryDate, cvv , timeStamp } = req.body; var sql = 'INSERT INTO customers (name,address,email,dateOfBirth,gender,age,cardHolderName,cardNumber,expiryDate,cvv,timeStamp) VALUES (?,?,?,?,?,?,?,?,?,?,?)' var params = [name,address,email,dateOfBirth,gender,age,cardHolderName,cardNumber,expiryDate,cvv,timeStamp] db.run(sql, params, function (err, result) { if (err) { return; } else { res.json({ "message": "customer " + req.body.name +" registered", "customerid": this.lastID }) } }); }catch (E) { res.status(400).send(E); } }); app.get("/api/customers/:customerid", (req, res, next) => { try { var sql = "select * from customers WHERE customerid =?" var params = [req.params.customerid] db.all(sql, params, (err, rows) => { if (err) { res.status(400).json({ "error": err.message }); return; } res.json({ "message": "success", "data": rows }) }); } catch (E) { res.status(400).send(E); } }); app.get("/api/customers", (req, res, next) => { try { var sql = "select * from customers" var params = [] db.all(sql, params, (err, rows) => { if (err) { res.status(400).json({ "error": err.message }); return; } res.json({ "message": "success", "data": rows }) }); } catch (E) { res.status(400).send(E); } }); app.patch("/api/customers/:customerid", (req, res, next) => { const { name, address, email , dateOfBirth , gender, age , cardHolderName , cardNumber, expiryDate, cvv , timeStamp } = req.body; db.run(`UPDATE customers set name =?,address=?,email=?,dateOfBirth=?,gender=?,age=?,cardHolderName=?,cardNumber=?,expiryDate=?,cvv=?,timeStamp=? WHERE customerid = ?`, [name,address,email,dateOfBirth,gender,age,cardHolderName,cardNumber,expiryDate,cvv,timeStamp,req.params.customerid], function (err, result) { if (err) { res.status(400).json({ "error": res.message }) return; } res.status(200).json({ "updated customerid": req.params.customerid }) }); }); app.delete("/api/customers/:customerid", (req, res, next) => { try { db.run('DELETE FROM customers WHERE customerid = ?', req.params.customerid, function (err, result) { if (err) { res.status(400).json({ "error": res.message }) return; } res.json({ "message": "deleted customer id "+ req.params.customerid}) }); } catch (E) { res.status(400).send(E) } }); [/CODE] database.js [CODE=javascript]var sqlite3 = require('sqlite3').verbose(); var md5 = require('md5'); const DBSOURCE = 'db.sqlite'; let db = new sqlite3.Database(DBSOURCE, (err) => { if (err) { // Cannot open database console.error(err.message); throw err; } else { console.log('Connected to the SQlite database.'); db.run( `CREATE TABLE customers ( id INTEGER PRIMARY KEY AUTOINCREMENT, customerName text, address text, email VARCHAR, dateofBirth VARCHAR, gender VARCHAR, age INTEGER, cardHolderName text, expirytdate VARCHAR, cvv INTEGER, timpStamp text )`, (err) => { if (err) { // Table already created } else { // Table just created, creating some rows var insert = ' INSERT INTO customers (customerName,address, email, dateofBirth, gender, age,cardHolderName,expirytdate,cvv , timpStamp ) VALUES (?,?,?,?,?,?,?,?,?,?)'; db.run(insert, [ 'Jems Watson', 'No 324/A ra de Mel Road , kadawataha', 'watsin34@gamail.com', '2002.02.25', 'male', '20', 'J.watson', '425678588956', '12/2026', '245', '2022-12-31 23:59:59', ]); } } ); } }); [/CODE] mn request damme Insomnia eken mehema error ekak enawa . :sorry: [IMG width="758px"]https://telegra.ph/file/6b49d8883ec8f892f5efa.jpg[/IMG] [/QUOTE]
Insert quotes…
Verification
Payakata winadi keeyak tibeda?
Post reply
Top
Bottom