Ejemplo n.º 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)));
        }
        // GET: Admin/Pictures/Create
        public async Task <IActionResult> Create(int screenId, bool isBackgroundImage)
        {
            var vm = new PictureCreateViewModel
            {
                IsBackgroundPicture = isBackgroundImage,
                ScreenId            = screenId
            };

            if (!isBackgroundImage)
            {
                vm.PromotionSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(false));
                var screen = await _bll.Screens.FindAsync(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));
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        public static DalDto.Screen MapFromExternal(BllDto.Screen screen)
        {
            var res = screen == null ? null : new DalDto.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.GetIntValue(screen.ShowScheduleSeconds)
            };

            return(res ?? default !);
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
0
        public static DalDto.PictureInScreen MapFromExternal(BllDto.PictureInScreen pictureInScreen)
        {
            var res = pictureInScreen == null ? null : new DalDto.PictureInScreen
            {
                Id                  = pictureInScreen.Id,
                CreatedAt           = pictureInScreen.CreatedAt,
                ChangedAt           = pictureInScreen.ChangedAt,
                CreatedBy           = pictureInScreen.CreatedBy,
                ChangedBy           = pictureInScreen.ChangedBy,
                PictureId           = pictureInScreen.PictureId,
                Picture             = PictureMapper.MapFromExternal(pictureInScreen.Picture),
                ScreenId            = pictureInScreen.ScreenId,
                Screen              = ScreenMapper.MapFromExternal(pictureInScreen.Screen),
                IsBackgroundPicture = pictureInScreen.IsBackgroundPicture,
                ShowAddSeconds      = SecondsValueManager.GetIntValue(pictureInScreen.ShowAddSeconds)
            };

            return(res ?? default !);
        }
Ejemplo n.º 7
0
        // GET: Schedules
        public async Task <IActionResult> Index()
        {
            var screen = await _bll.Screens.GetFirstAndActiveScreenAsync();

            if (screen == null)
            {
                if (User != null && _signInManager.IsSignedIn(User))
                {
                    return(RedirectToAction("Index", "ScreenSettings", new { Area = "Admin", showNoActiveScreenAlert = true }));
                }

                return(RedirectToAction("Index", "Home", new { noActiveScreen = true }));
            }

            var scheduleInScreen =
                await _bll.ScheduleInScreens.FindForScreenForDateWithoutIncludesAsync(screen.Id, screen.Prefix, DateTime.Today);

            var vm = new TimetableIndexViewModel
            {
                Events              = new List <EventForTimetable>(),
                Promotions          = new List <PromotionsForTimetable>(),
                BackgroundPicture   = (await _bll.PictureInScreens.GetBackgroundPictureForScreen(screen.Id))?.Picture,
                ShowScheduleSeconds = SecondsValueManager.GetIntValue(screen.ShowScheduleSeconds),
                WeekNumber          = 0
            };

            // Get all promotions
            var pictureInScreens = await _bll.PictureInScreens.GetAllPromotionsForTimetableAsync(screen.Id);

            var promotions = pictureInScreens.ToList();

            if (promotions.Any())
            {
                vm.Promotions = promotions.ToList();
            }

            if (scheduleInScreen != null)
            {
                var schedule             = scheduleInScreen.Schedule;
                var lecturesForTimetable = (await _bll.SubjectInSchedules.GetAllSubjectsForTimetableByScheduleIdWithoutFinishedAsync(schedule.Id, DateTime.Now)).ToList();

                foreach (var lectureForTimetable in lecturesForTimetable)
                {
                    var teacherNames = new List <string>();
                    (await _bll.TeacherInSubjectEvents.GetAllTeachersForSubjectEventWithoutSubjInclude(lectureForTimetable.SubjectInScheduleId)).ToList().ForEach(e => teacherNames.Add(e.Teacher.TeacherName));
                    lectureForTimetable.Lecturers = string.Join(", ", teacherNames);
                }
                vm.LecturesForTimetable = lecturesForTimetable;
                var eventsInSchedule = (await _bll.EventInSchedules.GetAllEventsForCurrentScheduleAsync(scheduleInScreen.Schedule.Id)).ToList();
                foreach (var eventInSchedule in eventsInSchedule)
                {
                    vm.Events.Add(eventInSchedule);
                }

                vm.WeekNumber = schedule.WeekNumber;
            }
            else
            {
                vm.LecturesForTimetable = new List <SubjectForTimetableAndSettings>();
            }

            return(View(vm));
        }
Ejemplo n.º 8
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"));
        }