Exemple #1
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));
        }
Exemple #2
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)));
        }