示例#1
0
        public async Task <IActionResult> DeletePromotionFromScreen(int promotionId)
        {
            var promotionInScreen = await _bll.PictureInScreens.FindAsync(promotionId);

            var picturesWithSamePath = await _bll.Pictures.FindPicturesByPathAsync(promotionInScreen.Picture.Path);

            if (picturesWithSamePath == null || !picturesWithSamePath.Any() ||
                (picturesWithSamePath.Count() == 1 && picturesWithSamePath.First().Id == promotionInScreen.PictureId))
            {
                var path = Path.Combine(_appEnvironment.ContentRootPath, "wwwroot",
                                        Path.Combine(promotionInScreen.Picture.Path.Split("/")));

                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
            }

            _bll.Pictures.Remove(promotionInScreen.Picture);
            await _bll.SaveChangesAsync();

            var promotions = await _bll.PictureInScreens.GetAllPromotionsForTimetableAsync(promotionInScreen.ScreenId);

            if (promotions == null || !promotions.Any())
            {
                var screen = await _bll.Screens.FindAsync(promotionInScreen.ScreenId);

                screen.ShowScheduleSeconds = SecondsValueManager.GetSelectedValue(null, true);
                _bll.Screens.Update(screen);
                await _bll.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(Index)));
        }
示例#2
0
        // GET: Admin/Screens/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var screen = await _bll.Screens.FindAsync(id);

            if (screen == null)
            {
                return(NotFound());
            }

            var vm = new ScreenCreateEditViewModel
            {
                Screen = screen,
                ScheduleAlwaysShown = false,
                PictureInScreens    = new List <PictureInScreen>(),
                PromotionSecondsSelectListDictionary = new Dictionary <int, SelectList>(),
                ScheduleSecondsSelectList            = new SelectList(
                    SecondsValueManager.GetDictionaryKeysList(true),
                    screen.ShowScheduleSeconds
                    ),
                ShowScheduleSecondsString            = screen.ShowScheduleSeconds,
                ScreenOldPrefix                      = screen.Prefix,
                ShowPromotionSecondsStringDictionary = new Dictionary <int, string>()
            };

            var promotions = (await _bll.PictureInScreens.GetAllPromotionsForScreenAsync((int)id)).ToList();
            await _bll.SaveChangesAsync();

            foreach (var promotion in promotions)
            {
                vm.PictureInScreens.Add(promotion);
                vm.PromotionSecondsSelectListDictionary[promotion.Id] = new SelectList(
                    SecondsValueManager.GetDictionaryKeysList(false),
                    promotion.ShowAddSeconds);
                vm.ShowPromotionSecondsStringDictionary.Add(promotion.Id, promotion.ShowAddSeconds);
            }

            if (vm.Screen.ShowScheduleSeconds.Equals(SecondsValueManager.GetSelectedValue(null, true)) &&
                vm.PictureInScreens.TrueForAll(p => p.ShowAddSeconds.Equals(SecondsValueManager.GetSelectedValue(null, false))))
            {
                vm.ScheduleAlwaysShown = true;
            }

            return(View(vm));
        }
示例#3
0
        public static BllDto.Screen MapFromInternal(DalDto.Screen screen)
        {
            var res = screen == null ? null : new BllDto.Screen
            {
                Id = screen.Id,
                UniqueIdentifier    = screen.UniqueIdentifier,
                CreatedAt           = screen.CreatedAt,
                ChangedAt           = screen.ChangedAt,
                CreatedBy           = screen.CreatedBy,
                ChangedBy           = screen.ChangedBy,
                Prefix              = screen.Prefix,
                IsActive            = screen.IsActive,
                ShowScheduleSeconds = SecondsValueManager.GetSelectedValue(screen.ShowScheduleSeconds, true)
            };

            return(res ?? default !);
        }
示例#4
0
        // GET: Admin/Screens/Create
        public async Task <IActionResult> Create()
        {
            if (await _bll.AppUsersScreens.GetScreenForUserAsync(_userManager.GetUserId(User)) != null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            if (await _bll.Screens.GetFirstAndActiveScreenAsync() != null)
            {
                return(RedirectToAction(nameof(ScreenNotAssigned)));
            }

            var screen = new Screen();

            screen.ShowScheduleSeconds = SecondsValueManager.GetSelectedValue(null, true);
            return(View(screen));
        }
        public static BllDto.PictureInScreen MapFromInternal(DalDto.PictureInScreen pictureInScreen)
        {
            var res = pictureInScreen == null ? null : new BllDto.PictureInScreen
            {
                Id                  = pictureInScreen.Id,
                CreatedAt           = pictureInScreen.CreatedAt,
                ChangedAt           = pictureInScreen.ChangedAt,
                CreatedBy           = pictureInScreen.CreatedBy,
                ChangedBy           = pictureInScreen.ChangedBy,
                PictureId           = pictureInScreen.PictureId,
                Picture             = PictureMapper.MapFromInternal(pictureInScreen.Picture),
                ScreenId            = pictureInScreen.ScreenId,
                Screen              = ScreenMapper.MapFromInternal(pictureInScreen.Screen) !,
                IsBackgroundPicture = pictureInScreen.IsBackgroundPicture,
                ShowAddSeconds      = SecondsValueManager.GetSelectedValue(pictureInScreen.ShowAddSeconds, false)
            };

            return(res ?? default !);
        }
示例#6
0
        public async Task <IActionResult> Edit(int id, ScreenCreateEditViewModel vm)
        {
            if (id != vm.Screen.Id)
            {
                return(NotFound());
            }

            vm.Screen.ChangedAt = DateTime.Now;
            vm.Screen.ChangedBy = _userManager.GetUserId(User);

            // If there is no promotions - show schedule always
            vm.Screen.ShowScheduleSeconds = vm.ShowPromotionSecondsStringDictionary == null
                ? SecondsValueManager.GetSelectedValue(null, true)
                : vm.ShowScheduleSecondsString;

            if (vm.PictureInScreens == null)
            {
                vm.PictureInScreens = new List <PictureInScreen>();
            }

            if (vm.PromotionSecondsSelectListDictionary == null)
            {
                vm.PromotionSecondsSelectListDictionary = new Dictionary <int, SelectList>();
            }

            if (vm.ScheduleSecondsSelectList == null)
            {
                vm.ScheduleSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(true));
            }

            if (vm.ShowPromotionSecondsStringDictionary != null)
            {
                foreach (var promotionId in vm.ShowPromotionSecondsStringDictionary.Keys)
                {
                    var promotion = await _bll.PictureInScreens.FindAsync(promotionId);

                    promotion.ShowAddSeconds = vm.ShowPromotionSecondsStringDictionary[promotionId];
                    vm.PictureInScreens.Add(promotion);
                }

                foreach (var pictureInScreen in vm.PictureInScreens)
                {
                    vm.PromotionSecondsSelectListDictionary[pictureInScreen.Id] = new SelectList(
                        SecondsValueManager.GetDictionaryKeysList(false),
                        vm.ShowPromotionSecondsStringDictionary[pictureInScreen.Id]);
                }
            }
            else
            {
                var promotions = (await _bll.PictureInScreens.GetAllPromotionsForScreenAsync(id)).ToList();
                await _bll.SaveChangesAsync();

                foreach (var promotion in promotions)
                {
                    promotion.ShowAddSeconds = SecondsValueManager.GetSelectedValue(0, false);
                    _bll.PictureInScreens.Update(promotion);
                }
            }

            // Before model validation set values to the following parameters to pass model validation.
            vm.ShowPromotionSecondsStringDictionary ??= new Dictionary <int, string>();
            vm.ShowScheduleSecondsString ??= SecondsValueManager.GetSelectedValue(null, true);

            ModelState.Clear();
            TryValidateModel(vm.Screen);
            if (ModelState.IsValid)
            {
                try
                {
                    foreach (var pictureInScreen in vm.PictureInScreens)
                    {
                        _bll.PictureInScreens.Update(pictureInScreen);
                    }

                    _bll.Screens.Update(vm.Screen);
                    await _bll.SaveChangesAsync();

                    if (vm.ScreenOldPrefix != vm.Screen.Prefix)
                    {
                        await ScheduleUpdateService.GetAndSaveScheduleForScreen(_bll, _userManager.GetUserId(User), vm.Screen);
                    }

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ScreenExists(vm.Screen.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(RedirectToAction(nameof(Edit)));
        }
        public async Task <IActionResult> Create(PictureCreateViewModel vm, IFormFile file)
        {
            if (!vm.PictureFromUrl || (vm.PictureFromUrl && string.IsNullOrEmpty(vm.Picture.Path) && file != null && file.Length > 0))
            {
                vm.Picture.Path = "";
                ModelState.Clear();
                TryValidateModel(vm.Picture);
                if (file == null || file.Length < 0)
                {
                    ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileMissing);
                }
                else
                {
                    if (file.Length > 20971520)
                    {
                        ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileTooBigError);
                    }

                    var fileExtension = Path.GetExtension(file.FileName);
                    if (!_imageExtensions.Contains(fileExtension.ToUpper()))
                    {
                        ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileFormatError + string.Join(',', _imageExtensions) + ".");
                    }
                }

                if (ModelState.ErrorCount > 0)
                {
                    if (!vm.IsBackgroundPicture)
                    {
                        vm.PromotionSecondsSelectList = new SelectList(
                            SecondsValueManager.GetDictionaryKeysList(false),
                            vm.ShowPromotionSecondsString);
                        var screen = await _bll.Screens.FindAsync(vm.ScreenId);

                        await _bll.SaveChangesAsync();

                        if (screen.ShowScheduleSeconds != null)
                        {
                            vm.ScheduleSecondsSelectList = new SelectList(
                                SecondsValueManager.GetDictionaryKeysList(true),
                                screen.ShowScheduleSeconds
                                );
                        }
                        else
                        {
                            vm.ScheduleSecondsSelectList =
                                new SelectList(SecondsValueManager.GetDictionaryKeysList(true));
                        }
                    }

                    return(View(vm));
                }

                var innerDirName  = vm.IsBackgroundPicture ? BackgroundDirectory : PromotionsDirectory;
                var directoryPath = Path.Combine(_appEnvironment.ContentRootPath, "wwwroot", "Images", innerDirName);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
                var path = Path.Combine(directoryPath, file.FileName);
                vm.Picture.Path = path.Substring(path.IndexOf("Images", StringComparison.Ordinal) - 1).Replace("\\", "/");
                try
                {
                    await using var fileStream = new FileStream(path, FileMode.Create);
                    await file.CopyToAsync(fileStream);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }

            var userId = _userManager.GetUserId(User);

            vm.Picture.CreatedAt = vm.Picture.ChangedAt = DateTime.Now;
            vm.Picture.CreatedBy = vm.Picture.ChangedBy = userId;

            var guid = await _bll.Pictures.AddAsync(vm.Picture);

            await _bll.SaveChangesAsync();

            var    picture        = _bll.Pictures.GetUpdatesAfterUowSaveChanges(guid);
            string?showAddSeconds = SecondsValueManager.GetSelectedValue(null, false);

            // Promotion was added and ShowScheduleSeconds and ShowAddSeconds values were selected
            if (vm.IsBackgroundPicture == false && vm.ShowPromotionSecondsString != null && vm.ShowScheduleSecondsString != null)
            {
                showAddSeconds = vm.ShowPromotionSecondsString;
                var screen = await _bll.Screens.FindAsync(vm.ScreenId);

                screen.ShowScheduleSeconds = vm.ShowScheduleSecondsString;
                _bll.Screens.Update(screen);
            }

            await _bll.PictureInScreens.AddAsync(new PictureInScreen
            {
                CreatedAt           = DateTime.Now,
                ChangedAt           = DateTime.Now,
                ChangedBy           = userId,
                CreatedBy           = userId,
                IsBackgroundPicture = vm.IsBackgroundPicture,
                ScreenId            = vm.ScreenId,
                PictureId           = picture.Id,
                ShowAddSeconds      = showAddSeconds
            });

            await _bll.SaveChangesAsync();

            return(RedirectToAction("Index", "ScreenSettings"));
        }