public async Task <AddRecordItemResponse> AddRecordItemAsync(Dictionary <ParameterTypeEnum, object> parameters) { telemetryClient.TrackTrace("Starting helper"); AddRecordItemResponse result = new AddRecordItemResponse { IsSucceded = true, ResultId = (int)AddRecordItemResultEnum.Success }; try { telemetryClient.TrackTrace("Getting parameters"); parameters.TryGetValue(ParameterTypeEnum.RecordItem, out global::System.Object orecordItemRequest); Domain.Models.Request.RecordItem recordItemRequest = orecordItemRequest as Domain.Models.Request.RecordItem; parameters.TryGetValue(ParameterTypeEnum.MasterAddress, out global::System.Object omasterAddress); string masterAddress = omasterAddress.ToString(); parameters.TryGetValue(ParameterTypeEnum.MasterPrivateKey, out global::System.Object omasterPrivateKey); string masterPrivateKey = omasterPrivateKey.ToString(); parameters.TryGetValue(ParameterTypeEnum.ContractAddress, out global::System.Object ocontractAddress); string contractAddress = ocontractAddress.ToString(); parameters.TryGetValue(ParameterTypeEnum.ContractABI, out global::System.Object ocontractABI); string contractABI = ocontractABI.ToString(); parameters.TryGetValue(ParameterTypeEnum.RecordItemImageContainer, out global::System.Object orecordItemImageContainer); string recordItemImageContainer = orecordItemImageContainer.ToString(); //database helpers DBRecordItemHelper dbRecordItemHelper = new DBRecordItemHelper(DBCONNECTION_INFO); DBUserAccountHelper dbUserAccountHelper = new DBUserAccountHelper(DBCONNECTION_INFO); //blockchain helper BlockchainHelper bh = new BlockchainHelper(telemetryClient, STORAGE_ACCOUNT, RPC_CLIENT, masterAddress, masterPrivateKey); telemetryClient.TrackTrace("Validating username length"); //validate username length if (!RegexValidation.IsValidUsername(recordItemRequest.username)) { result.IsSucceded = false; result.ResultId = (int)AddRecordItemResultEnum.InvalidUsernameLength; return(result); } telemetryClient.TrackTrace("Validating username existance"); //validate if account exists UserAccount userAccount = dbUserAccountHelper.GetUser(recordItemRequest.username); if (userAccount == null) { result.IsSucceded = false; result.ResultId = (int)AddRecordItemResultEnum.UsernameNotExists; return(result); } telemetryClient.TrackTrace("Validating record item existance"); //validate if record item exists for voting RecordItem recordItemExists = dbRecordItemHelper.GetRecordItem(recordItemRequest.hash); if (recordItemExists != null) { //there is no record item linked with this vote result.IsSucceded = false; result.ResultId = (int)AddRecordItemResultEnum.AlreadyExists; } else { telemetryClient.TrackTrace("Adding record item to blockchain"); var res_AddRecordAsync = await bh.AddRecordAsync(recordItemRequest.hash, recordItemRequest.username, contractAddress, contractABI); telemetryClient.TrackTrace($"Add record item result: {res_AddRecordAsync}"); if (string.IsNullOrEmpty(res_AddRecordAsync)) { //there was an error adding the record to the blockchain result.IsSucceded = false; result.ResultId = (int)AddRecordItemResultEnum.BlockchainIssue; return(result); } telemetryClient.TrackTrace("Adding record item to database"); RecordItem recordItem = RecordItemParser.TransformRecordItem(recordItemRequest); recordItem.hash = recordItem.hash.ToLower(); recordItem.transactionId = res_AddRecordAsync; recordItem.createdDate = Timezone.GetCustomTimeZone(); recordItem.image = recordItem.image + ".png"; //perform insert in mongodb await dbRecordItemHelper.CreateRecordItem(recordItem); telemetryClient.TrackTrace("Adding record item image to storage"); //upload image to blobstorage byte[] buffer = Convert.FromBase64String(recordItemRequest.ImageBytes); await UploadRecordItemImageAsync(recordItemImageContainer, recordItem.image, buffer); } } catch (AggregateException ex) { foreach (var innerException in ex.Flatten().InnerExceptions) { telemetryClient.TrackException(innerException); } result.IsSucceded = false; result.ResultId = (int)AddRecordItemResultEnum.Failed; } catch (Exception ex) { telemetryClient.TrackException(ex); result.IsSucceded = false; result.ResultId = (int)AddRecordItemResultEnum.Failed; } telemetryClient.TrackTrace("Finishing helper"); return(result); }
public async Task <AddRecordVoteResponse> AddRecordVoteAsync(Dictionary <ParameterTypeEnum, object> parameters) { telemetryClient.TrackTrace("Starting helper"); AddRecordVoteResponse result = new AddRecordVoteResponse { IsSucceded = true, ResultId = (int)AddRecordVoteResultEnum.Success }; try { telemetryClient.TrackTrace("Getting parameters"); parameters.TryGetValue(ParameterTypeEnum.Username, out global::System.Object ousername); string username = ousername.ToString().ToLower(); parameters.TryGetValue(ParameterTypeEnum.Hash, out global::System.Object ohash); string hash = ohash.ToString().ToLower(); parameters.TryGetValue(ParameterTypeEnum.IsApproval, out global::System.Object oisApproval); bool isApproval = Convert.ToBoolean(oisApproval.ToString()); parameters.TryGetValue(ParameterTypeEnum.MasterAddress, out global::System.Object omasterAddress); string masterAddress = omasterAddress.ToString(); parameters.TryGetValue(ParameterTypeEnum.MasterPrivateKey, out global::System.Object omasterPrivateKey); string masterPrivateKey = omasterPrivateKey.ToString(); parameters.TryGetValue(ParameterTypeEnum.ContractAddress, out global::System.Object ocontractAddress); string contractAddress = ocontractAddress.ToString(); parameters.TryGetValue(ParameterTypeEnum.ContractABI, out global::System.Object ocontractABI); string contractABI = ocontractABI.ToString(); //database helpers DBUserAccountHelper dbUserAccountHelper = new DBUserAccountHelper(DBCONNECTION_INFO); DBRecordItemHelper dbRecordItemHelper = new DBRecordItemHelper(DBCONNECTION_INFO); DBRecordVoteHelper dbRecordVoteHelper = new DBRecordVoteHelper(DBCONNECTION_INFO); //blockchain helper BlockchainHelper bh = new BlockchainHelper(telemetryClient, STORAGE_ACCOUNT, RPC_CLIENT, masterAddress, masterPrivateKey); telemetryClient.TrackTrace("Validating username length"); //validate username length if (!RegexValidation.IsValidUsername(username)) { result.IsSucceded = false; result.ResultId = (int)AddRecordVoteResultEnum.InvalidUsernameLength; return(result); } telemetryClient.TrackTrace("Validating username existance"); //validate if account exists UserAccount userAccount = dbUserAccountHelper.GetUser(username); if (userAccount == null) { result.IsSucceded = false; result.ResultId = (int)AddRecordVoteResultEnum.UsernameNotExists; return(result); } telemetryClient.TrackTrace("Validating record item existance"); //validate if record item exists for voting RecordItem recordItem = dbRecordItemHelper.GetRecordItem(hash); if (recordItem == null) { //there is no record item linked with this vote result.IsSucceded = false; result.ResultId = (int)AddRecordVoteResultEnum.NotExists; return(result); } else { telemetryClient.TrackTrace("Validating record vote existance"); //validate if user has voted submit a vote before RecordVote vote = dbRecordVoteHelper.GetRecordVote(hash, username); if (vote != null) { //the user already voted result.IsSucceded = false; result.ResultId = (int)AddRecordVoteResultEnum.AlreadyVoted; return(result); } else { telemetryClient.TrackTrace("Adding record vote to blockchain"); //sending to the blockchain var res_IncreaseOperationAsync = string.Empty; if ((bool)isApproval) { res_IncreaseOperationAsync = await bh.IncreaseApprovalsAsync(hash, contractAddress, contractABI); System.Diagnostics.Trace.TraceInformation($"Add record for approval vote result: {res_IncreaseOperationAsync}"); } else { res_IncreaseOperationAsync = await bh.IncreaseDisapprovalsAsync(hash, contractAddress, contractABI); System.Diagnostics.Trace.TraceInformation($"Add record for disapproval vote result: {res_IncreaseOperationAsync }"); } if (string.IsNullOrEmpty(res_IncreaseOperationAsync)) { //there was an error voting the record in the blockchain result.IsSucceded = false; result.ResultId = (int)AddRecordVoteResultEnum.BlockchainIssue; return(result); } telemetryClient.TrackTrace("Adding record vote to database"); //save record in mongodb vote = new RecordVote { username = username, hash = hash, isApproval = isApproval, transactionId = res_IncreaseOperationAsync, createdDate = Timezone.GetCustomTimeZone() }; //perform insert in mongodb await dbRecordVoteHelper.CreateRecordVote(vote); } } } catch (AggregateException ex) { foreach (var innerException in ex.Flatten().InnerExceptions) { telemetryClient.TrackException(innerException); } result.IsSucceded = false; result.ResultId = (int)AddRecordVoteResultEnum.Failed; } catch (Exception ex) { telemetryClient.TrackException(ex); result.IsSucceded = false; result.ResultId = (int)AddRecordVoteResultEnum.Failed; } telemetryClient.TrackTrace("Finishing helper"); return(result); }