Beispiel #1
0
        public async Task <IActionResult> OnPostAsync(CancellationToken cancellationToken)
        {
            try
            {
                // Model must always be set up
                await SetupModel(TournamentDate, Registration.RegistrationId);
            }
            catch (Exception e)
            {
                _logger.LogCritical("Error setting up the model for date {new DateOnly(TournamentDate)}", e);
            }

            // Handle data annotation model binding errors
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            ModelState.Clear();

            await TryUpdateModelAsync(new { Registration });

            if (Captcha != HttpContext.Session.GetString(CaptchaSvgGenerator.CaptchaSessionKeyName) &&
                !string.IsNullOrEmpty(Captcha))
            {
                ModelState.AddModelError(nameof(Captcha), "Ergebnis der Rechenaufgabe war nicht korrekt");
            }
            if (AllRegistrations.Any(ar =>
                                     ar.TeamName != null &&
                                     ar.Id != Registration.Id &&
                                     ar.TeamName.Replace(" ", string.Empty).Equals(
                                         Registration.TeamName?.Replace(" ", string.Empty),
                                         StringComparison.CurrentCultureIgnoreCase)))
            {
                ModelState.AddModelError(nameof(Registration) + "." + nameof(Registration.TeamName),
                                         "Teamname ist bereits vergeben");
            }
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            try
            {
                var isNewRegistration = Registration.RegistrationId == new Guid() && Registration.Id == 0;
                var now = DateTime.Now;
                if (isNewRegistration)
                {
                    // New record
                    Registration.RegistrationId        = Guid.NewGuid();
                    Registration.CreatedOn             = Registration.ModifiedOn = Registration.RegisteredOn = now;
                    Registration.TournamentDate        = new DateTime(TournamentDate);
                    Registration.IsStandByRegistration = MaxRegistrationsReached;
                    await _clubDbContext.TournamentRegistration !.AddAsync(Registration, cancellationToken);
                }
                else
                {
                    Registration.ModifiedOn = now;
                }

                await _clubDbContext.SaveChangesAsync(cancellationToken);

                await _clubDbContext.DisposeAsync();

                if (isNewRegistration)
                {
                    await _mailService.SendTournamentRegistrationEmailAsync(Registration.GetCompleteName(),
                                                                            Registration.Email !,
                                                                            $"Anmeldung zum Turnier am {TournamentPage.TournamentDefinition.DateFrom.Value!.Value.ToShortDateString()}",
                                                                            GetConfirmationMessage(), cancellationToken);
                }

                // Todo: Add notification for successful saving on TournamentPage
            }
            catch (Exception e)
            {
                _logger.LogCritical("Error saving the tournament registration", e);
                // Todo: Add notification for failure of saving on TournamentPage
            }

            // Redirect to the TournamentPage
            return(Redirect(TournamentPage?.Permalink ?? "/"));
        }