Ejemplo n.º 1
0
        public async Task <IActionResult> Get()
        {
            Uri serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri proxyAddress = this.GetProxyAddress(serviceName);

            ServicePartitionList partitions = await this.fabricClient.QueryManager.GetPartitionListAsync(serviceName);

            List <KeyValuePair <string, int> > result = new List <KeyValuePair <string, int> >();

            foreach (Partition partition in partitions)
            {
                string proxyUrl =
                    $"{proxyAddress}/api/VoteData?PartitionKey={((Int64RangePartitionInformation)partition.PartitionInformation).LowKey}&PartitionKind=Int64Range";

                using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl))
                {
                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        continue;
                    }

                    result.AddRange(JsonConvert.DeserializeObject <List <KeyValuePair <string, int> > >(await response.Content.ReadAsStringAsync()));
                }
            }

            return(Json(result));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CastVote([FromBody] UserDetails userDetails)
        {
            try
            {
                IActionResult validationResult = Validator.ValidateCastVoteData(userDetails);
                if (validationResult != null)
                {
                    return(validationResult);
                }

                Uri    serviceName  = VotingWeb.GetVotingDataServiceName(serviceContext);
                Uri    proxyAddress = GetProxyAddress(serviceName);
                int    partitionKey = userDetails.AadharNo.Sum(c => (int)char.GetNumericValue(c));
                string proxyUrl     = $"{proxyAddress}/api/VoteData/CastVote?PartitionKey={partitionKey}&PartitionKind=Int64Range";

                StringContent postContent = new StringContent($"{{ 'AadharNo' : '{userDetails.AadharNo}', 'VoterId' : '{userDetails.VoterId}', 'VoteFor' : '{userDetails.VoteFor}', 'Otp' : '{userDetails.Otp}' }}", Encoding.UTF8, "application/json");
                postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                using (HttpResponseMessage response = await httpClient.PostAsync(proxyUrl, postContent))
                {
                    return(ActionResultCreator.CreateCastVoteActionResult(response));
                }
            }
            catch (Exception)
            {
                return(new ContentResult
                {
                    StatusCode = (int)Enums.ResponseMessageCode.Success,
                    Content = Enums.CastVoteStatus.VotingFailed.ToString()
                });
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> VerifyOtp(string aadharNo, string userEnteredOtp)
        {
            try
            {
                IActionResult validationResult = Validator.ValidateVerifyOtpData(aadharNo, userEnteredOtp);
                if (validationResult != null)
                {
                    return(validationResult);
                }

                Uri    serviceName  = VotingWeb.GetVotingDataServiceName(serviceContext);
                Uri    proxyAddress = GetProxyAddress(serviceName);
                int    partitionKey = aadharNo.Sum(c => (int)char.GetNumericValue(c));
                string proxyUrl     = $"{proxyAddress}/api/VoteData/VerifyOtp/{aadharNo}/{userEnteredOtp}?PartitionKey={partitionKey}&PartitionKind=Int64Range";

                StringContent postContent = new StringContent($"{{ 'aadharNo' : '{aadharNo}', 'userEnteredOtp' : '{userEnteredOtp}' }}", Encoding.UTF8, "application/json");
                postContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                using (HttpResponseMessage response = await httpClient.PostAsync(proxyUrl, postContent))
                {
                    return(ActionResultCreator.CreateActionResult(response));
                }
            }
            catch (Exception)
            {
                return(new ContentResult
                {
                    StatusCode = (int)Enums.ResponseMessageCode.Success,
                    Content = Enums.ResponseMessageCode.Failure.ToString()
                });
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Put(string name)
        {
            ServiceEventSource.Current.ServiceRequestStart("VotesController.Put");
            ContentResult result = null;

            Uri    serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri    proxyAddress = this.GetProxyAddress(serviceName);
            long   partitionKey = this.GetPartitionKey(name);
            string proxyUrl     = $"{proxyAddress}/api/VoteData/{name}?PartitionKey={partitionKey}&PartitionKind=Int64Range";

            StringContent putContent = new StringContent($"{{ 'name' : '{name}' }}", Encoding.UTF8, "application/json");

            putContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            // Insert "trump" snippet here to introduce a nasty bug
            if (!string.IsNullOrEmpty(name) && name.ToUpper().StartsWith("TRUMP"))
            {
                for (int i = 0; i < 10; i++)
                {
                    await this.httpClient.PutAsync(proxyUrl, putContent);
                }
            }

            // Record the new vote
            using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent))
            {
                result = new ContentResult()
                {
                    StatusCode = (int)response.StatusCode,
                    Content    = await response.Content.ReadAsStringAsync()
                };
            }

            // Separately audit the ballot
            if (result.StatusCode == 200)
            {
                proxyUrl = $"{proxyAddress}/api/VoteData/?PartitionKey={partitionKey}&PartitionKind=Int64Range";
                using (HttpResponseMessage response = await this.httpClient.PostAsync(proxyUrl, null))
                {
                    result = new ContentResult()
                    {
                        StatusCode = (int)response.StatusCode,
                        Content    = await response.Content.ReadAsStringAsync()
                    };
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Delete(string name)
        {
            Uri    serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri    proxyAddress = this.GetProxyAddress(serviceName);
            string proxyUrl     = $"{proxyAddress}/api/VoteData/{name}";

            using (HttpResponseMessage response = await this.httpClient.DeleteAsync(proxyUrl))
            {
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(this.StatusCode((int)response.StatusCode));
                }
            }

            return(new OkResult());
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Get()
        {
            ServiceEventSource.Current.ServiceRequestStart("VotesController.Get");

            Uri  serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri  proxyAddress = this.GetProxyAddress(serviceName);
            long totalBallots = 0;

            ServicePartitionList partitions = await this.fabricClient.QueryManager.GetPartitionListAsync(serviceName);

            List <KeyValuePair <string, int> > votes = new List <KeyValuePair <string, int> >();

            foreach (Partition partition in partitions)
            {
                string proxyUrl =
                    $"{proxyAddress}/api/VoteData?PartitionKey={((Int64RangePartitionInformation)partition.PartitionInformation).LowKey}&PartitionKind=Int64Range";

                using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl))
                {
                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        continue;
                    }

                    votes.AddRange(JsonConvert.DeserializeObject <List <KeyValuePair <string, int> > >(await response.Content.ReadAsStringAsync()));
                }

                string proxyUrl2 =
                    $"{proxyAddress}/api/VoteData/ballots?PartitionKey={((Int64RangePartitionInformation)partition.PartitionInformation).LowKey}&PartitionKind=Int64Range";

                using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl2))
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        Int64.TryParse(await response.Content.ReadAsStringAsync(), out totalBallots);
                    }
                }
            }

            Models.VoteTally result = new Models.VoteTally()
            {
                Votes = votes, TotalBallots = totalBallots
            };

            return(this.Json(result));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> GetReport()
        {
            Uri serviceName  = VotingWeb.GetReportgGeneratorServiceName(this.serviceContext);
            Uri proxyAddress = this.GetProxyAddress(serviceName);

            string proxyUrl = $"{proxyAddress}/api/Report/NumberOfVotes";

            var response = await this.httpClient.GetAsync(proxyUrl);

            if (!response.IsSuccessStatusCode)
            {
                return(this.NoContent());
            }

            var result = await response.Content.ReadAsStringAsync();

            return(Ok(result));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Delete(string name)
        {
            ServiceEventSource.Current.ServiceRequestStart("VotesController.Delete");

            Uri    serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri    proxyAddress = this.GetProxyAddress(serviceName);
            long   partitionKey = this.GetPartitionKey(name);
            string proxyUrl     = $"{proxyAddress}/api/VoteData/{name}?PartitionKey={partitionKey}&PartitionKind=Int64Range";

            using (HttpResponseMessage response = await this.httpClient.DeleteAsync(proxyUrl))
            {
                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    return(this.StatusCode((int)response.StatusCode));
                }
            }

            return(new OkResult());
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Put(string name)
        {
            Uri    serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri    proxyAddress = this.GetProxyAddress(serviceName);
            string proxyUrl     = $"{proxyAddress}/api/VoteData/{name}";

            StringContent putContent = new StringContent($"{{ 'name' : '{name}' }}", Encoding.UTF8, "application/json");

            putContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent))
            {
                return(new ContentResult()
                {
                    StatusCode = (int)response.StatusCode,
                    Content = await response.Content.ReadAsStringAsync()
                });
            }
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Get()
        {
            Uri serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri proxyAddress = this.GetProxyAddress(serviceName);

            List <KeyValuePair <string, int> > result = new List <KeyValuePair <string, int> >();

            string proxyUrl = $"{proxyAddress}/api/VoteData";

            using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl))
            {
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    result.AddRange(JsonConvert.DeserializeObject <List <KeyValuePair <string, int> > >(await response.Content.ReadAsStringAsync()));
                }
            }

            return(this.Json(result));
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> Get()
        {
            Uri serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri proxyAddress = this.GetProxyAddress(serviceName);

            // reading the config in the settings.xml
            var    config   = this.serviceContext.CodePackageActivationContext.GetConfigurationPackageObject("Config");
            var    settings = config.Settings.Sections["MyConfigSection"];
            string name     = settings.Parameters["MyParameter"].Name;
            string val      = settings.Parameters["MyParameter"].Value;

            ServicePartitionList partitions = await this.fabricClient.QueryManager.GetPartitionListAsync(serviceName);

            List <KeyValuePair <string, int> > result = new List <KeyValuePair <string, int> >();

            foreach (Partition partition in partitions)
            {
                string proxyUrl =
                    $"{proxyAddress}/api/VoteData?PartitionKey={((Int64RangePartitionInformation) partition.PartitionInformation).LowKey}&PartitionKind=Int64Range";

                using (HttpResponseMessage response = await this.httpClient.GetAsync(proxyUrl))
                {
                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        continue;
                    }

                    var voteDatas = JsonConvert.DeserializeObject <List <KeyValuePair <string, int> > >(await response.Content.ReadAsStringAsync());
                    foreach (KeyValuePair <string, int> voteData in voteDatas)
                    {
                        KeyValuePair <string, int> newData = new KeyValuePair <string, int>(val + voteData.Key, voteData.Value);
                        result.Add(newData);
                    }
                }
            }

            return(this.Json(result));
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> Put(string name)
        {
            Uri  serviceName  = VotingWeb.GetVotingDataServiceName(this.serviceContext);
            Uri  proxyAddress = this.GetProxyAddress(serviceName);
            long partitionKey = this.GetPartitionKey(name);

            //1. construct the URL to the ReverseProxy for our back-end service (1).
            string proxyUrl = $"{proxyAddress}/api/VoteData/{name}?PartitionKey={partitionKey}&PartitionKind=Int64Range";

            StringContent putContent = new StringContent($"{{ 'name' : '{name}' }}", Encoding.UTF8, "application/json");

            putContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            //(2). end the HTTP PUT Request to the ReverseProxy
            using (HttpResponseMessage response = await this.httpClient.PutAsync(proxyUrl, putContent))
            {
                //(3).Return the response from the back-end service to the client
                return(new ContentResult()
                {
                    StatusCode = (int)response.StatusCode,
                    Content = await response.Content.ReadAsStringAsync()
                });
            }
        }