-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.bal
181 lines (139 loc) · 6.12 KB
/
crud.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import ballerina/http;
import ballerina/log;
import ballerina/mysql;
import ballerina/sql;
import ballerina/io;
string dbUser = "root";
string dbPassword = "";
string dbName = "crud";
int serverPort = 9092;
type Customer record {|
int id;
string firstname;
string lastname;
string email;
|};
function initializeDatabase() returns sql:Error? {
mysql:Client mysqlClient = check new (user = dbUser, password = dbPassword);
sql:ExecuteResult? result = check mysqlClient->execute("CREATE DATABASE IF NOT EXISTS " + dbName);
io:println("Database created. ");
check mysqlClient.close();
}
function initializeTable(mysql:Client mysqlClient) returns sql:Error? {
sql:ExecuteResult? result = check mysqlClient->execute("CREATE TABLE IF NOT EXISTS customers" +
"(id INTEGER NOT NULL AUTO_INCREMENT, firstname VARCHAR(300)" +
",lastname VARCHAR(300), email VARCHAR(300), PRIMARY KEY (id))");
}
@http:ServiceConfig {
basePath: "/customers"
}
service customer on new http:Listener(serverPort) {
@http:ResourceConfig {
methods: ["GET"],
path: "/"
}
resource function getCustomers(http:Caller caller, http:Request req) {
mysql:Client|sql:Error mysqlClient = new (user = dbUser, password = dbPassword, database = dbName);
if (mysqlClient is mysql:Client) {
stream<record{}, error> resultStream = mysqlClient->query("Select * from customers", Customer);
stream<Customer, sql:Error> customerStream = <stream<Customer, sql:Error>>resultStream;
json[] array=[];
error? e = customerStream.forEach(function(Customer customer) {
json|error j = json.constructFrom(customer);
if (j is json) {
array.push(j);
}
});
var result = caller->respond(array);
if (result is error) {
log:printError("Error sending response", result);
}
sql:Error? err = mysqlClient.close();
} else {
io:println("MySQL Client initialization for querying data failed!", mysqlClient);
var result = caller->respond(mysqlClient.toString());
}
}
@http:ResourceConfig {
methods: ["POST"],
path: "/"
}
resource function addCustomers(http:Caller caller, http:Request req) {
error? initializeDatabaseResult = initializeDatabase();
mysql:Client|sql:Error mysqlClient = new (user = dbUser, password = dbPassword, database = dbName);
if (mysqlClient is mysql:Client) {
error? initializeTableResult = initializeTable(mysqlClient);
var customer = req.getJsonPayload();
if(customer is json){
json|error firstName = customer.firstname;
json|error lastName = customer.lastname;
json|error email = customer.email;
string sqlString = "('"+firstName.toString() + "','"+lastName.toString()+"','"+email.toString()+"');";
sql:ExecuteResult|()|error execute = mysqlClient->execute(<@untained> ("INSERT INTO customers (firstname,lastname,email) VALUES "+ sqlString));
var result = caller->respond("success");
if (result is error) {
log:printError("Error sending response", result);
}
}
sql:Error? err = mysqlClient.close();
} else {
io:println("MySQL Client initialization for querying data failed!", mysqlClient);
var result = caller->respond(mysqlClient.toString());
}
}
@http:ResourceConfig {
methods: ["PUT"],
path: "/"
}
resource function editCustomers(http:Caller caller, http:Request req) {
mysql:Client|sql:Error mysqlClient = new (user = dbUser, password = dbPassword, database = dbName);
if (mysqlClient is mysql:Client) {
var customer = req.getJsonPayload();
if(customer is json){
json|error id = customer.id;
json|error firstName = customer.firstname;
json|error lastName = customer.lastname;
json|error email = customer.email;
string sqlString = "UPDATE customers SET firstname='"+firstName.toString()+"',lastname='"+lastName.toString()+"',email='"+email.toString()+"' WHERE id="+id.toString();
sql:ExecuteResult|()|error execute = mysqlClient->execute(<@untained> (sqlString));
if (execute is error) {
io: println(execute.toString());
var respond = caller->respond("failed");
}
else{
var respond = caller->respond("success");
}
}
sql:Error? err = mysqlClient.close();
} else {
io:println("MySQL Client initialization for querying data failed!", mysqlClient);
var result = caller->respond(mysqlClient.toString());
}
}
@http:ResourceConfig {
methods: ["DELETE"],
path: "/"
}
resource function deleteCustomer(http:Caller caller, http:Request req) {
mysql:Client|sql:Error mysqlClient = new (user = dbUser, password = dbPassword, database = dbName);
if (mysqlClient is mysql:Client) {
var customer = req.getJsonPayload();
if(customer is json){
json|error id = customer.id;
string sqlString = "DELETE FROM customers WHERE id="+id.toString();
sql:ExecuteResult|()|error execute = mysqlClient->execute(<@untained> (sqlString));
if (execute is error) {
io: println(execute.toString());
var respond = caller->respond("failed");
}
else{
var respond = caller->respond("success");
}
}
sql:Error? err = mysqlClient.close();
} else {
io:println("MySQL Client initialization for querying data failed!", mysqlClient);
var result = caller->respond(mysqlClient.toString());
}
}
}