Exemplo n.º 1
0
        public async Task <IActionResult> UploadAsync(List <IFormFile> files)
        {
            IList <Job> jobList = new List <Job>();

            try
            {
                long size = files.Sum(f => f.Length);
                if (size > 0)
                {
                    foreach (var formFile in files)
                    {
                        Stream    fileStream = formFile.OpenReadStream();
                        DataTable clientData = await _importService.ImportExcelAsync(fileStream);

                        //End of testing code
                        foreach (DataRow dr in clientData.Rows)
                        {
                            Job model = new Job();
                            if (dr["Client_Id"] == null || dr["Client_Id"].ToString().Trim() == "")
                            {
                                throw new Exception("Client Id can't be null");
                            }
                            int clientsId = Convert.ToInt32(dr["Client_Id"]);
                            var client    = _clientRepository.GetById(Convert.ToInt32(dr["Client_Id"] ?? 0));
                            model.Client            = client ?? throw new Exception($"client {clientsId} do not exists");
                            model.CompanyName       = client.CompanyName;
                            model.ContractType_Text = Convert.ToString(dr["Contract_Type"]);
                            model.NeededStaff       = Convert.ToString(dr["Needed_Staff"]);
                            model.Role           = Convert.ToString(dr["Role"]);
                            model.Salary_Monthly = Convert.ToString(dr["Salary_Monthly"]);
                            model.Salary_Hourly  = Convert.ToString(dr["Salary_Hourly"]);
                            if (dr["Is_Trasporation_Include"] == null || dr["Is_Trasporation_Include"].ToString().Trim() == "")
                            {
                                throw new Exception("Transporation_Include value can't null");
                            }

                            model.TrasportationIncluded = Convert.ToChar(dr["Is_Trasporation_Include"]);
                            model.Transporationfee      = Convert.ToString(dr["Transporation_Fee_Max"]);
                            model.WorkingdaysPerweek    = Convert.ToString(dr["Working_Days_Per_Week"]);
                            model.Workinghour           = Convert.ToString(dr["Working_Hours_Per_Day"]);
                            if (dr["Postal_Code"] == null || dr["Postal_Code"].ToString().Trim() == "")
                            {
                                throw new Exception("Postal Code value can't be null");
                            }
                            string postalcodestr = Convert.ToString(dr["Postal_Code"]);
                            var    postalCode    = _postalCodeRepository.GetPostalCodeDetail(Convert.ToString(dr["Postal_Code"]));
                            model.PostalCode   = postalCode ?? throw new Exception($"Postal Code {postalcodestr} do not exists");
                            model.provinceName = Convert.ToString(dr["Prefrecture"]);
                            if (dr["Job_Category_Id"] == null || dr["Job_Category_Id"].ToString().Trim() == "")
                            {
                                throw new Exception("Job Category can't be null");
                            }
                            int jobcategoryId = Convert.ToInt32(dr["Job_Category_Id"]);
                            var jobCategory   = _jobCategoryRepository.GetById(Convert.ToInt32(dr["Job_Category_Id"]));

                            model.JobCategory = jobCategory ?? throw new Exception($"jobcategory {jobcategoryId} do not exists");;
                            model.JobTitle    = jobCategory.CategoryName;
                            if (dr["Status"] == null || dr["Status"].ToString().Trim() == "")
                            {
                                throw new Exception("Status value should have 0 or 1");
                            }
                            model.Status             = Convert.ToBoolean(dr["Status"].ToString() == "1" ? 1 : 0);
                            model.BusinessStreamID   = Convert.ToInt32(dr["Business_Stream_Id"]);
                            model.JapaneseLevel_Text = Convert.ToString(dr["Japanese_Level"]);
                            // model.JobTitle_JP = Convert.ToString(dr["Job_Title_JP"]);
                            model.WorkLocationAddress = Convert.ToString(dr["Job_Address"]);
                            model.PostDate            = DateTime.Now;
                            model.JapaneseLevel       = new JapaneseLevel();
                            model.ContractType        = new ContractType();
                            jobList.Add(model);
                        }
                        await _jobsRepository.AddListAsyn(jobList);

                        await _jobsRepository.SaveAsync();

                        TempData["success"] = "Successfully Uploaded";
                    }
                }
                else
                {
                    TempData["error"] = "Please browse excel file (format .xls,.xlsx) to upload and then click on upload button";
                }
            }
            catch (Exception ex)
            {
                TempData["error"] = ex.Message;
            }
            return(RedirectToAction("IndexAsync"));
        }