Exemple #1
0
        public async Task <ActionResult> Create(OperatorPayload payload, HttpPostedFileBase operatorImage)
        {
            try
            {
                //save image

                if (operatorImage.ContentLength == 0)
                {
                    ModelState.AddModelError("", "operator Image is required");
                    return(View(payload));
                }
                int            MaxContentLength      = 1024 * 1024 * 2;
                IList <string> AllowedFileExtensions = new List <string> {
                    ".jpg", ".gif", ".png", ".jfif", ".jpeg"
                };
                FileInfo fi = new FileInfo(operatorImage.FileName);
                if (!AllowedFileExtensions.Contains(fi.Extension.ToLower()))
                {
                    ModelState.AddModelError("", "allowed extension:.jpg, .gif, .png, .jfif, .jpeg ");
                    return(View(payload));
                }
                if (operatorImage.ContentLength > MaxContentLength)
                {
                    ModelState.AddModelError("", "Please select payment receipt screenshot less than 2mb");
                    return(View(payload));
                }
                var imagePath = UploadPaymentReceipt(operatorImage);
                if (string.IsNullOrEmpty(imagePath))
                {
                    ModelState.AddModelError("", "unable to upload image");
                    return(View(payload));
                }
                payload.OperatorImage = imagePath;
                //valida url of operator
                if (!ValidateUrl(payload.OperatorUrl))
                {
                    ModelState.AddModelError("", "inavlid Url provided");
                    return(View(payload));
                }
                if (ModelState.IsValid)
                {
                    var result = await _operatorService.CreateOperator(payload);

                    if (result.Status)
                    {
                        TempData["alert_data"] = new Alert(result.Message, AlertStatus.Success);
                    }
                    TempData["alert_data"] = new Alert(result.Message, AlertStatus.Error);
                }
                // TODO: Add insert logic here

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }