示例#1
0
        public IActionResult ExportToExcel()
        {
            //This function download list of all Voters as excel file
            try
            {
                var stream = new System.IO.MemoryStream();
                using (ExcelPackage package = new ExcelPackage(stream))
                {
                    var            voters    = _voterBusiness.GetAll();
                    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(_messagesLoclizer["Voters"]);

                    worksheet.Cells[1, 1].Value      = _messagesLoclizer["First Name"];
                    worksheet.Cells[1, 2].Value      = _messagesLoclizer["Last Name"];
                    worksheet.Cells[1, 3].Value      = _messagesLoclizer["State"];
                    worksheet.Row(1).Style.Font.Bold = true;


                    for (int c = 2; c < voters.Count + 2; c++)
                    {
                        worksheet.Cells[c, 1].Value = voters[c - 2].FirstName;
                        worksheet.Cells[c, 2].Value = voters[c - 2].LastName;
                        worksheet.Cells[c, 3].Value = voters[c - 2].State?.Name;
                    }

                    package.Save();
                }

                StringBuilder fileName = new StringBuilder();
                fileName.Append(_messagesLoclizer["Voters"] + ".xlsx");

                StringBuilder fileType = new StringBuilder();
                fileType.Append("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

                stream.Position = 0;
                return(File(stream, fileType.ToString(), fileName.ToString()));
            }
            catch (Exception E)
            {
                throw E;
            }
        }
示例#2
0
        public async Task <IActionResult> ValidateElection([FromBody] Election election)
        {
            //this method is called using ajax calls
            //so if a business rule is not met we'll throw a businessException and catch it to create and internal server error and return its msg
            //as json

            //Step(1): Adding info of the election(name, duration,,,) and send them to backend using api method in inside the controller,
            //then send back the response to javascript to redirect to step2

            try
            {
                if (ModelState.IsValid)
                {
                    election.Id = Guid.NewGuid();

                    _electionBusiness.AddNewElection(election);

                    response_Voters_and_NewElection r;
                    r.ElectionId = election.Id;
                    r.Voters     = _voterBusiness.ConvertVoterList_ToPersonViewModelList(_voterBusiness.GetAll());

                    //lets serialize the struct we've got and send it back as a reponse
                    var json = JsonConvert.SerializeObject(r);
                    return(Ok(json));
                }
                else
                {
                    //Model is not valid
                    throw new BusinessException(_messagesLoclizer["Data not valid, please check again."]);
                }
            }
            catch (DataNotUpdatedException bnu)
            {
                //lets now create a suitable message for the user and store it inside a ViewBag (which is a Dynamic Object we can fill it
                //by whatever we want
                BusinessMessage bm = new BusinessMessage("Error", bnu.Message);
                ViewBag.BusinessMessage = bm;
                return(View());
            }
            catch (BusinessException be)
            {
                //lets create an internal server error so that the response returned is an ERROR, and jQuery ajax will understand that.
                HttpContext.Response.StatusCode = 500;
                return(Json(new { Message = be.Message }));
            }
            catch (Exception E)
            {
                HttpContext.Response.StatusCode = 500;
                return(Json(new { Message = E.Message }));
                //In above code I created an internal server error so that the response returned is an ERROR, and jQuery ajax will understand that.
            }
        }