public static async Task saveStream(IHostingEnvironment environment,
                                     HttpContext httpContext,
                                     List <IFormFile> stream_list,
                                     string stream_type,
                                     int errand_id)
 {
     foreach (IFormFile s in stream_list)
     {
         if (s.Length > 0)
         {
             var tmpPath = System.IO.Path.GetTempFileName();
             using (var stream = new FileStream(tmpPath, FileMode.Create))
             {
                 await s.CopyToAsync(stream);
             }
             var toPath = System.IO.Path.Combine(environment.WebRootPath, "uploads", s.FileName);
             toPath = ErrandHelper.idPath(toPath, errand_id, ftype: stream_type);
             toPath = ErrandHelper.GetUniqueFilename(toPath);
             System.IO.File.Move(tmpPath, toPath);
             if (stream_type == "picture")
             {
                 await httpContext.RequestServices.GetRequiredService <IPicture>().SavePicture(new Picture {
                     PictureName = Path.GetFileName(toPath), ErrandId = errand_id
                 });
             }
             else
             {
                 await httpContext.RequestServices.GetRequiredService <ISample>().SaveSample(new Sample {
                     SampleName = Path.GetFileName(toPath), ErrandId = errand_id
                 });
             }
         }
     }
 }
        public async Task <IActionResult> UpdateErrandDetails(ErrandDetailVM errandDetailVM)
        {
            string user           = this._httpContext.User.Identity.Name;
            var    employeeEntity = await this._httpContext.RequestServices.GetRequiredService <IEmployee>().getEmployee(user);

            EmployeeModel employeeModel = employeeEntity;

            //if (user == null)
            //    return View("Login");
            //Create and fill Errand for saving
            Errand errand = new Errand
            {
                ErrandID     = errandDetailVM.errandId,
                DepartmentId = errandDetailVM.departmentId,
                StatusId     = errandDetailVM.statusId,
                EmployeeId   = errandDetailVM.employeeId,
            };

            if (employeeModel.RoleTitle == Employee.EMP_ROLE_COORDINATOR)
            {
                if (errandDetailVM.statusId == ErrandStatus.ST_REPORTED)
                {
                    errand.StatusId = ErrandStatus.ST_STARTED;
                }
            }
            if (employeeModel.RoleTitle == Employee.EMP_ROLE_MANAGER)
            {
                errand.StatusId         = errandDetailVM.noAction == true ? ErrandStatus.ST_NO_ACTION : ErrandStatus.ST_STARTED;
                errand.InvestigatorInfo = errandDetailVM.information + "\n\n" + errandDetailVM.noActionReason;
            }
            if (employeeModel.RoleTitle == Employee.EMP_ROLE_INVESTIGATOR)
            {
                errand.InvestigatorAction = errandDetailVM.events;
                errand.InvestigatorInfo   = errandDetailVM.information;
                errand.StatusId           = errandDetailVM.statusId;
            }
            //Save Errand
            await this._httpContext.RequestServices.GetRequiredService <IErrand>().SaveErrand(errand);

            //Save images
            if (errandDetailVM.loadImage != null)
            {
                await ErrandHelper.saveStream(environment : this._environment, httpContext : this._httpContext, stream_list : errandDetailVM.loadImage, stream_type : "picture", errand_id : errandDetailVM.errandId);
            }
            if (errandDetailVM.loadSample != null)
            {
                await ErrandHelper.saveStream(environment : this._environment, httpContext : this._httpContext, stream_list : errandDetailVM.loadSample, stream_type : "sample", errand_id : errandDetailVM.errandId);
            }
            //Redirect back again
            return(RedirectToAction("ErrandDetails", new { id = errandDetailVM.errandId }));
        }