Beispiel #1
0
        public async Task <IActionResult> ViewSocial(int socialHeaderId, int languageId)
        {
            var socialHeader = await _socialManagementService
                               .GetHeaderAndSocialAsync(socialHeaderId, languageId);

            if (socialHeader == null)
            {
                return(RedirectToAction(nameof(AddSocialHeader)));
            }

            var language = await _languageService.GetActiveByIdAsync(languageId);

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

            var viewmodel = new SocialItemViewModel
            {
                Description     = social?.Description,
                HeaderId        = socialHeaderId,
                ImageAlt        = social?.ImageAlt,
                ImageLink       = social?.ImageLink,
                LanguageId      = languageId,
                LanguageName    = language.Description,
                Name            = socialHeader.Name,
                SocialIsNew     = social == null,
                StartDate       = socialHeader.StartDate,
                Title           = social?.Title,
                TwitterUsername = social?.TwitterUsername
            };

            if (social?.ImageLink != null &&
                social.ImageLink.Contains('/', StringComparison.OrdinalIgnoreCase))
            {
                viewmodel.Filename = social.ImageLink[social.ImageLink.LastIndexOf('/')..];
Beispiel #2
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)));
        }
Beispiel #3
0
        public async Task <IActionResult> AddSocialHeader()
        {
            var viewmodel = new SocialItemViewModel
            {
                LanguageList = new SelectList(await _languageService.GetActiveAsync(),
                                              "Id",
                                              "Description")
            };

            if (viewmodel.StartDate == default)
            {
                viewmodel.StartDate = _dateTimeProvider.Now;
            }

            return(View(viewmodel));
        }
Beispiel #4
0
        public async Task <IActionResult> UpdateSocial(SocialItemViewModel viewmodel)
        {
            if (viewmodel == null)
            {
                return(RedirectToAction(nameof(Index)));
            }
            var header = await _socialManagementService.GetHeaderAndSocialAsync(viewmodel.HeaderId,
                                                                                viewmodel.LanguageId);

            if (header.Name != viewmodel.Name || header.StartDate != viewmodel.StartDate)
            {
                await _socialManagementService.UpdateHeaderAsync(viewmodel.HeaderId,
                                                                 viewmodel.Name,
                                                                 viewmodel.StartDate);
            }

            var social = new Social
            {
                Description     = viewmodel.Description,
                ImageAlt        = viewmodel.ImageAlt,
                LanguageId      = viewmodel.LanguageId,
                SocialHeaderId  = viewmodel.HeaderId,
                Title           = viewmodel.Title,
                TwitterUsername = viewmodel.TwitterUsername
            };

            if (viewmodel.SocialIsNew)
            {
                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)
                {
                    return(RedirectToAction(nameof(ViewSocial), new
                    {
                        socialHeaderId = viewmodel.HeaderId,
                        languageId = viewmodel.LanguageId
                    }));
                }

                await _socialManagementService.AddSocialAsync(social,
                                                              viewmodel.UploadedImage.FileName,
                                                              imageBytes);
            }
            else
            {
                await _socialManagementService.UpdateSocialAsync(social);
            }

            TempData[$"RowStatus{viewmodel.HeaderId}"] = "success";

            return(RedirectToAction(nameof(Index)));
        }