-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJobBand.bal
91 lines (85 loc) · 2.59 KB
/
JobBand.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
import ballerina/sql;
public type JobBand record {
int? id = ();
string name;
string description;
int level;
decimal min_salary;
decimal max_salary;
};
public isolated function getJobBands() returns JobBand[]|error {
JobBand[] jobBands = [];
stream<JobBand, error?> resultStream = smsDBClient->query(
`SELECT
id ,
name ,
description ,
level ,
min_salary ,
max_salary
FROM job_band`
);
check from JobBand jobBand in resultStream
do {
jobBands.push(jobBand);
};
check resultStream.close();
return jobBands;
}
public isolated function getJobBand(int id) returns JobBand|error {
JobBand jobBand = check smsDBClient->queryRow(
`SELECT * FROM job_band WHERE id = ${id}`
);
return jobBand;
}
public isolated function addJobBand(JobBand jobBand) returns int|error {
sql:ExecutionResult result = check smsDBClient->execute(`
INSERT INTO job_band (
name ,
description ,
level ,
min_salary ,
max_salary )
VALUES (
${jobBand.name},
${jobBand.description},
${jobBand.level},
${jobBand.min_salary},
${jobBand.max_salary}
)
`);
int|string? lastInsertId = result.lastInsertId;
if lastInsertId is int {
return lastInsertId;
} else {
return error("Unable to obtain last insert ID for job_band");
}
}
public isolated function updateJobBand(JobBand jobBand) returns int|error {
sql:ExecutionResult result = check smsDBClient->execute(`
UPDATE job_band SET
name = ${jobBand.name},
description = ${jobBand.description},
level = ${jobBand.level},
min_salary = ${jobBand.min_salary},
max_salary = ${jobBand.max_salary}
WHERE id = ${jobBand.id}
`);
int|string? affectedRowCount = result.affectedRowCount;
if affectedRowCount is int {
return affectedRowCount;
} else {
return error("Unable to obtain last affected count for job_band update");
}
}
isolated function deleteJobBand(int id) returns int|error {
sql:ExecutionResult result = check smsDBClient->execute(`
DELETE FROM job_band WHERE id = ${id}
`);
int? affectedRowCount = result.affectedRowCount;
if affectedRowCount is int {
return affectedRowCount;
} else {
return error("Unable to obtain the affected row count for job_band delete");
}
}