// 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));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(Screen screen)
        {
            screen.CreatedAt        = screen.ChangedAt = DateTime.Now;
            screen.CreatedBy        = screen.ChangedBy = _userManager.GetUserId(User);
            screen.UniqueIdentifier = Guid.NewGuid().ToString();
            ModelState.Clear();
            TryValidateModel(screen);
            if (ModelState.IsValid)
            {
                var guid = await _bll.Screens.AddAsync(screen);

                await _bll.SaveChangesAsync();

                var updatedScreen = _bll.Screens.GetUpdatesAfterUowSaveChanges(guid);

                await _bll.AppUsersScreens.AddAsync(new AppUsersScreen
                {
                    CreatedAt = DateTime.Now,
                    ChangedAt = DateTime.Now,
                    AppUserId = Guid.Parse(_userManager.GetUserId(User)),
                    ScreenId  = updatedScreen.Id
                });

                await _bll.SaveChangesAsync();

                await ScheduleUpdateService.GetAndSaveScheduleForScreen(_bll, _userManager.GetUserId(User), updatedScreen);

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

            return(View(screen));
        }
Esempio n. 3
0
        public async Task <IActionResult> Create(Event createdEvent)
        {
            if (createdEvent.EndDateTime < createdEvent.StartDateTime)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.EventView.Events.EventTimeDifferenceError);
            }
            if (createdEvent.ShowEndDateTime <= createdEvent.ShowStartDateTime)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.EventView.Events.EventShowTimeDifferenceError);
            }

            if (ModelState.IsValid)
            {
                if (ModelState.ErrorCount > 0)
                {
                    return(View(createdEvent));
                }
                createdEvent.CreatedAt = DateTime.Now;
                createdEvent.CreatedBy = _userManager.GetUserId(User);
                var eventGuid = await _bll.Events.AddAsync(createdEvent);

                await _bll.SaveChangesAsync();

                if (DateTime.Now >= createdEvent.ShowStartDateTime && DateTime.Now < createdEvent.ShowEndDateTime)
                {
                    var eventId = _bll.Events.GetUpdatesAfterUowSaveChanges(eventGuid).Id;
                    var screen  = await _bll.Screens.GetFirstAndActiveScreenAsync();

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

                        if (scheduleInScreen != null)
                        {
                            var eventInSchedule = new EventInSchedule
                            {
                                CreatedAt  = DateTime.Now,
                                CreatedBy  = _userManager.GetUserId(User),
                                EventId    = eventId,
                                ScheduleId = scheduleInScreen.ScheduleId
                            };
                            await _bll.EventInSchedules.AddAsync(eventInSchedule);

                            await _bll.SaveChangesAsync();
                        }
                    }
                }
                return(RedirectToAction("Index", "ScheduleAndEvents"));
            }

            return(View(createdEvent));
        }
Esempio n. 4
0
        public async Task <IActionResult> DeleteEventOrSubject(int id, bool isSubject)
        {
            if (isSubject)
            {
                _bll.SubjectInSchedules.Remove(id);
            }
            else
            {
                _bll.Events.Remove(id);
            }
            await _bll.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 5
0
        public async Task <IActionResult> Create(AppUserCreateViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var user = new AppUser
                {
                    UserName  = vm.Email,
                    Email     = vm.Email,
                    FirstName = vm.FirstName,
                    LastName  = vm.LastName,
                    ChangedAt = DateTime.Now,
                    CreatedAt = DateTime.Now,
                    CreatedBy = _userManager.GetUserId(User),
                    ChangedBy = _userManager.GetUserId(User)
                };

                var result = await _userManager.CreateAsync(user, vm.Password);

                if (result.Succeeded)
                {
                    var errorList = new List <string?>()
                    {
                        vm.ScheduleManagement
                            ? await AddUserToRoleAsync(user, nameof(RoleNamesEnum.ScheduleSettingsAdmin))
                            : null,
                        vm.ScreenManagement
                            ? await AddUserToRoleAsync(user, nameof(RoleNamesEnum.ScreenSettingsAdmin))
                            : null,
                        vm.EventsManagement
                            ? await AddUserToRoleAsync(user, nameof(RoleNamesEnum.EventSettingsAdmin))
                            : null
                    };
                    if (errorList.TrueForAll(e => e == null))
                    {
                        var screen = await _bll.Screens.AllAsync();

                        // If there is only one screen!
                        var screens = screen.ToList();

                        if (screens.Count == 1)
                        {
                            await _bll.AppUsersScreens.AddAsync(new AppUsersScreen
                            {
                                CreatedAt = DateTime.Now,
                                CreatedBy = _userManager.GetUserId(User),
                                AppUserId = user.Id,
                                ScreenId  = screens.First().Id
                            });

                            await _bll.SaveChangesAsync();
                        }

                        var passwordCode = await _userManager.GeneratePasswordResetTokenAsync(user);

                        passwordCode = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(passwordCode));

                        var accountCode = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                        accountCode = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(accountCode));

                        var callbackUrl = Url.Action(
                            "ActivateAccountAndResetPassword",
                            "Account",
                            new { Area = "", accountCode, passwordCode },
                            Request.Scheme);

                        var htmlMessageText = "<h4>Timeable registration notification!</h4>" +
                                              "<p>You have been registered to Timeable application! " +
                                              $"Your email can be verified by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking this link</a>.</p>";

                        await _emailSender.SendEmailAsync(
                            vm.Email,
                            "You have been registered to Timeable",
                            htmlMessageText);

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

                    foreach (var error in errorList)
                    {
                        ModelState.AddModelError("UserAdditionFailed", error);
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("UserAdditionFailed", error.Description);
                }
            }

            return(View(vm));
        }
        public static async Task GetAndSaveScheduleForScreen(IBLLApp bll, string userId, Screen screen)
        {
            var timeplanGettingSystem = new GetTimePlanFromInformationSystem(screen.Prefix);
            var schedule = timeplanGettingSystem.GetScheduleForToday();

            var bllSchedule =
                ScheduleMapper.MapFromInternal(DAL.App.Mappers.ScheduleMapper.MapFromDomain(schedule));

            bllSchedule.Prefix = screen.Prefix;

            var scheduleGuid = await bll.Schedules.AddAsync(bllSchedule);

            await bll.SaveChangesAsync();

            var scheduleIdAfterSaveChanges = bll.Schedules.GetUpdatesAfterUowSaveChanges(scheduleGuid).Id;

            var subjects = schedule.SubjectsInSchedules;

            if (subjects != null)
            {
                foreach (var subjectInSchedule in subjects)
                {
                    int subjectInScheduleId;
                    var subjectInScheduleThatAlreadyExists =
                        await bll.SubjectInSchedules.FindByUniqueIdentifierAsync(subjectInSchedule.UniqueIdentifier);

                    if (subjectInScheduleThatAlreadyExists != null)
                    {
                        subjectInScheduleThatAlreadyExists.ScheduleId = scheduleIdAfterSaveChanges;
                        bll.SubjectInSchedules.Update(subjectInScheduleThatAlreadyExists);
                        await bll.SaveChangesAsync();

                        subjectInScheduleId = subjectInScheduleThatAlreadyExists.Id;
                    }
                    else
                    {
                        var bllSubjectInSchedule = new SubjectInSchedule
                        {
                            CreatedAt        = DateTime.Now,
                            CreatedBy        = userId,
                            Rooms            = subjectInSchedule.Rooms,
                            Groups           = subjectInSchedule.Groups,
                            UniqueIdentifier = subjectInSchedule.UniqueIdentifier,
                            StartDateTime    = subjectInSchedule.StartDateTime,
                            EndDateTime      = subjectInSchedule.EndDateTime,
                            SubjectType      = subjectInSchedule.SubjectType,
                            ScheduleId       = scheduleIdAfterSaveChanges
                        };

                        var subject = await bll.Subjects
                                      .FindBySubjectNameAndCodeAsync(subjectInSchedule.Subject.SubjectName,
                                                                     subjectInSchedule.Subject.SubjectCode);

                        if (subject != null)
                        {
                            bllSubjectInSchedule.SubjectId = subject.Id;
                            bllSubjectInSchedule.Subject   = null;
                        }
                        else
                        {
                            var bllSubject = new Subject
                            {
                                CreatedAt   = DateTime.Now,
                                CreatedBy   = userId,
                                SubjectCode = subjectInSchedule.Subject.SubjectCode,
                                SubjectName = subjectInSchedule.Subject.SubjectName
                            };
                            var subjectGuid = await bll.Subjects.AddAsync(bllSubject);

                            await bll.SaveChangesAsync();

                            bllSubjectInSchedule.SubjectId = bll.Subjects.GetUpdatesAfterUowSaveChanges(subjectGuid).Id;
                        }
                        var subjInScheduleGuid = await bll.SubjectInSchedules.AddAsync(bllSubjectInSchedule);

                        await bll.SaveChangesAsync();

                        subjectInScheduleId =
                            bll.SubjectInSchedules.GetUpdatesAfterUowSaveChanges(subjInScheduleGuid).Id;
                    }

                    var teachers = new List <Teacher>();

                    if (subjectInSchedule.TeacherInSubjectEvents != null)
                    {
                        foreach (var teacherInSubjectEvent in subjectInSchedule.TeacherInSubjectEvents)
                        {
                            var teacher = await bll.Teachers
                                          .FindTeacherByNameAndRoleAsync(teacherInSubjectEvent.Teacher.FullName,
                                                                         teacherInSubjectEvent.Teacher.Role);

                            if (teacher != null)
                            {
                                teachers.Add(teacher);
                            }
                            else
                            {
                                var newTeacher = new Teacher
                                {
                                    CreatedAt   = DateTime.Now,
                                    CreatedBy   = userId,
                                    TeacherName = teacherInSubjectEvent.Teacher.FullName,
                                    TeacherRole = teacherInSubjectEvent.Teacher.Role
                                };
                                var teacherGuid = await bll.Teachers.AddAsync(newTeacher);

                                await bll.SaveChangesAsync();

                                teachers.Add(bll.Teachers.GetUpdatesAfterUowSaveChanges(teacherGuid));
                            }
                        }
                    }

                    foreach (var teacher in teachers)
                    {
                        bll.TeacherInSubjectEvents.Add(new TeacherInSubjectEvent
                        {
                            CreatedAt           = DateTime.Now,
                            CreatedBy           = userId,
                            TeacherId           = teacher.Id,
                            SubjectInScheduleId = subjectInScheduleId
                        });
                    }
                    await bll.SaveChangesAsync();
                }
            }

            await bll.ScheduleInScreens.AddAsync(new ScheduleInScreen
            {
                CreatedAt  = DateTime.Now,
                CreatedBy  = userId,
                ScreenId   = screen.Id,
                ScheduleId = scheduleIdAfterSaveChanges
            });

            var futureEvents = await bll.Events.GetAllFutureEventsAsync(DateTime.Now);

            foreach (var futureEvent in futureEvents)
            {
                if (futureEvent.ShowStartDateTime <= DateTime.Now && futureEvent.ShowEndDateTime > DateTime.Now)
                {
                    await bll.EventInSchedules.AddAsync(new EventInSchedule
                    {
                        CreatedAt  = DateTime.Now,
                        CreatedBy  = userId,
                        ScheduleId = scheduleIdAfterSaveChanges,
                        EventId    = futureEvent.Id
                    });
                }
            }

            await bll.SaveChangesAsync();
        }
Esempio n. 7
0
        public async Task <IActionResult> Create(SubjectInScheduleCreateEditViewModel vm)
        {
            if (vm.ScheduleId == null)
            {
                return(NotFound());
            }
            vm.SelectList = new SelectList(MapSubjectTypes.GetSubjectTypes(), vm.SelectedSubjectType);
            vm.SubjectInSchedule.SubjectType      = (int)MapSubjectTypes.GetResultSubjectTypeByString(vm.SelectedSubjectType);
            vm.SubjectInSchedule.UniqueIdentifier = $"{vm.Subject.SubjectCode}-{vm.SubjectInSchedule.StartDateTime:yyyyMMddHHmmss}-{vm.SubjectInSchedule.EndDateTime:yyyyMMddHHmmss}";
            vm.SubjectInSchedule.ScheduleId       = (int)vm.ScheduleId;
            vm.SubjectInSchedule.Schedule         = await _bll.Schedules.FindAsync(vm.ScheduleId); // used to pass through model state check

            if (vm.Teachers == null)
            {
                vm.Teachers = new List <Teacher>();
            }
            ModelState.Clear();
            if (!string.IsNullOrWhiteSpace(vm.SubjectInSchedule.Groups) && !string.IsNullOrWhiteSpace(vm.SubjectInSchedule.Rooms) &&
                TryValidateModel(vm.Subject) &&
                TryValidateModel(vm.SubjectInSchedule.Groups) &&
                TryValidateModel(vm.SubjectInSchedule.Rooms))
            {
                if (vm.SubjectInSchedule.StartDateTime == DateTime.MinValue ||
                    vm.SubjectInSchedule.EndDateTime == DateTime.MinValue)
                {
                    ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesChooseError);
                    vm.SubjectInSchedule.StartDateTime = vm.SubjectInSchedule.EndDateTime = DateTime.Today;
                    return(View(vm));
                }

                if (vm.SubjectInSchedule.StartDateTime >= vm.SubjectInSchedule.EndDateTime)
                {
                    ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesDifferenceError);
                    return(View(vm));
                }

                // Add subject
                vm.Subject.CreatedAt = DateTime.Now;
                vm.Subject.CreatedBy = _userManager.GetUserId(User);
                var subjectFromDb = await AddSubjectIfDontExistAsync(vm.Subject);

                vm.SubjectInSchedule.SubjectId = subjectFromDb.Id;
                vm.SubjectInSchedule.Subject   = subjectFromDb;
            }
            ModelState.Clear();
            TryValidateModel(vm);
            if (ModelState.IsValid)
            {
                // set schedule to null so there will be no tracking error!
                vm.SubjectInSchedule.Schedule  = null;
                vm.SubjectInSchedule.Subject   = null;
                vm.SubjectInSchedule.CreatedAt = DateTime.Now;
                vm.SubjectInSchedule.CreatedBy = _userManager.GetUserId(User);

                // Add SubjectInSchedule
                var subjectInScheduleGuid = await _bll.SubjectInSchedules.AddAsync(vm.SubjectInSchedule);

                await _bll.SaveChangesAsync();

                // If teacher list is not empty - add teachers to subject
                if (vm.Teachers != null && vm.Teachers.Count > 0)
                {
                    var subjectInScheduleFromDb =
                        _bll.SubjectInSchedules.GetUpdatesAfterUowSaveChanges(subjectInScheduleGuid);
                    foreach (var teacher in vm.Teachers)
                    {
                        var teacherFromDb = await AddTeacherIfDontExistAsync(teacher);

                        await _bll.TeacherInSubjectEvents.AddAsync(new TeacherInSubjectEvent
                        {
                            CreatedAt           = DateTime.Now,
                            CreatedBy           = _userManager.GetUserId(User),
                            SubjectInScheduleId = subjectInScheduleFromDb.Id,
                            TeacherId           = teacherFromDb.Id
                        });
                    }

                    await _bll.SaveChangesAsync();
                }

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

            if (vm.SubjectInSchedule.StartDateTime == DateTime.MinValue ||
                vm.SubjectInSchedule.EndDateTime == DateTime.MinValue)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesChooseError);
                vm.SubjectInSchedule.StartDateTime = vm.SubjectInSchedule.EndDateTime = DateTime.Today;
                return(View(vm));
            }

            if (vm.SubjectInSchedule.StartDateTime >= vm.SubjectInSchedule.EndDateTime)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesDifferenceError);
                return(View(vm));
            }

            return(View(vm));
        }