Exemple #1
0
        public async Task <IActionResult> AddSocialHeader(SocialItemViewModel viewmodel)
        {
            if (viewmodel == null)
            {
                viewmodel = new SocialItemViewModel();
            }

            byte[] imageBytes = null;

            try
            {
                using var ms = new System.IO.MemoryStream();
                await viewmodel.UploadedImage.CopyToAsync(ms);

                imageBytes = ms.ToArray();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(nameof(viewmodel.UploadedImage), ex.Message);
            }

            if (!ModelState.IsValid)
            {
                viewmodel.LanguageList = new SelectList(await _languageService.GetActiveAsync(),
                                                        "Id",
                                                        "Description");
                return(View(viewmodel));
            }

            var header = new SocialHeader
            {
                Name      = viewmodel.Name,
                StartDate = viewmodel.StartDate,
                SiteId    = GetCurrentSiteId()
            };

            header.Socials = new List <Social>
            {
                new Social
                {
                    Description     = viewmodel.Description,
                    ImageAlt        = viewmodel.ImageAlt,
                    LanguageId      = viewmodel.LanguageId,
                    Title           = viewmodel.Title,
                    TwitterUsername = viewmodel.TwitterUsername
                }
            };

            var headerId = await _socialManagementService.AddHeaderAndSocialAsync(header,
                                                                                  viewmodel.UploadedImage.FileName,
                                                                                  imageBytes);

            TempData[$"RowStatus{headerId}"] = "success";

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <int> AddHeaderAndSocialAsync(SocialHeader header,
                                                        string filename,
                                                        byte[] imageBytes)
        {
            VerifyManagementPermission();

            var insertedHeader
                = await _socialHeaderRepository.AddSaveAsync(GetActiveUserId(), header);

            var social = header?.Socials.FirstOrDefault();

            if (social != null)
            {
                social.SocialHeaderId = insertedHeader.Id;
                await AddSocialAsync(social, filename, imageBytes);

                await ClearSocialCache(insertedHeader.Id, social.LanguageId);
            }

            return(insertedHeader.Id);
        }