Ejemplo n.º 1
0
 public async Task <IActionResult> ImportIndividualCodes()
 {
     try {
         var file = Request.Form.Files[0];
         if (Request.Form.ContainsKey("parameters"))
         {
             CodeGenerationViewModel codeParameters = JsonConvert.DeserializeObject <CodeGenerationViewModel> (Request.Form["parameters"]);
             codeParameters.NumberOfCodes = 1;                     //hack to prevent error
             CodeGenerationViewModelValidator parameterValidator = new CodeGenerationViewModelValidator();
             ValidationResult validParameters = parameterValidator.Validate(codeParameters);
             if (validParameters.IsValid)
             {
                 int    numberofCodes = 0;
                 string line;
                 using (StreamReader fileStream = new StreamReader(file.OpenReadStream())) {
                     //for now assume valid line, generate a code per line (change later)
                     while ((line = fileStream.ReadLine()) != null)
                     {
                         numberofCodes++;
                     }
                 }
                 codeParameters.NumberOfCodes = numberofCodes;
                 return(await this.CreateShortcodes(codeParameters));
             }
             else
             {
                 AddErrors(validParameters.Errors.Select(e => e.ErrorMessage));
                 return(BadRequest(ModelState));
             }
         }
         else
         {
             return(BadRequest("Missing Code Generation Parameters"));
         }
     } catch (System.Exception ex) {
         return(BadRequest("Upload Failed: " + ex.Message));
     }
 }
Ejemplo n.º 2
0
        public async Task <IActionResult> ImportGroupCodes()
        {
            try {
                var file = Request.Form.Files[0];
                if (Request.Form.ContainsKey("parameters"))
                {
                    CodeGenerationViewModel codeParameters = JsonConvert.DeserializeObject <CodeGenerationViewModel> (Request.Form["parameters"]);
                    codeParameters.NumberOfCodes = 1;                     //hack to prevent error
                    CodeGenerationViewModelValidator parameterValidator = new CodeGenerationViewModelValidator();
                    ValidationResult validParameters = parameterValidator.Validate(codeParameters);
                    if (validParameters.IsValid)
                    {
                        var survey = await this._unitOfWork.Surveys.GetAsync(codeParameters.SurveyId);

                        if (survey.Owner == this.User.Identity.Name || await HasExecuteSurveyPermissions(codeParameters.SurveyId))
                        {
                            CodeGeneration codeGenerationParameters = Mapper.Map <CodeGeneration> (codeParameters);
                            string         line;
                            int            lineIndex  = 1;
                            List <string>  errorList  = new List <string> ();
                            List <string>  groupNames = new List <string> ();
                            using (StreamReader fileStream = new StreamReader(file.OpenReadStream())) {
                                //for now assume valid line, generate a code per line (change later)
                                while ((line = fileStream.ReadLine()) != null)
                                {
                                    var lineSplit = line.Split(',');
                                    if (lineSplit.Count() != 1)
                                    {
                                        errorList.Add($"Error at line {lineIndex}: {line} - Skipped generation");
                                    }
                                    else
                                    {
                                        groupNames.Add(lineSplit[0]);
                                    }
                                }
                            }
                            codeGenerationParameters.NumberOfCodes = groupNames.Count;
                            this._codeGeneration.GenerateGroupCodesBatch(codeGenerationParameters, groupNames, survey);
                            await this._unitOfWork.SaveChangesAsync();

                            if (errorList.Count == 0)
                            {
                                return(new OkResult());
                            }
                            else
                            {
                                AddErrors(errorList);
                                return(BadRequest(ModelState));
                            }
                        }
                        return(BadRequest("User does not have permissions to execute this survey."));
                    }
                    else
                    {
                        foreach (var error in validParameters.Errors)
                        {
                            ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
                        }
                        return(BadRequest(ModelState));
                    }
                }
                else
                {
                    return(BadRequest("Missing Code Generation Parameters"));
                }
            } catch (System.Exception ex) {
                return(BadRequest("Upload Failed: " + ex.Message));
            }
        }