コード例 #1
0
        public async Task <IActionResult> AddMinistryArm()
        {
            var ministry = new MinistryArmDTO
            {
                OutputHandler = new OutputHandler {
                    IsErrorOccured = false
                }
            };

            return(View(ministry));
        }
コード例 #2
0
        public async Task <IActionResult> UpdateMinistryArm(MinistryArmDTO ministryArm, IFormFile artwork)
        {
            try
            {
                var user = await StaticDataHandler.GetSessionDetails();

                ministryArm.ModifiedBy   = user.Username;
                ministryArm.ModifiedDate = DateTime.Now.AddHours(2);
                if (ModelState.IsValid)
                {
                    if (artwork == null)
                    {
                    }
                    else
                    {
                        var fileUploadResult = await StaticDataHandler.fileUpload(artwork, FolderName, ministryArm.IsFeaturedOnHomePage);

                        if (fileUploadResult.IsErrorOccured)
                        {
                            return(View(ministryArm.OutputHandler = new OutputHandler
                            {
                                IsErrorOccured = true,
                                Message = "Something went wrong"
                            }));
                        }
                        else
                        {
                            ministryArm.ImgBytes = (byte[])fileUploadResult.Result; //return the byte data
                            ministryArm.Filename = artwork.FileName;
                        }
                    }
                    var requestUrl = $"{BaseUrl}{apiUriMinistryArms}/UpdateMinistryArm";
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(requestUrl);
                        client.DefaultRequestHeaders.Accept.Clear();

                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", user.Token);

                        var result = await client.PutAsJsonAsync(client.BaseAddress, ministryArm);

                        if (result.StatusCode == HttpStatusCode.OK)
                        {
                            return(RedirectToAction("ManageMinistryArms"));
                        }
                        else if (result.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            ministryArm.OutputHandler = new OutputHandler
                            {
                                IsErrorOccured = true,
                                Message        = "You're not Authorized to perfom this task"
                            };
                        }
                        else
                        {
                            ministryArm.OutputHandler = await result.Content.ReadAsAsync <OutputHandler>();
                        }
                    };
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Message != null)
                {
                    ministryArm.OutputHandler = new OutputHandler {
                        IsErrorOccured = true, Message = ex.InnerException.Message
                    };
                }
                else
                {
                    ministryArm.OutputHandler = new OutputHandler {
                        IsErrorOccured = true, Message = ex.Message
                    };
                }
            }
            return(View(ministryArm));
        }