public async Task <IActionResult> Edit(int?id)
        {
            int    eventId = ViewBag.EventId;
            string userId  = ViewBag.UserId;

            if (id == null)
            {
                return(NotFound());
            }
            MediaDelegation selectedMedia = await _context.MediaDelegations.GetAsync((int)id);

            if (selectedMedia == null)
            {
                return(RedirectToAction(nameof(Delegations)));
            }

            MediaDelegation mediaDelegation = _context.MediaDelegations.
                                              GetDelegationByEventId(eventId, (int)id);


            IEnumerable <Country> countries = await _context.Countries.GetAllAsync();


            MediaDelegationIndexModel pim = new MediaDelegationIndexModel
            {
                Countries       = countries,
                MediaDelegation = mediaDelegation,
                MediaCategories = await _context.MediaCategories.GetWithRelatedDataAsync()
            };

            return(View(pim));
        }
        public async Task <IActionResult> Edit(MediaDelegationAdminEditModel part)
        {
            MediaDelegation mediaDelegation = await _context.MediaDelegations.GetWithRelatedDataByIdAsync(part.Id);

            if (mediaDelegation == null)
            {
                return(RedirectToAction(nameof(Delegations)));
            }

            if (part.Photo_image_path == null)
            {
                part.Photo_image_path = new FormFile(null, 0, 0, mediaDelegation.PhotoPath, mediaDelegation.PhotoPath);
            }

            if (ModelState.IsValid)
            {
                MediaSubCategory mediaSubCategory = await _context.MediaSubCategories.GetAsync(part.MediaSubCategoryId);

                Country country = await _context.Countries.GetAsync(part.CountryId);

                if (mediaSubCategory == null || country == null)
                {
                    ModelState.AddModelError("err", "Media sub Category or Country is invalid");
                    return(RedirectToAction(nameof(Delegations)));
                }


                int    eventId   = ViewBag.EventId;
                string appUserId = ViewBag.UserId;
                string path      = Path.Combine(_hostingEnvironment.WebRootPath, "uploads", eventId.ToString(), "media");


                Guid gPhoto          = Guid.NewGuid();
                bool willSendMessage = false;

                try
                {
                    if (part.Press_image_path != null)
                    {
                        string pressCard     = part.Press_image_path.ContentType;
                        string cPressCard    = pressCard.Substring(pressCard.IndexOf("/") + 1).Trim();
                        Guid   gPress        = Guid.NewGuid();
                        string fullPathPress = Path.Combine(path, gPress.ToString() + "." + cPressCard);

                        if (mediaDelegation.PressCardPath != null)
                        {
                            _photoService.TryDelete(path, Path.Combine(path, mediaDelegation.PressCardPath));
                        }
                        if (equalsContentType(cPressCard, "png", "jpg", "jpeg", "pdf") == false)
                        {
                            ModelState.AddModelError("err", "File format not supported!!!");
                            return(RedirectToAction(nameof(Delegations)));
                        }
                        mediaDelegation.PressCardPath = String.Format("{0}.{1}", gPress.ToString(), cPressCard);

                        bool isUploadedPress = await _photoService.TryUploadAsync(part.Press_image_path, path, fullPathPress);

                        if (isUploadedPress)
                        {
                            mediaDelegation.PressCardPath = String.Format("{0}.{1}", gPress.ToString(), cPressCard);
                        }
                    }
                    if (mediaDelegation.MediaStatusType != part.MediaStatusType)
                    {
                        willSendMessage = true;//status is different..we must send a message...
                    }
                    if (part.Photo_image_path.FileName == mediaDelegation.PhotoPath)
                    {
                        mediaDelegation.CountryId          = part.CountryId;
                        mediaDelegation.Email              = part.Email;
                        mediaDelegation.FirstName          = part.FirstName;
                        mediaDelegation.LastName           = part.LastName;
                        mediaDelegation.MediaSubCategoryId = part.MediaSubCategoryId;
                        mediaDelegation.MobilePhone        = part.MobilePhone;
                        mediaDelegation.Nationality        = part.Nationality;
                        mediaDelegation.MediaStatusType    = part.MediaStatusType;
                        mediaDelegation.Comment            = part.Comment;
                        mediaDelegation.MediaTitle         = part.MediaTitle;
                        _context.Update <MediaDelegation>(mediaDelegation);
                        if (willSendMessage)
                        {
                            string mailMessage = String.Empty;
                            if (part.MediaStatusType == MediaStatusType.Rejected)
                            {
                                mailMessage = System.IO.File.ReadAllText(Path.Combine(_hostingEnvironment.WebRootPath, "templates", "MediaFail.html"));
                            }
                            else if (part.MediaStatusType == MediaStatusType.Accepted)
                            {
                                mailMessage = System.IO.File.ReadAllText(Path.Combine(_hostingEnvironment.WebRootPath, "templates", "MediaSuccess.html"));
                            }
                            else if (part.MediaStatusType == MediaStatusType.Investigating)
                            {
                                mailMessage = System.IO.File.ReadAllText(Path.Combine(_hostingEnvironment.WebRootPath, "templates", "MediaQueue.html"));
                            }
                            mailMessage = mailMessage.Replace("<%FirstName%>", part.FirstName);
                            mailMessage = mailMessage.Replace("<%LastName%>", part.LastName);
                            mailMessage = mailMessage.Replace("<%eventName%>", ViewBag.CurrentEvent.Name);
                            _emailSender.SendEmailAsync(part.Email, "Media Registration for " + String.Format("{0} {1}", part.FirstName, part.LastName), mailMessage, (int)ViewBag.EventId, true);
                        }

                        await _context.SaveAsync();
                    }
                    else
                    {
                        string photo = part.Photo_image_path.ContentType;
                        string cType = photo.Substring(photo.IndexOf("/") + 1).Trim();



                        if (equalsContentType(cType, "png", "jpg", "jpeg", "pdf") == false)
                        {
                            ModelState.AddModelError("err", "File format not supported!!!");
                            return(RedirectToAction(nameof(Delegations)));
                        }

                        string fullPathPhoto = Path.Combine(path, gPhoto.ToString() + "." + cType);

                        bool IsDeleted       = _photoService.TryDelete(path, Path.Combine(path, mediaDelegation.PhotoPath));
                        bool isUploadedPhoto = await _photoService.TryUploadAsync(part.Photo_image_path, path, fullPathPhoto);

                        if (isUploadedPhoto && IsDeleted)
                        {
                            mediaDelegation.CountryId          = part.CountryId;
                            mediaDelegation.Email              = part.Email;
                            mediaDelegation.FirstName          = part.FirstName;
                            mediaDelegation.LastName           = mediaDelegation.LastName;
                            mediaDelegation.MediaSubCategoryId = part.MediaSubCategoryId;
                            mediaDelegation.MobilePhone        = part.MobilePhone;
                            mediaDelegation.Nationality        = part.Nationality;
                            mediaDelegation.MediaStatusType    = part.MediaStatusType;
                            mediaDelegation.Comment            = part.Comment;
                            mediaDelegation.MediaTitle         = part.MediaTitle;
                            mediaDelegation.PhotoPath          = String.Format("{0}.{1}", gPhoto.ToString(), cType);
                            _context.Update <MediaDelegation>(mediaDelegation);
                            await _context.SaveAsync();

                            if (willSendMessage)
                            {
                                string mailMessage = String.Empty;
                                if (part.MediaStatusType == MediaStatusType.Rejected)
                                {
                                    mailMessage = System.IO.File.ReadAllText(Path.Combine(_hostingEnvironment.WebRootPath, "templates", "`"));
                                }
                                else if (part.MediaStatusType == MediaStatusType.Accepted)
                                {
                                    mailMessage = System.IO.File.ReadAllText(Path.Combine(_hostingEnvironment.WebRootPath, "templates", "MediaSuccess.html"));
                                }
                                else if (part.MediaStatusType == MediaStatusType.Investigating)
                                {
                                    mailMessage = System.IO.File.ReadAllText(Path.Combine(_hostingEnvironment.WebRootPath, "templates", "MediaQueue.html"));
                                }

                                mailMessage = mailMessage.Replace("<%FirstName%>", part.FirstName);
                                mailMessage = mailMessage.Replace("<%LastName%>", part.LastName);
                                mailMessage = mailMessage.Replace("<%eventName%>", ViewBag.CurrentEvent.Name);
                                _emailSender.SendEmailAsync(part.Email, "Media Registration for " + String.Format("{0} {1}", part.FirstName, part.LastName), mailMessage, (int)ViewBag.EventId, true);
                            }
                        }
                    }
                }
                catch (Exception exp)
                {
                    ModelState.AddModelError("err", exp.Message);
                }
            }

            return(RedirectToAction(nameof(Delegations)));
        }