Esempio n. 1
0
        public static CSVResponse ProcessCSV(IFormFile formFile)
        {
            var validation = new CSVResponse()
            {
                Response = new GeneralResponse(),
                Jobs     = new List <JobPosting>()
            };
            var formats   = new string[] { "dd-MM-yy", "dd.MM.yy", "dd/MM/yy" };
            var reader    = new StreamReader(formFile.OpenReadStream());
            var iteration = 1;

            var csv = new CsvReader(reader);

            csv.Configuration.Delimiter = ",";

            while (csv.Read())
            {
                csv.ReadHeader();
                var headers = csv.Context.HeaderRecord.ToList();
                if (iteration == 1)
                {
                    if (!headers.SequenceEqual(ConstantStrings.HEADERS))
                    {
                        validation.Response.Errors["Error"].Add("The header columns must in the following order: Title, Description, Type, Education, Experience, DateFrom, DateTo ");
                    }
                    iteration++;
                    continue;
                }
                try
                {
                    bool dateFromParse = DateTime.TryParseExact(csv[5].ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateFrom);
                    bool dateToParse   = DateTime.TryParseExact(csv[6].ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTo);

                    if (csv[0].Length > 30 || csv[0].Length == 0 || csv[1].Length > 800 || !dateFromParse || !dateToParse)
                    {
                        validation.Response.Errors["Error"].Add(ErrorConstants.InvalidInput + " at line " + iteration);
                    }
                    var _Job = new JobPosting()
                    {
                        Title            = csv[0],
                        Description      = csv[1],
                        EmpCategory      = Enum.Parse <JobType>(csv[2].ToString()),
                        Education        = Enum.Parse <EducationType>(csv[3].ToString()),
                        NeededExperience = csv[4],
                        DateFrom         = dateFrom,
                        DateTo           = dateTo
                    };


                    validation.Jobs.Add(_Job);
                    iteration++;
                }
                catch
                {
                    validation.Response.Errors["Error"].Add(ErrorConstants.InvalidFormat + " at line " + iteration);
                    return(validation);
                }
            }
            return(validation);
        }
Esempio n. 2
0
        private static async Task <List <CSVResponse> > GetSurveyResponses(object data)
        {
            AzureBlobDocuments document = jsonHelper.FromJson <AzureBlobDocuments>(data.ToString());

            CSVResponse        question    = new CSVResponse();
            List <CSVResponse> csvResponse = new List <CSVResponse>();
            bool   headerIgnore            = true;
            string csvData = null;

            CloudAppendBlob appendBlob = CONTAINER.GetAppendBlobReference(document.Filename);

            try
            {
                csvData = await appendBlob.DownloadTextAsync();

                if (csvData != null)
                {
                    csvData = csvData.Replace("\r\n", ",-,");     //replace "\r\n" with end line character
                    csvData = csvData.Remove(csvData.Length - 1); //remove trailing "," before splitting to stop erronous final record.
                    string[] values = csvData.Split(',');

                    for (int i = 0; i < values.Count(); i++)
                    {
                        Int32.TryParse(values[i++], out int surveyID);
                        question.SurveyID = surveyID;
                        question.Date     = values[i++];
                        question.Time     = values[i++];

                        int j = 1;
                        do
                        {
                            if (values[i].Equals("-"))
                            {
                                break;
                            }
                            question.Responses.Add(new QuestionResponse()
                            {
                                QuestionNumber = j++, Answer = values[i++]
                            });
                        } while (i <= values.Count());

                        if (!headerIgnore)
                        {
                            csvResponse.Add(question);
                        }

                        question     = new CSVResponse();
                        headerIgnore = false;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception Caught - " + ex.Message);
            }

            return(csvResponse);
        }
Esempio n. 3
0
        public static CSVResponse ValidateCSV(IFormFile formFile)
        {
            var result = new CSVResponse()
            {
                Response = new GeneralResponse(),
                Jobs     = new List <JobPosting>()
            };

            var format = ValidateFormat(formFile);

            if (format.Errors["Error"].Any())
            {
                result.Response = format;
                return(result);
            }
            var content = ProcessCSV(formFile);

            if (content.Response.Errors["Error"].Any())
            {
                result.Response = content.Response;
            }
            result.Jobs = content.Jobs;
            return(result);
        }