Skip to content

Commit

Permalink
Merge pull request #28 from sanjiva/master
Browse files Browse the repository at this point in the history
Align data response to meet App's usage
  • Loading branch information
sanjiva authored Aug 4, 2020
2 parents 8a463be + 4c75816 commit 974520e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
10 changes: 5 additions & 5 deletions backend/src/services/electors.bal
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jdbc:Client dbClient = new ({
# + return - list of matching electors
function getElectors(string election, string districtSI, string divisionSI, string stationID) returns @tainted json[]|error {
string SELECT_ELECTORS =
"SELECT ID, ElectorID, NationalID, GNDivision_SI, Street_SI, HouseNo, Name_SI, Name_TA, Sex " +
"SELECT ID as ElectorID, ElectorID as ID, NationalID, GNDivision_SI, Street_SI, HouseNo, Name_SI, Name_TA, Sex " +
"FROM ElectorRegistry " +
"WHERE DistrictID=? AND PollingDivisionID=? AND PollingStationID=?";
table<record{}> ret = check dbClient->select(SELECT_ELECTORS, ElectorResponse, districtSI, divisionSI, stationID);
Expand All @@ -36,7 +36,7 @@ function getElectors(string election, string districtSI, string divisionSI, stri

function setVoterStatus(string election, string districtID, string divisionID, string stationID, string voterID, string timestamp, string status) returns error? {
string STATUSUPDATE = "REPLACE INTO VoteRecords(Election, DistrictID, PollingDivisionID, PollingStationID, ID, Age, VotingStatus, Timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
var res = dbClient->update(STATUSUPDATE, election, districtID, divisionID, stationID, voterID, <@untainted> getVoterAge(voterID), status, timestamp);
var res = dbClient->update(STATUSUPDATE, election, districtID, divisionID, stationID, voterID, <@untainted> getVoterAge(districtID, divisionID, stationID, voterID), status, timestamp);
if !(res is jdbc:UpdateResult) {
log:printError(string`Error recording ${status} status of voter ${voterID} at polling station '${districtID}/${divisionID}/${stationID}'`);
return res;
Expand All @@ -52,9 +52,9 @@ function getInStatus(string election, string districtID, string divisionID, stri
# Look up the elector in the DB and calculate their age as of the current date (in years only) from their NIC.
# + elector - ID of the elector in the DB
# + return - age of the elector in years with -1 being returned if unable to get it for whatever reason
function getVoterAge(string elector) returns @tainted int {
string ELECTOR_QUERY = "SELECT NationalID from ElectorRegistry where ID = ?";
table<record{}>|error ret = dbClient->select(ELECTOR_QUERY, record { string nationalID; }, elector);
function getVoterAge(string district, string division, string station, string elector) returns @tainted int {
string ELECTOR_QUERY = "SELECT NationalID from ElectorRegistry where DistrictID = ? and PollingDivisionID = ? and PollingStationID = ? and ElectorID = ?";
table<record{}>|error ret = dbClient->select(ELECTOR_QUERY, record { string nationalID; }, district, division, station, elector);
string nic = "";
if ret is error {
log:printError(string`Error while retrieving nationalID of elector ${elector}: ${ret.reason()}`);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type InfoResponse record {
};

type ElectorResponse record {
int ID;
string ElectorID;
int ID;
string NationalID;
string GNDivision_SI;
string Street_SI;
Expand Down
20 changes: 4 additions & 16 deletions datagen/dbsetup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,8 @@ CREATE TABLE PollingDivisions (
);

INSERT INTO PollingDivisions VALUES
(1, 'A', 'COLOMBO-NORTH', '', ''),
(1, 'B', 'COLOMBO-CENTRAL', '', ''),
(1, 'C', 'BORELLA', '', ''),
(1, 'D', 'COLOMBO-EAST', '', ''),
(1, 'E', 'COLOMBO-WEST', 'බටහිර කොළඹ', 'கொழும்பு மேற்க'),
(1, 'F', 'DEHIWALA', '', ''),
(1, 'G', 'RATMALANA', '', ''),
(1, 'H', 'KOLONNAWA', '', ''),
(1, 'I', 'KOTTE', '', ''),
(1, 'J', 'KADUWELA', 'කඩුවෙල', 'கடுவெல'),
(1, 'K', 'AVISSAWELLA', '', ''),
(1, 'L', 'HOMAGAMA', '', ''),
(1, 'M', 'MAHARAGAMA', '', ''),
(1, 'N', 'KESBEWA', '', ''),
(1, 'O', 'MORATUWA', '', '');
(1, 'J', 'KADUWELA', 'කඩුවෙල', 'கடுவெல');

CREATE TABLE ElectorRegistry (
ID INT NOT NULL AUTO_INCREMENT,
Expand Down Expand Up @@ -70,6 +57,7 @@ CREATE TABLE VoteRecords (
Age int DEFAULT -1,
VotingStatus enum('NOT-VOTED','QUEUED','VOTED') DEFAULT 'NOT-VOTED',
TimeStamp timestamp DEFAULT CURRENT_TIMESTAMP(),
PRIMARY KEY (ID),
CONSTRAINT ID FOREIGN KEY (ID) REFERENCES ElectorRegistry (ID)
PRIMARY KEY (DistrictID, PollingDivisionID, PollingStationID, ID)
-- PRIMARY KEY (ID),
-- CONSTRAINT ID FOREIGN KEY (ID) REFERENCES ElectorRegistry (ID)
);

0 comments on commit 974520e

Please sign in to comment.