<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using DevSpace.Common;
using Newtonsoft.Json;

namespace DevSpace.Api.Controllers {
	public class JsonStudentCodeBinder : IModelBinder {
		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;
		}
	}

	public class TicketController : ApiController {
		private class AccessCode {
			public string code { get; set; }
			public string[] ticket_ids { get; set; }
			public int quantity_available { get; set; }
		}

		private class EventBriteApiObject {
			public AccessCode access_code { get; set; }
		}

		private IDataStore<IStudentCode> _DataStore;
		public TicketController( IDataStore<IStudentCode> DataStore ) {
			this._DataStore = DataStore;
		}

		private class MutableStudentCode : IStudentCode {
			public string Code { get; set; }
			public string Email { get; set; }
			public int Id { get; set; }
		};

		[AllowAnonymous]
		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( "EmailAddress", 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 );<commit_msg>Had a column name wrong. Causing a 500 error<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using DevSpace.Common;
using Newtonsoft.Json;

namespace DevSpace.Api.Controllers {
	public class JsonStudentCodeBinder : IModelBinder {
		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;
		}
	}

	public class TicketController : ApiController {
		private class AccessCode {
			public string code { get; set; }
			public string[] ticket_ids { get; set; }
			public int quantity_available { get; set; }
		}

		private class EventBriteApiObject {
			public AccessCode access_code { get; set; }
		}

		private IDataStore<IStudentCode> _DataStore;
		public TicketController( IDataStore<IStudentCode> DataStore ) {
			this._DataStore = DataStore;
		}

		private class MutableStudentCode : IStudentCode {
			public string Code { get; set; }
			public string Email { get; set; }
			public int Id { get; set; }
		};

		[AllowAnonymous]
		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 );
{'fraction_non_alphanumeric': 0.1, 'fraction_numerical': 0.003482587064676617, 'mean_word_length': 4.605018587360595, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6192682', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1715, 'n_words': 442}