示例#1
0
        // GET: FileImport
        public ActionResult FileUpload(Guid?businessLocationId, Guid businessId)
        {
            FileImportDTO model = new FileImportDTO();

            model.BusinessLocationId = (!businessLocationId.HasValue) ? model.BusinessLocationId : businessLocationId.Value;
            model.BusinessId         = businessId;

            return(PartialView(model));
        }
示例#2
0
        // POST: api/FileImportAPI
        public LogFileDTO PostFile(FileImportDTO filedto)
        {
            if (ModelState.IsValid)
            {
                if (ClaimsAuthorization.CheckAccess("Put", "BusinessLocationId", filedto.BusinessLocationId.ToString()))
                {
                    MemoryStream stream = new MemoryStream(filedto.FileUpload);

                    if (filedto.DataType == UploadTypesDTO.Employees)
                    {
                        Tuple <List <EmployeeDTO>, LogFileDTO> result = ValidateCSV(stream, filedto.BusinessLocationId);

                        if (result.Item2.ErrorLines == 0)
                        {
                            foreach (EmployeeDTO employeeDTO in result.Item1)
                            {
                                employeeDTO.BusinessId         = filedto.BusinessId;
                                employeeDTO.BusinessLocationId = filedto.BusinessLocationId;

                                using (var employeeAPIController = new EmployeeAPIController())
                                {
                                    employeeAPIController.CreateNewEmployee(employeeDTO, true);
                                }

                                result.Item2.LoadedSuccesfully++;
                            }
                        }
                        return(result.Item2);
                    }
                    else if (filedto.DataType == UploadTypesDTO.Roles)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        return(new LogFileDTO
                        {
                            Status = "Failed",
                            LinesRead = 0
                        });
                    }
                }
                else
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have appropriate permission"));
                }
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
示例#3
0
        public ActionResult FileUpload(FileImportDTO filedto, HttpPostedFileBase file)
        {
            if (file != null)
            {
                byte[] fileAsBytes = new byte[file.ContentLength];
                using (BinaryReader theReader = new BinaryReader(file.InputStream))
                {
                    fileAsBytes = theReader.ReadBytes(file.ContentLength);
                }
                filedto.FileUpload = fileAsBytes;
                ModelState["FileUpload"].Errors.Clear();
            }

            if (ModelState.IsValid)
            {
                using (HttpClientWrapper httpClient = new HttpClientWrapper(Session))
                {
                    var responseMessage = httpClient.PostAsJsonAsync("/api/FileImportAPI", filedto).Result;

                    if (responseMessage.IsSuccessStatusCode)
                    {
                        CacheManager.Instance.Remove(CacheManager.CACHE_KEY_BUSINESS + filedto.BusinessId.ToString());                  //Remove the stale business item from the cache
                        CacheManager.Instance.Remove(CacheManager.CACHE_KEY_BUSINESS_LOCATION + filedto.BusinessLocationId.ToString()); //Remove the stale business location item from the cache

                        var ret = JsonConvert.DeserializeObject <LogFileDTO>(responseMessage.Content.ReadAsStringAsync().Result);

                        return(Json(ret, JsonRequestBehavior.DenyGet));
                    }
                    else
                    {
                        //If and error occurred add details to model error.
                        var error = JsonConvert.DeserializeObject <System.Web.Http.HttpError>(responseMessage.Content.ReadAsStringAsync().Result);
                        ModelState.AddModelError(String.Empty, error.Message);
                    }
                }
            }

            return(Json(new { Status = "Failed" }, JsonRequestBehavior.DenyGet));
        }