Beispiel #1
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            HttpContent  content = actionContext.Request.Content;
            string       json    = content.ReadAsStringAsync().Result;
            IStudentCode obj     = JsonConvert.DeserializeObject <StudentCode>(json);

            bindingContext.Model = obj;
            return(true);
        }
Beispiel #2
0
        private void SendEmail(IStudentCode studentCode)
        {
            Email Mail = new Email(studentCode.Email);

            Mail.Subject = "Student Ticket Code";
            Mail.Body    = string.Format(
                @"This email is a response to a request for a student discount code. We have validated your email address and are pleased to offer you this code.

This code is tied to your email address is a valid for one use. If you misplace this email, you may supply your email to the DevSpace website on the tickets page to receive another copy of this email. A new code will not be generated.

You may go directly to out ticketing page using the link below.
 
https://www.eventbrite.com/e/devspace-2016-registration-24347789895?access={0}

If you wish, you may also go directly to EventBrite, find out event, and manually enter the code:

{0}

We thank you for your interest in the DevSpace Technical Conference and look forward to seeing you there.", studentCode.Code);

            Mail.Send();
        }
Beispiel #3
0
        public async Task <HttpResponseMessage> Post([ModelBinder(typeof(JsonStudentCodeBinder))] IStudentCode value)
        {
            MutableStudentCode NewStudentCode = new MutableStudentCode {
                Email = value.Email
            };

            if (null == NewStudentCode)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            // Check for .edu email
            if (string.IsNullOrWhiteSpace(NewStudentCode.Email))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            if (!NewStudentCode.Email.Trim().EndsWith(".edu", StringComparison.InvariantCultureIgnoreCase))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            // Check DataStore for existing code
            IList <IStudentCode> ExistingCodes = await _DataStore.Get("Email", NewStudentCode.Email);

            //	If exists, resent existing code
            if (ExistingCodes.Count > 0)
            {
                SendEmail(ExistingCodes[0]);
                return(new HttpResponseMessage(HttpStatusCode.NoContent));
            }

            // Generate Code
            NewStudentCode.Code = BitConverter.ToString(Guid.NewGuid().ToByteArray()).Replace("-", "").Substring(0, 16);
            while (1 == (await _DataStore.Get("Code", NewStudentCode.Code)).Count)
            {
                NewStudentCode.Code = BitConverter.ToString(Guid.NewGuid().ToByteArray()).Replace("-", "").Substring(0, 16);
            }

            // Call EventBrite to create code
#if DEBUG
            string EventBriteApiKey   = Environment.GetEnvironmentVariable("EVENTBRITEAPIKEY", EnvironmentVariableTarget.Machine);
            string EventBriteEventId  = Environment.GetEnvironmentVariable("EVENTBRITEEVENTID", EnvironmentVariableTarget.Machine);
            string EventBriteTicketId = Environment.GetEnvironmentVariable("EVENTBRITETICKETID", EnvironmentVariableTarget.Machine);
#else
            string EventBriteApiKey   = ConfigurationManager.AppSettings["EventBriteApiKey"];
            string EventBriteEventId  = ConfigurationManager.AppSettings["EventBriteEventId"];
            string EventBriteTicketId = ConfigurationManager.AppSettings["EventBriteTicketId"];
#endif
            EventBriteApiObject JsonObject = new EventBriteApiObject {
                access_code = new AccessCode {
                    code               = NewStudentCode.Code,
                    ticket_ids         = new string[] { EventBriteTicketId },
                    quantity_available = 1
                }
            };

            string Uri = string.Format("https://www.eventbriteapi.com/v3/events/{0}/access_codes/", EventBriteEventId);

            HttpWebRequest EventBriteRequest = HttpWebRequest.Create(Uri) as HttpWebRequest;
            EventBriteRequest.Headers.Add("Authorization", string.Format("Bearer {0}", EventBriteApiKey));
            EventBriteRequest.ContentType = "application/json";
            EventBriteRequest.Accept      = "application/json";
            EventBriteRequest.Method      = "POST";

            byte[] RequestBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(JsonObject, Formatting.None));
            EventBriteRequest.ContentLength = RequestBytes.Length;
            using (Stream RequestStream = EventBriteRequest.GetRequestStream())
                RequestStream.Write(RequestBytes, 0, RequestBytes.Length);

            try {
                using (HttpWebResponse EventBriteResponse = await EventBriteRequest.GetResponseAsync() as HttpWebResponse);
            } catch (WebException) {
                return(new HttpResponseMessage(HttpStatusCode.BadGateway));
            } catch (Exception) {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }

            // Store Code in DataStore
            NewStudentCode.Id = (await _DataStore.Add(NewStudentCode)).Id;

            // Email Code
            SendEmail(NewStudentCode);

            return(new HttpResponseMessage(HttpStatusCode.Created));
        }
Beispiel #4
0
        public async Task <HttpResponseMessage> Post([ModelBinder(typeof(JsonStudentCodeBinder))] IStudentCode value)
        {
            MutableStudentCode NewStudentCode = new MutableStudentCode {
                Email = value.Email
            };

            if (null == NewStudentCode)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            // Check for .edu email
            if (string.IsNullOrWhiteSpace(NewStudentCode.Email))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            if (!NewStudentCode.Email.Trim().EndsWith(".edu", StringComparison.InvariantCultureIgnoreCase))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            // Check DataStore for existing code
            IList <IStudentCode> ExistingCodes = await _DataStore.Get("Email", NewStudentCode.Email);

            //	If exists, resent existing code
            if (ExistingCodes.Count > 0)
            {
                SendEmail(ExistingCodes[0]);
                return(new HttpResponseMessage(HttpStatusCode.NoContent));
            }

            // Generate Code
            NewStudentCode.Code = BitConverter.ToString(Guid.NewGuid().ToByteArray()).Replace("-", "").Substring(0, 16);
            while (1 == (await _DataStore.Get("Code", NewStudentCode.Code)).Count)
            {
                NewStudentCode.Code = BitConverter.ToString(Guid.NewGuid().ToByteArray()).Replace("-", "").Substring(0, 16);
            }

            Newtonsoft.Json.Linq.JObject input = new Newtonsoft.Json.Linq.JObject {
                ["discount"] = new Newtonsoft.Json.Linq.JObject {
                    ["type"]               = "access",
                    ["code"]               = NewStudentCode.Code,
                    ["event_id"]           = EventBriteEventId,
                    ["ticket_class_ids"]   = new Newtonsoft.Json.Linq.JArray(new[] { EventBriteTicketId }),
                    ["quantity_available"] = 1
                }
            };

            try {
                HttpResponseMessage EventBriteResponse = await EventBriteApi.PostAsync(
                    string.Empty,
                    new StringContent(
                        input.ToString(),
                        Encoding.UTF8,
                        "application/json"
                        )
                    );

                EventBriteResponse.Dispose();
            } catch (WebException) {
                return(new HttpResponseMessage(HttpStatusCode.BadGateway));
            } catch (Exception) {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }

            // Store Code in DataStore
            NewStudentCode.Id = (await _DataStore.Add(NewStudentCode)).Id;

            // Email Code
            SendEmail(NewStudentCode);

            return(new HttpResponseMessage(HttpStatusCode.Created));
        }