public async Task <ActionResult> Create(PostViewModel postViewModel) { if (!ModelState.IsValid) { return(new JsonResult(new { success = false, Response = "", Error = ErrorHelper.GetModelStateErrorMessages(ModelState) })); } if (!_identityHelper.TryGetDistributor(out Distributor distributor, out string errorMessage)) { return(new JsonResult(new { success = false, Response = "", Error = errorMessage })); } try { //uploading post photo to azure storage. string url = await _azureBlobService.UploadPostImage(postViewModel.DistributorId, postViewModel.Id, Request.Form.Files [0].FileName, Request.Form.Files [0].OpenReadStream()); postViewModel.OriginalMediaLink = url; var createdPost = _postAdapter.Create(postViewModel, distributor); return(new JsonResult(new { success = true, Response = createdPost, Error = "" })); } catch (Exception exception) { return(new JsonResult(new { success = false, Response = "", Error = exception.Message })); } }
public async Task <ActionResult> Create([Bind(Include = "Id,DistributorId,PostTitle,PostMessage,MediaType,OriginalMediaLink,ThumbnailLink")] PostViewModel postViewModel) { if (!ModelState.IsValid) { return(Json(new { success = false, Response = "", Error = ErrorHelper.GetModelStateErrorMessages(ModelState) }, JsonRequestBehavior.AllowGet)); } var errorMessage = ""; var distributor = new Distributor(); if (!IdentityHelper.TryGetDistributor(User, out distributor, out errorMessage)) { return(Json(new { success = false, Response = "", Error = errorMessage }, JsonRequestBehavior.AllowGet)); } try { //uploading post photo to azure storage. string url = ConnectToAzure.UploadPostImage(postViewModel.DistributorId, postViewModel.Id, Request.Files[0].FileName, Request.Files[0].InputStream); postViewModel.OriginalMediaLink = url; var createdPost = _postAdapter.Create(postViewModel, distributor); return(Json(new { success = true, Response = createdPost, Error = "" }, JsonRequestBehavior.AllowGet)); } catch (Exception exception) { return(Json(new { success = false, Response = "", Error = exception.Message }, JsonRequestBehavior.AllowGet)); } }