-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add address and organization tables, and related services
- Loading branch information
Showing
9 changed files
with
241 additions
and
4 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
USER="root" | ||
PASSWORD="test" | ||
DATABASE="avinya_db" | ||
HOST="localhost" | ||
PASSWORD="test" | ||
PORT=3306 | ||
DATABASE="avinya_db" | ||
USER="root" |
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
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 |
---|---|---|
@@ -1,8 +1,63 @@ | ||
import ballerina/graphql; | ||
import ballerina/sql; | ||
|
||
service graphql:Service /graphql on new graphql:Listener(4000) { | ||
resource function get geo() returns GeoData { | ||
return new (); | ||
} | ||
|
||
resource function get organization(string? name, int? id) returns OrganizationData|error? { | ||
return new (name, id); | ||
} | ||
|
||
remote function add_organization(Organization org) returns OrganizationData|error? { | ||
sql:ExecutionResult res = check db_client->execute( | ||
`INSERT INTO avinya_db.organization ( | ||
name_en, | ||
name_si, | ||
name_ta, | ||
address_id, | ||
phone | ||
) VALUES ( | ||
${org.name_en}, | ||
${org.name_si}, | ||
${org.name_ta}, | ||
${org.address_id}, | ||
${org.phone}, | ||
);` | ||
); | ||
|
||
int|string? insert_id = res.lastInsertId; | ||
if !(insert_id is int) { | ||
return error("Unable to insert organization"); | ||
} | ||
|
||
// Insert child and parent organization relationships | ||
int[] child_org_ids = org.child_organizations ?: []; | ||
int[] parent_org_ids = org.parent_organizations ?: []; | ||
|
||
foreach int child_idx in child_org_ids { | ||
_ = check db_client->execute( | ||
`INSERT INTO avinya_db.parent_child_organization ( | ||
child_org_id, | ||
parent_org_id | ||
) VALUES ( | ||
${child_idx}, ${org.id} | ||
);` | ||
); | ||
} | ||
|
||
foreach int parent_idx in parent_org_ids { | ||
_ = check db_client->execute( | ||
`INSERT INTO avinya_db.parent_child_organization ( | ||
child_org_id, | ||
parent_org_id | ||
) VALUES ( | ||
${org.id}, ${parent_idx} | ||
);` | ||
); | ||
} | ||
|
||
return new ((), insert_id); | ||
} | ||
} |
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,73 @@ | ||
public distinct service class OrganizationData { | ||
private Organization organization; | ||
|
||
function init(string? name, int? organization_id) returns error? { | ||
Organization org_raw = check db_client -> queryRow( | ||
`SELECT * | ||
FROM avinya_db.organization | ||
WHERE | ||
id = ${organization_id} | ||
OR name_en = ${name};` | ||
); | ||
|
||
self.organization = org_raw.cloneReadOnly(); | ||
} | ||
|
||
resource function get address() returns AddressData|error? { | ||
return new AddressData(self.organization.address_id); | ||
} | ||
|
||
resource function get phone() returns int { | ||
return self.organization.phone; | ||
} | ||
|
||
resource function get name() returns LocalizedName { | ||
return { | ||
"name_en": self.organization["name_en"], | ||
"name_si": <string>self.organization["name_si"], | ||
"name_ta": <string>self.organization["name_ta"] | ||
}; | ||
} | ||
|
||
resource function get child_organizations() returns OrganizationData[]|error? { | ||
// Get list of child organizations | ||
stream<ParentChildOrganization, error?> child_org_ids = db_client->query( | ||
`SELECT * | ||
FROM avinya_db.parent_child_organization | ||
WHERE parent_org_id = ${self.organization.id}` | ||
); | ||
|
||
OrganizationData[] child_orgs = []; | ||
|
||
check from ParentChildOrganization pco in child_org_ids | ||
do { | ||
OrganizationData|error candidate_org = new OrganizationData((), pco.child_org_id); | ||
if !(candidate_org is error) { | ||
child_orgs.push(candidate_org); | ||
} | ||
}; | ||
|
||
return child_orgs; | ||
} | ||
|
||
resource function get parent_organizations() returns OrganizationData[]|error? { | ||
// Get list of child organizations | ||
stream<ParentChildOrganization, error?> parent_org_ids = db_client->query( | ||
`SELECT * | ||
FROM avinya_db.parent_child_organization | ||
WHERE parent_org_id = ${self.organization.id}` | ||
); | ||
|
||
OrganizationData[] parent_orgs = []; | ||
|
||
check from ParentChildOrganization pco in parent_org_ids | ||
do { | ||
OrganizationData|error candidate_org = new OrganizationData((), pco.parent_org_id); | ||
if !(candidate_org is error) { | ||
parent_orgs.push(candidate_org); | ||
} | ||
}; | ||
|
||
return parent_orgs; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
USER="root" | ||
PASSWORD="test" | ||
HOST="mysql" | ||
PASSWORD="test" | ||
PORT=3306 | ||
DATABASE="avinya_db" |
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
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
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,33 @@ | ||
USE avinya_db; | ||
|
||
-- Address | ||
CREATE TABLE IF NOT EXISTS address ( | ||
id INT NOT NULL PRIMARY KEY, | ||
name_en VARCHAR(255) NOT NULL, | ||
name_ta VARCHAR(255), | ||
name_si VARCHAR(255), | ||
street_address VARCHAR(255) NOT NULL, | ||
phone INT, | ||
city_id INT NOT NULL, | ||
FOREIGN KEY (city_id) REFERENCES city(id) | ||
); | ||
|
||
-- Organization | ||
CREATE TABLE IF NOT EXISTS organization ( | ||
id INT NOT NULL PRIMARY KEY, | ||
name_en VARCHAR(255) NOT NULL, | ||
name_ta VARCHAR(255), | ||
name_si VARCHAR(255), | ||
phone int, | ||
address_id INT NOT NULL, | ||
FOREIGN KEY (address_id) REFERENCES address(id) | ||
); | ||
|
||
-- Parent/Child Organization relationship | ||
CREATE TABLE IF NOT EXISTS parent_child_organization ( | ||
child_org_id INT NOT NULL, | ||
parent_org_id INT NOT NULL, | ||
FOREIGN KEY (child_org_id) REFERENCES organization(id), | ||
FOREIGN KEY (parent_org_id) REFERENCES organization(id), | ||
CONSTRAINT pk_parent_child_organization PRIMARY KEY (child_org_id, parent_org_id) | ||
); |
Empty file.