Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chamil321 committed Jul 25, 2020
1 parent 323077f commit c83d57a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
37 changes: 16 additions & 21 deletions distributor/src/distributor/messenger.bal
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,27 @@ function unregisterSmsRecipient(string username, string mobileNo) returns string

function unregisterAllSMSRecipient() returns error? {
mobileSubscribers = {};
_ = check dbClient->update(DROP_RECIPIENT_TABLE);
_ = check dbClient->update(CREATE_RECIPIENT_TABLE);
_ = check dbClient->update(DELETE_RECIPIENT_TABLE);
}

function readRecipients(io:ReadableByteChannel|error payload) returns @tainted Recipient[]|error {
Recipient[] recipients = [];
if (payload is io:ReadableByteChannel) {
io:ReadableCharacterChannel rch = new (payload, "UTF8");
var result = rch.readJson();
closeReadableChannel(rch);
if (result is error) {
string logMessage = "Error occurred while reading json: malformed Recipient[]";
log:printError(logMessage, result);
return error(ERROR_REASON, message = logMessage, cause = result);
}

any|error recipientArray = Recipient[].constructFrom(<json>result);
if recipientArray is error {
string logMessage = "Error occurred while converting json: malformed Recipient[]";
log:printError(logMessage, recipientArray);
return error(ERROR_REASON, message = logMessage, cause = recipientArray);
}
return <Recipient[]> recipientArray;
} else {
return payload;
io:ReadableByteChannel rbChannel = check payload;

io:ReadableCharacterChannel rch = new (rbChannel, "UTF8");
var result = rch.readJson();
closeReadableChannel(rch);
if (result is error) {
string logMessage = "Error occurred while reading json: malformed Recipient[]";
return error(ERROR_REASON, message = logMessage, cause = result);
}

Recipient[]|error recipientArray = Recipient[].constructFrom(<json>result);
if recipientArray is error {
string logMessage = "Error occurred while converting json: malformed Recipient[]";
return error(ERROR_REASON, message = logMessage, cause = recipientArray);
}
return recipientArray;
}

function closeReadableChannel(io:ReadableCharacterChannel rc) {
Expand Down
1 change: 1 addition & 0 deletions distributor/src/distributor/save.bal
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const INSERT_RECIPIENT = "INSERT INTO smsRecipients (username, mobileNo) VALUES
const DELETE_RECIPIENT = "DELETE FROM smsRecipients WHERE username = ?";
const SELECT_RECIPIENT_DATA = "SELECT * FROM smsRecipients";
const DROP_RECIPIENT_TABLE = "DROP TABLE smsRecipients";
const DELETE_RECIPIENT_TABLE = "DELETE FROM smsRecipients";

jdbc:Client dbClient = new ({
url: config:getAsString("eclk.hub.db.url"),
Expand Down
1 change: 1 addition & 0 deletions distributor/src/distributor/website.bal
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ service mediaWebsite on mediaListener {
resource function smsBulkRegistration (http:Caller caller, http:Request req) returns error? {
Recipient[]|error recipient = readRecipients(req.getByteChannel());
if recipient is error {
log:printError("Invalid input", recipient);
return caller->badRequest(<@untainted> recipient.toString());
}
Recipient[] smsRecipient = <Recipient[]> recipient;
Expand Down

0 comments on commit c83d57a

Please sign in to comment.