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)); }
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)); }