public async Task <GeneralResource> CreateOperator(OperatorPayload payload) { if (string.IsNullOrEmpty(payload.OperatorName) || string.IsNullOrEmpty(payload.OperatorUrl) || string.IsNullOrEmpty(payload.OperatorImage)) { return new GeneralResource { Status = false, Message = "all payloads are required" } } ; var operat = new Operator { OperatorName = payload.OperatorName, OperatorImage = payload.OperatorImage, OperatorUrl = payload.OperatorUrl }; _db.Operators.Add(operat); if (!await _db.TrySaveChanges()) { return new GeneralResource { Status = false, Message = "Unable to save data to db" } } ; return(new GeneralResource { Status = true, Message = "success" }); } } }
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()); } }