Пример #1
0
        public IActionResult Edit(int id)
        {
            var timeZone = GetTimeZoneOfCurrentUser();
            var meeting  = _meetingRepository.GetMeeting(id);

            if (meeting == null)
            {
                _logger.LogCritical(Constants.UnknownId + " of meeting");
                return(RedirectToAction(nameof(ErrorController.Error), "Error"));
            }
            var vm = new MeetingViewModels.CreateEditMeetingViewModel
            {
                Organizers    = _userManager.Users.ToList(),
                OrganizerId   = meeting.Organizer.Id,
                Title         = meeting.Title,
                MeetingId     = meeting.MeetingId,
                StartDateTime = TimeZoneInfo.ConvertTimeFromUtc(meeting.StartDateTime, timeZone),
                EndDateTime   = TimeZoneInfo.ConvertTimeFromUtc(meeting.EndDateTime, timeZone),
                Notes         = meeting.Notes,
                Games         = _gameRepository.Games.ToList(),
                SelectedGames = GetGameIdsFromMeeting(id),
                IsEditable    = meeting.Organizer.UserName == User.Identity.Name,
                Address       = new AddressViewModels
                {
                    Street     = meeting.Street,
                    City       = meeting.City,
                    PostalCode = meeting.PostalCode,
                    Country    = meeting.Country
                }
            };

            return(View(vm));
        }
        public IActionResult Edit(int id)
        {
            var currentUserId = GetCurrentUserId().Result;
            var meeting       = _meetingRepository.GetMeeting(id);

            if (meeting == null)
            {
                _logger.LogCritical($"{Constants.UnknownId} of meeting");
                return(RedirectToAction(nameof(ErrorController.Error), "Error"));
            }

            var timeZone = _userManager.FindByIdAsync(currentUserId).Result.TimeZone;

            var vm = new MeetingViewModels.CreateEditMeetingViewModel
            {
                Organizer     = meeting.Organizer.FullName,
                Title         = meeting.Title,
                MeetingId     = meeting.MeetingId,
                StartDateTime = ToolsExtensions.ConvertToTimeZoneFromUtc(meeting.StartDateTime, timeZone, _logger)
                                .ToString(Constants.DateTimeFormat, CultureInfo.InvariantCulture),
                EndDateTime = ToolsExtensions.ConvertToTimeZoneFromUtc(meeting.EndDateTime, timeZone, _logger)
                              .ToString(Constants.DateTimeFormat, CultureInfo.InvariantCulture),
                Notes         = meeting.Notes,
                Games         = _meetingRepository.GetGamesForOrganizer(id, meeting.OrganizerId).OrderBy(g => g.Title),
                SelectedGames = GetGameIdsFromMeeting(id),
                IsEditable    = meeting.Organizer.UserName == User.Identity.Name,
                TimeZone      = timeZone,
                Address       = new AddressViewModels
                {
                    Street     = meeting.Street,
                    City       = meeting.City,
                    PostalCode = meeting.PostalCode,
                    Country    = meeting.Country
                }
            };

            return(View(vm));
        }
Пример #3
0
        public async Task <IActionResult> Edit(MeetingViewModels.CreateEditMeetingViewModel vm)
        {
            var timeZone            = GetTimeZoneOfCurrentUser();
            var startDateUTC        = TimeZoneInfo.ConvertTimeToUtc(vm.StartDateTime, timeZone);
            var endDateUTC          = TimeZoneInfo.ConvertTimeToUtc(vm.EndDateTime, timeZone);
            var currentUserId       = GetCurrentUserId().Result;
            var overlappingMeetings = new List <Meeting>();

            overlappingMeetings = vm.MeetingId == 0
                ? _meetingRepository.GetOverlappingMeetingsForUser(startDateUTC, endDateUTC, currentUserId).ToList()
                : _meetingRepository.GetOverlappingMeetingsForMeeting(startDateUTC, endDateUTC, vm.MeetingId).ToList();

            if (!ToolsExtensions.IsDateInFuture(startDateUTC))
            {
                ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.StartDateTime),
                                         Constants.FutureDateInPastMessage);
            }

            if (!ToolsExtensions.IsStartDateBeforeEndDate(startDateUTC, endDateUTC))
            {
                ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.EndDateTime),
                                         Constants.EndDateBeforeStartMessage);
            }

            if (overlappingMeetings.Count > 0)
            {
                var overlappingMeetingsTitle = ": ";
                foreach (var meeting in overlappingMeetings)
                {
                    overlappingMeetingsTitle += meeting.Title + " ";
                }

                ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.OrganizerId),
                                         Constants.OverlappingMeetingsMessage + overlappingMeetingsTitle);
            }

            if (ModelState.IsValid)
            {
                var organizer = await _userManager.FindByIdAsync(currentUserId);

                var meeting = new Meeting
                {
                    MeetingId     = vm.MeetingId,
                    Title         = vm.Title,
                    StartDateTime = startDateUTC,
                    EndDateTime   = endDateUTC,
                    Organizer     = organizer,
                    Street        = vm.Address.Street,
                    PostalCode    = vm.Address.PostalCode,
                    City          = vm.Address.City,
                    Country       = vm.Address.Country,
                    Notes         = vm.Notes
                };
                _meetingRepository.SaveMeeting(meeting);
                var savedGames    = GetGameIdsFromMeeting(meeting.MeetingId);
                var selectedGames = vm.SelectedGames ?? new int[0];
                var gamesToAdd    = selectedGames.Except(savedGames).ToList();
                var gamesToRemove = savedGames.Except(selectedGames).ToList();

                if (gamesToAdd.Count > 0)
                {
                    foreach (var game in gamesToAdd)
                    {
                        var gameToAdd = new MeetingGame {
                            GameId = game, MeetingId = meeting.MeetingId
                        };
                        _meetingRepository.AddGameToMeeting(gameToAdd);
                    }
                }

                if (gamesToRemove.Count > 0)
                {
                    foreach (var game in gamesToRemove)
                    {
                        _meetingRepository.RemoveGameFromMeeting(game, meeting.MeetingId);
                    }
                }

                TempData["SuccessMessage"] = Constants.GeneralSuccessMessage;
                return(RedirectToAction(nameof(Edit), new { id = meeting.MeetingId }));
            }

            vm.Organizers = _userManager.Users.ToList();
            vm.Games      = _gameRepository.Games.ToList();
            return(View(vm));
        }
        public async Task <IActionResult> Edit(MeetingViewModels.CreateEditMeetingViewModel vm)
        {
            var startDateUtc  = new DateTime();
            var endDateUtc    = new DateTime();
            var currentUserId = GetCurrentUserId().Result;
            var timeZone      = _userManager
                                .FindByIdAsync(currentUserId).Result.TimeZone;

            if (ModelState.IsValid)
            {
                if (DateTime.TryParse(vm.StartDateTime, out var startDate) &&
                    DateTime.TryParse(vm.EndDateTime, out var endDate))
                {
                    startDateUtc = ToolsExtensions.ConvertFromTimeZoneToUtc(startDate, timeZone, _logger);
                    endDateUtc   = ToolsExtensions.ConvertFromTimeZoneToUtc(endDate, timeZone, _logger);

                    var overlappingMeetings = new List <string>();

                    overlappingMeetings = vm.MeetingId == 0
                        ? _meetingRepository.GetOverlappingMeetingsForUser(startDateUtc, endDateUtc, currentUserId)
                                          .Select(m => m.Title).ToList()
                        : _meetingRepository.GetOverlappingMeetingsForMeeting(startDateUtc, endDateUtc, vm.MeetingId)
                                          .Select(m => m.Title).ToList();

                    if (!ToolsExtensions.IsDateInFuture(startDateUtc))
                    {
                        ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.StartDateTime),
                                                 Constants.FutureDateInPastMessage);
                    }

                    if (!ToolsExtensions.IsStartDateBeforeEndDate(startDateUtc, endDateUtc))
                    {
                        ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.EndDateTime),
                                                 Constants.EndDateBeforeStartMessage);
                    }

                    if (overlappingMeetings.Count > 0)
                    {
                        var overlappingMeetingsTitle = string.Join(", ", overlappingMeetings);

                        ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.Title),
                                                 $"{Constants.OverlappingMeetingsMessage}: {overlappingMeetingsTitle}");
                    }
                }
                else
                {
                    if (!DateTime.TryParse(vm.StartDateTime, out startDate))
                    {
                        ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.StartDateTime),
                                                 Constants.WrongDateTimeFormat);
                    }

                    if (!DateTime.TryParse(vm.EndDateTime, out endDate))
                    {
                        ModelState.AddModelError(nameof(MeetingViewModels.CreateEditMeetingViewModel.EndDateTime),
                                                 Constants.WrongDateTimeFormat);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var organizer = await _userManager.FindByIdAsync(currentUserId);

                var meeting = new Meeting
                {
                    MeetingId     = vm.MeetingId,
                    Title         = vm.Title,
                    StartDateTime = startDateUtc,
                    EndDateTime   = endDateUtc,
                    Organizer     = organizer,
                    Street        = vm.Address.Street,
                    PostalCode    = vm.Address.PostalCode,
                    City          = vm.Address.City,
                    Country       = vm.Address.Country,
                    Notes         = vm.Notes
                };
                var    baseMeeting = _meetingRepository.GetMeeting(meeting.MeetingId);
                string changes     = null;
                if (baseMeeting != null)
                {
                    changes = CompareMeetingData(baseMeeting, meeting, timeZone);
                }

                _meetingRepository.SaveMeeting(meeting);
                var savedGames    = GetGameIdsFromMeeting(meeting.MeetingId);
                var selectedGames = vm.SelectedGames ?? new List <int>();
                var gamesToAdd    = selectedGames.Except(savedGames).ToList();
                var gamesToRemove = savedGames.Except(selectedGames).ToList();

                if (gamesToAdd.Count > 0)
                {
                    foreach (var gameToAdd in gamesToAdd.Select(game => new MeetingGame
                    {
                        GameId = game, MeetingId = meeting.MeetingId
                    }))
                    {
                        _meetingRepository.AddGameToMeeting(gameToAdd);
                    }
                }

                if (gamesToRemove.Count > 0)
                {
                    foreach (var game in gamesToRemove)
                    {
                        _meetingRepository.RemoveGameFromMeeting(game, meeting.MeetingId);
                    }
                }

                TempData["SuccessMessage"] = Constants.GeneralSuccessMessage;
                if (changes == null)
                {
                    return(RedirectToAction(nameof(Edit), new { id = meeting.MeetingId }));
                }
                var appLink = Url.Action(nameof(Edit), "Meeting", new { id = baseMeeting.MeetingId },
                                         HttpContext.Request.Scheme);
                foreach (var email in _invitedUserRepository.GetUsersEmailsForNotification(baseMeeting.MeetingId, currentUserId))
                {
                    _templateSender.SendGeneralEmailAsync(new SendEmailDetails
                    {
                        IsHTML  = true,
                        ToEmail = email,
                        Subject = Constants.SubjectChangeMeetingDataEmail
                    }, Constants.TitleChangeMeetingDataEmail,
                                                          $"{Constants.ContentChangeMeetingDataEmail}: {changes}",
                                                          Constants.ButtonCheckMeeting,
                                                          appLink)
                    .ContinueWith(t =>
                    {
                        if (t.Result.Successful)
                        {
                            return;
                        }
                        foreach (var error in t.Result.Errors)
                        {
                            _logger.LogError(error);
                        }
                    }, TaskScheduler.Default);
                }

                return(RedirectToAction(nameof(Edit), new { id = meeting.MeetingId }));
            }

            vm.Games = _meetingRepository.GetGamesForOrganizer(vm.MeetingId, currentUserId).OrderBy(g => g.Title);
            return(View(vm));
        }