-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the entity records, DB CRUD and REST CRUD
- Loading branch information
1 parent
d7fda49
commit 7ad99d5
Showing
47 changed files
with
5,109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import ballerina/sql; | ||
|
||
public type Address record { | ||
int? id = (); | ||
string line1; | ||
string line2; | ||
string line3; | ||
int city_id; | ||
int address_type_id; | ||
string notes; | ||
}; | ||
|
||
public isolated function getAddresses() returns Address[]|error { | ||
Address[] addresses = []; | ||
stream<Address, error?> resultStream = smsDBClient->query( | ||
`SELECT | ||
id , | ||
line1 , | ||
line2 , | ||
line3 , | ||
city_id , | ||
address_type_id , | ||
notes | ||
FROM address` | ||
); | ||
check from Address address in resultStream | ||
do { | ||
addresses.push(address); | ||
}; | ||
check resultStream.close(); | ||
return addresses; | ||
} | ||
|
||
public isolated function getAddress(int id) returns Address|error { | ||
Address address = check smsDBClient->queryRow( | ||
`SELECT * FROM address WHERE id = ${id}` | ||
); | ||
return address; | ||
} | ||
|
||
public isolated function addAddress(Address address) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
INSERT INTO address ( | ||
line1 , | ||
line2 , | ||
line3 , | ||
city_id , | ||
address_type_id , | ||
notes ) | ||
VALUES ( | ||
${address.line1}, | ||
${address.line2}, | ||
${address.line3}, | ||
${address.city_id}, | ||
${address.address_type_id}, | ||
${address.notes} | ||
) | ||
`); | ||
int|string? lastInsertId = result.lastInsertId; | ||
if lastInsertId is int { | ||
return lastInsertId; | ||
} else { | ||
return error("Unable to obtain last insert ID for address"); | ||
} | ||
} | ||
|
||
public isolated function updateAddress(Address address) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
UPDATE address SET | ||
line1 = ${address.line1}, | ||
line2 = ${address.line2}, | ||
line3 = ${address.line3}, | ||
city_id = ${address.city_id}, | ||
address_type_id = ${address.address_type_id}, | ||
notes = ${address.notes} | ||
WHERE id = ${address.id} | ||
`); | ||
int|string? affectedRowCount = result.affectedRowCount; | ||
if affectedRowCount is int { | ||
return affectedRowCount; | ||
} else { | ||
return error("Unable to obtain last affected count for address update"); | ||
} | ||
} | ||
|
||
isolated function deleteAddress(int id) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
DELETE FROM address WHERE id = ${id} | ||
`); | ||
int? affectedRowCount = result.affectedRowCount; | ||
if affectedRowCount is int { | ||
return affectedRowCount; | ||
} else { | ||
return error("Unable to obtain the affected row count for address delete"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import ballerina/sql; | ||
|
||
import ballerina/time; | ||
|
||
public type Applicant record { | ||
int? id = (); | ||
string positions_vacant_id; | ||
string first_name; | ||
string last_name; | ||
string name_with_initials; | ||
string full_name; | ||
string gender; | ||
string applied_date; | ||
string id_number; | ||
string phone_number1; | ||
string? phone_number2 = (); | ||
string? email = (); | ||
string? cv_location = (); | ||
time:Utc last_updated = time:utcNow(); | ||
}; | ||
|
||
public isolated function getApplicants() returns Applicant[]|error { | ||
Applicant[] applicants = []; | ||
stream<Applicant, error?> resultStream = smsDBClient->query( | ||
`SELECT | ||
id , | ||
positions_vacant_id , | ||
first_name , | ||
last_name , | ||
name_with_initials , | ||
full_name , | ||
gender , | ||
applied_date , | ||
id_number , | ||
phone_number1 , | ||
phone_number2 , | ||
email , | ||
cv_location , | ||
last_updated | ||
FROM applicant` | ||
); | ||
check from Applicant applicant in resultStream | ||
do { | ||
applicants.push(applicant); | ||
}; | ||
check resultStream.close(); | ||
return applicants; | ||
} | ||
|
||
public isolated function getApplicant(int id) returns Applicant|error { | ||
Applicant applicant = check smsDBClient->queryRow( | ||
`SELECT * FROM applicant WHERE id = ${id}` | ||
); | ||
return applicant; | ||
} | ||
|
||
public isolated function addApplicant(Applicant applicant) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
INSERT INTO applicant ( | ||
positions_vacant_id , | ||
first_name , | ||
last_name , | ||
name_with_initials , | ||
full_name , | ||
gender , | ||
applied_date , | ||
id_number , | ||
phone_number1 , | ||
phone_number2 , | ||
email , | ||
cv_location , | ||
last_updated ) | ||
VALUES ( | ||
${applicant.positions_vacant_id}, | ||
${applicant.first_name}, | ||
${applicant.last_name}, | ||
${applicant.name_with_initials}, | ||
${applicant.full_name}, | ||
${applicant.gender}, | ||
${applicant.applied_date}, | ||
${applicant.id_number}, | ||
${applicant.phone_number1}, | ||
${applicant.phone_number2}, | ||
${applicant.email}, | ||
${applicant.cv_location}, | ||
${applicant.last_updated} | ||
) | ||
`); | ||
int|string? lastInsertId = result.lastInsertId; | ||
if lastInsertId is int { | ||
return lastInsertId; | ||
} else { | ||
return error("Unable to obtain last insert ID for applicant"); | ||
} | ||
} | ||
|
||
public isolated function updateApplicant(Applicant applicant) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
UPDATE applicant SET | ||
positions_vacant_id = ${applicant.positions_vacant_id}, | ||
first_name = ${applicant.first_name}, | ||
last_name = ${applicant.last_name}, | ||
name_with_initials = ${applicant.name_with_initials}, | ||
full_name = ${applicant.full_name}, | ||
gender = ${applicant.gender}, | ||
applied_date = ${applicant.applied_date}, | ||
id_number = ${applicant.id_number}, | ||
phone_number1 = ${applicant.phone_number1}, | ||
phone_number2 = ${applicant.phone_number2}, | ||
email = ${applicant.email}, | ||
cv_location = ${applicant.cv_location}, | ||
last_updated = ${applicant.last_updated} | ||
WHERE id = ${applicant.id} | ||
`); | ||
int|string? affectedRowCount = result.affectedRowCount; | ||
if affectedRowCount is int { | ||
return affectedRowCount; | ||
} else { | ||
return error("Unable to obtain last affected count for applicant update"); | ||
} | ||
} | ||
|
||
isolated function deleteApplicant(int id) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
DELETE FROM applicant WHERE id = ${id} | ||
`); | ||
int? affectedRowCount = result.affectedRowCount; | ||
if affectedRowCount is int { | ||
return affectedRowCount; | ||
} else { | ||
return error("Unable to obtain the affected row count for applicant delete"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import ballerina/sql; | ||
|
||
public type ApplicantAddress record { | ||
int applicant_id; | ||
int address_id; | ||
}; | ||
|
||
public isolated function getApplicantAddresses() returns ApplicantAddress[]|error { | ||
ApplicantAddress[] applicantAddresses = []; | ||
stream<ApplicantAddress, error?> resultStream = smsDBClient->query( | ||
`SELECT | ||
applicant_id , | ||
address_id | ||
FROM applicant_address` | ||
); | ||
check from ApplicantAddress applicantAddress in resultStream | ||
do { | ||
applicantAddresses.push(applicantAddress); | ||
}; | ||
check resultStream.close(); | ||
return applicantAddresses; | ||
} | ||
|
||
public isolated function getApplicantAddress(int applicant_id, int address_id) returns ApplicantAddress|error { | ||
ApplicantAddress applicantAddress = check smsDBClient->queryRow( | ||
`SELECT * FROM applicant_address WHERE applicant_id = ${applicant_id} AND address_id = ${address_id}` | ||
); | ||
return applicantAddress; | ||
} | ||
|
||
public isolated function getAddressesForApplicant(int applicant_id) returns ApplicantAddress[]|error { | ||
ApplicantAddress[] applicantAddresses = []; | ||
stream<ApplicantAddress, error?> resultStream = smsDBClient->query( | ||
`SELECT | ||
applicant_id , | ||
address_id | ||
FROM applicant_address WHERE applicant_id = ${applicant_id}` | ||
); | ||
check from ApplicantAddress applicantAddress in resultStream | ||
do { | ||
applicantAddresses.push(applicantAddress); | ||
}; | ||
check resultStream.close(); | ||
return applicantAddresses; | ||
} | ||
|
||
public isolated function addApplicantAddress(ApplicantAddress applicantAddress) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
INSERT INTO applicant_address ( | ||
applicant_id , | ||
address_id ) | ||
VALUES ( | ||
${applicantAddress.applicant_id}, | ||
${applicantAddress.address_id} | ||
) | ||
`); | ||
int|string? lastInsertId = result.lastInsertId; | ||
if lastInsertId is int { | ||
return lastInsertId; | ||
} else { | ||
return error("Unable to obtain last insert ID for applicant_address"); | ||
} | ||
} | ||
|
||
public isolated function updateApplicantAddress(ApplicantAddress applicantAddress) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
UPDATE applicant_address SET | ||
applicant_id = ${applicantAddress.applicant_id}, | ||
address_id = ${applicantAddress.address_id} | ||
WHERE applicant_address = ${applicantAddress.applicant_id} | ||
`); | ||
int|string? affectedRowCount = result.affectedRowCount; | ||
if affectedRowCount is int { | ||
return affectedRowCount; | ||
} else { | ||
return error("Unable to obtain last affected count for applicant_address update"); | ||
} | ||
} | ||
|
||
isolated function deleteApplicantAddress(int applicant_id, int address_id) returns int|error { | ||
sql:ExecutionResult result = check smsDBClient->execute(` | ||
DELETE FROM applicant_address WHERE applicant_id = ${applicant_id} AND address_id = ${address_id} | ||
`); | ||
int? affectedRowCount = result.affectedRowCount; | ||
if affectedRowCount is int { | ||
return affectedRowCount; | ||
} else { | ||
return error("Unable to obtain the affected row count for applicant_address delete"); | ||
} | ||
} |
Oops, something went wrong.