public static string Encrypt(string input, bool getFromConfig = false) { if (getFromConfig && TfSettings.Encryption.Active == false) { return(input); } StringCipher stringCipher = new StringCipher { PasswordHash = TfSettings.Encryption.PasswordHash, SaltKey = TfSettings.Encryption.SaltKey, VIKey = TfSettings.Encryption.VIKey }; try { return(stringCipher.Encrypt(input)); } catch { TfDebug.WriteLog( TfSettings.Logs.System, $"Ignored Invalid Ecryption - {DateTime.Now}", $"Value: {input}{Environment.NewLine}"); return(null); } }
public void WriteSql(string sql, Dictionary <string, object> parameters) { if (sql.IsEmpty()) { throw new TfException("SQL not provided."); } if (this.SqlCommand == null) { this.SqlCommand = this.SqlConnection.CreateCommand(); } TfDebug.WriteLine("Executing query", sql); this.SqlCommand.CommandText = sql; if (parameters != null) { foreach (var parameter in parameters) { this.SqlCommand.Parameters.Add(ConnectProvider.SqlParameter(parameter.Key, parameter.Value)); } TfDebug.WriteLine("SQL Parameters", string.Join(Environment.NewLine, parameters)); } }
public void Request() { var dataString = string.Empty; var apiUrl = $"{this.URL}/{this.Actions}"; HttpWebRequest request = null; if (this.HttpMethod == HttpMethod.GET) { dataString = JsonTools.DictionaryToQuery(this.Parameters); request = (HttpWebRequest)WebRequest.Create($"{apiUrl}?{dataString}"); request.Method = this.HttpMethod.GetString(); } else { dataString = JsonTools.DictionaryToJson(this.Parameters); request = (HttpWebRequest)WebRequest.Create(apiUrl); request.Method = this.HttpMethod.GetString(); request.ContentType = "application/json"; request.ContentLength = dataString.Length; using (Stream webStream = request.GetRequestStream()) { using (StreamWriter requestWriter = new StreamWriter(webStream, Encoding.ASCII)) { requestWriter.Write(dataString); } } } if (request == null) { return; } try { WebResponse webResponse = request.GetResponse(); using (Stream webStream = webResponse.GetResponseStream()) { if (webStream != null) { using (StreamReader responseReader = new StreamReader(webStream)) { this.Response = responseReader.ReadToEnd().JsonGetDictionary(); } } } } catch (Exception ex) { TfDebug.WriteLog( ex, $"URL: {apiUrl}", $"Method: {this.HttpMethod.GetString()}", $"Paremeters: {dataString}"); this.Response = new Dictionary <string, string>() { { "Error", ex.Message } }; } }
public void Initiate <Model>(bool authorize = true) where Model : TfModel, new() { try { var obj = Activator.CreateInstance(typeof(Model)); if (!this.Authorize(authorize)) { this.Response.StatusCode = (int)HttpStatusCode.Unauthorized; return; } this.GetBody(obj); this.GetQueries(obj); this.ModelObject = this.ModelDictionary.ToClass <Model>(); this.GetNecessities(); this.ModelObject.BeforeStartUp(); this.ValidateModel(); this.ModelObject.OnStartUp(); } catch (Exception ex) when(!TfSettings.System.Debug) { this.Response.StatusCode = (int)HttpStatusCode.InternalServerError; TfDebug.WriteLog(ex); } }
public JsonResult Conclude() { try { if (this.ModelObject != null && this.ModelObject.Stop == false && this.ModelState.IsValid) { this.ExecuteMapping(); this.ExecuteHandling(); this.BuildModelDictionary(); this.Response.StatusCode = (int)HttpStatusCode.OK; } else { this.Response.StatusCode = (int)HttpStatusCode.BadRequest; } } catch (Exception ex) when(!TfSettings.System.Debug) { this.Response.StatusCode = (int)HttpStatusCode.InternalServerError; TfDebug.WriteLog(ex); } return(this.JsonResult); }
protected JsonResult Conclude() { if (!this.Initiated) { return(this.JsonModel); } try { if (this.Model != null && this.Model.Stop == false && this.ModelState.IsValid) { if (this.Model.Mapping) { this.Model.MapModel(); } if (this.Model.Handling) { this.Model.HandleModel(); } this.BuildModelDictionary(); this.Response.StatusCode = (int)HttpStatusCode.OK; } else { this.Response.StatusCode = (int)HttpStatusCode.BadRequest; } } catch (Exception ex) when(!TfSettings.System.Debug) { this.Response.StatusCode = (int)HttpStatusCode.InternalServerError; TfDebug.WriteLog(ex); } return(this.JsonModel); }
private static dynamic GetObject(this Dictionary <string, object> dictionary, Type type) { dynamic model = Activator.CreateInstance(type); foreach (var keyValue in dictionary) { var property = type.GetProperty(keyValue.Key); if (property == null) { continue; } object value = keyValue.Value; if (value.IsDictionary()) { value = GetObject((Dictionary <string, object>)value, property.PropertyType); } else if (value.IsList()) { dynamic dynamicList = Activator.CreateInstance(property.PropertyType); var itemType = property.PropertyType.GetGenericArguments()[0]; if (value is List <Dictionary <string, object> > ) { foreach (var item in value as List <Dictionary <string, object> > ) { dynamicList.Add(GetObject(item, itemType)); } } else if (value is List <dynamic> ) { foreach (var item in value as List <dynamic> ) { dynamicList.Add(item); } } value = dynamicList; } try { property.SetValue(model, TfConvert.ChangeType(value, property.PropertyType), null); } catch { TfDebug.WriteLog( TfSettings.Logs.System, $"Ignored Malformed Line - {DateTime.Now}", $"Name: {keyValue.Key}{Environment.NewLine}" + $"Value: {keyValue.Value}{Environment.NewLine}" + $"Type: {property.PropertyType}"); } } return(model); }
public static NpgsqlConnection SqlConnection(string connection = null) { if (connection.IsEmpty()) { connection = TfSettings.Database.ConnectionString; } TfDebug.WriteLine("Connecting database", connection); return(new NpgsqlConnection(connection)); }
public static void Send(string mailTo, string emailFile, Dictionary <string, object> items) { try { var emailContent = ReadEmailFromFile(emailFile, items); SendBase(mailTo, emailContent.Title, emailContent.Message); } catch (Exception ex) when(!TfSettings.System.Debug) { TfDebug.WriteLog(ex); } }
public static bool IsValidJson(string jsonString) { try { JToken.Parse(jsonString); return(true); } catch (JsonReaderException ex) { TfDebug.WriteLine(ex.Message, ex.StackTrace); return(false); } }
public void Commit() { try { this.SqlTransaction.Commit(); } catch (Exception ex) { this.SqlTransaction.Rollback(); TfDebug.WriteLog(ex); } this.SqlConnection.Close(); }
public static dynamic SqlConnection() { string connection = TfSettings.Database.ConnectionString; TfDebug.WriteLine("Connecting database", connection); switch (TfSettings.Database.Provider) { case Provider.MySql: return(new MySqlConnection(connection)); case Provider.Postgres: return(new NpgsqlConnection(connection)); case Provider.SqlServer: return(new SqlConnection(connection)); default: return(new NpgsqlConnection(connection)); } }
public int ExecuteNonQuery(Operations operation) { if (this.SqlTransaction == null) { this.SqlConnection.Open(); } long executionCount = 0; executionCount = this.SqlCommand.ExecuteNonQuery(); if (operation == Operations.INSERT) { this.WriteSql($"{Operations.SELECT} currval('{this.TableName}_id_seq');", null); executionCount = this.SqlCommand.ExecuteScalar(); } if (this.SqlTransaction == null) { this.SqlConnection.Close(); } TfDebug.WriteLine($"{operation.GetString()} count", Convert.ToString(executionCount)); if (new[] { Operations.ADD, Operations.ALTER_TABLE, Operations.CREATE_TABLE, Operations.DROP_COLUMN }.Contains(operation)) { TfDebug.WriteLog( TfSettings.Logs.Migration, $"Migration Details - {DateTime.Now}", this.SqlCommand.CommandText); } return((int)executionCount); }
public TfException(string message) { TfDebug.WriteLine("System Exception:", message); throw new Exception(message); }
private static dynamic GetObject(this Dictionary <string, object> dictionary, Type type) { dynamic model = Activator.CreateInstance(type); foreach (var keyValue in dictionary) { var property = type.GetProperty(keyValue.Key); if (property == null) { continue; } object value = keyValue.Value; if (value.IsDictionary()) { value = GetObject((Dictionary <string, object>)value, property.PropertyType); } else if (value.IsList()) { dynamic dynamicList; if (property.PropertyType.GetConstructor(Type.EmptyTypes) != null) { dynamicList = Activator.CreateInstance(property.PropertyType); var itemType = property.PropertyType.GetGenericArguments()[0]; if (value is List <Dictionary <string, object> > ) { foreach (var item in value as List <Dictionary <string, object> > ) { dynamicList.Add(GetObject(item, itemType)); } } } else { if (property.PropertyType == typeof(String[])) { dynamicList = new List <string>(); } else if (property.PropertyType == typeof(Int32[])) { dynamicList = new List <int>(); } else { dynamicList = new List <dynamic>(); } if (value is List <dynamic> ) { foreach (var item in value as List <dynamic> ) { dynamicList.Add(item); } } } if (typeof(IEnumerable <dynamic>).IsAssignableFrom(property.PropertyType)) { if (property.PropertyType == typeof(String[])) { var valueList = (dynamicList as List <string>); if (valueList.Count == 0) { value = null; } else { value = valueList.ToArray(); } } else if (property.PropertyType == typeof(Int32[])) { var valueList = (dynamicList as List <int>); if (valueList.Count == 0) { value = null; } else { value = valueList.ToArray(); } } else { var valueList = (dynamicList as List <dynamic>); if (valueList.Count == 0) { value = null; } else { value = valueList.ToArray(); } } } else { value = dynamicList; } } else if (value.GetType().IsNullableEnum()) { value = (Enum)value; } try { property.SetValue(model, TfConvert.ChangeType(value, property.PropertyType), null); } catch { TfDebug.WriteLog( TfSettings.Logs.System, $"Ignored Malformed Line - {DateTime.Now}", $"Name: {keyValue.Key}{Environment.NewLine}" + $"Value: {keyValue.Value}{Environment.NewLine}" + $"Type: {property.PropertyType}"); } } return(model); }