/// <summary> /// This is called upon the register button being pressed. /// </summary> /// <param name="sender">The object that caused this event.</param> /// <param name="e">The event arguments.</param> protected async void BtnRegister_Click(object sender, EventArgs e) { // This event is only ever called after the client-side JavaScript returns true. So that means that the information is, up to this point, valid. // First, let's see if the user passed the Google reCAPTCHA test. if (CaptchaPass()) { // If they passed, we get to try and register them for an event! try { // All of this is already on the EventRegistrationPage of the mobile application, but I'll explain it here for fun. RestClient client = new RestClient(); // Object that calls the API. Models.User user = await client.GetUser(username); // Getting the user from the username field. // Making a new event registration model. Models.EventRegistration eventRegistration = new Models.EventRegistration() { EventId = (int)(eventView.DataSource as DataTable).Rows[eventView.PageIndex]["eventId"], Guests = guests, UserId = user.UserId }; // Getting the event from the current page. Models.Event @event = (eventView.DataSource as DataTable).Rows[eventView.PageIndex]["event"] as Models.Event; // Re-check to ensure that we can sign-up for this event still. if (@event.CurrentAttendees >= @event.MaxAttendees) { throw new InvalidOperationException("Maximum reached!"); } // Actually create the registration record in the database. await client.CreateRegistration(eventRegistration); // If they are volunteering they do not count against the event's maximum attendees. Otherwise, they will. if (!(eventView.FindControl("chkVolunteering") as CheckBox).Checked) { @event.CurrentAttendees += eventRegistration.Guests + 1; // Add the user and their guests to the event. await client.UpdateEvent(@event.EventId, @event); // Update the event record in the database. } // Show the user an alert signifying that they succeeded in the registration. eventView.Page.ClientScript.RegisterStartupScript(GetType(), "Success", "alert('Registration successful!');", true); // Re-call the API to populate the event view here. eventsSingleton.Restart(); eventsSingleton = EventsSingleton.Instance; // Re-bind the event data. LoadData(); } catch (Refit.ApiException) // No username found. { eventView.Page.ClientScript.RegisterStartupScript(GetType(), "NoUsernameFound", "alert('Error - No user exists with that username. Try again or download the Pine Grove App to become a member!');", true); } catch (InvalidOperationException) // The event was already filled. { eventView.Page.ClientScript.RegisterStartupScript(GetType(), "EventFull", "alert('Error - The event became full prior to form submission!');", true); } } // The user failed the reCAPTCHA. else { eventView.Page.ClientScript.RegisterStartupScript(GetType(), "Failure", "alert('Error - Failed reCAPTCHA Response.');", true); } }
/// <summary> /// This is called every time the page is loaded, including post-backs. /// </summary> /// <param name="sender">The object that caused this event.</param> /// <param name="e">The event arguments.</param> protected void Page_Load(object sender, EventArgs e) { eventsSingleton = EventsSingleton.Instance; // If there is no instance, one is created. If there is one, it sets it to the one that has already been created. LoadData(); // This get's the data from the events singleton. }