public static object SaveAddNewProperty(RequestModel request) { var connection = new MySqlConnection(ConfigurationManager.AppSettings["MySqlDBConn"].ToString()); var compiler = new MySqlCompiler(); var db = new QueryFactory(connection, compiler); SuccessResponse successResponseModel = new SuccessResponse(); db.Connection.Open(); using (var scope = db.Connection.BeginTransaction()) { try { var test = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(Convert.ToString(request.RequestData)); object PropertyType; test.TryGetValue("PropertyType", out PropertyType); string _PropertyType = PropertyType.ToString(); object PropertyPics; test.TryGetValue("PropertyPics", out PropertyPics); List <string> _PropertyPics = PropertyPics as List <string>; test.Remove("PropertyPics"); if (_PropertyPics != null && _PropertyPics.Count > 0) { //convert and add key } object PropertyVideos; test.TryGetValue("PropertyVideos", out PropertyVideos); List <string> _PropertyVideos = PropertyVideos as List <string>; test.Remove("PropertyVideos"); if (_PropertyVideos != null && _PropertyVideos.Count > 0) { //convert and add key } object Documents; test.TryGetValue("Documents", out Documents); List <string> _Documents = Documents as List <string>; test.Remove("Documents"); if (_Documents != null && _Documents.Count > 0) { //convert and add key } object PropertyShare; test.TryGetValue("PropertyShare", out PropertyShare); test.Remove("PropertyShare"); object PropertyPrediction; test.TryGetValue("PropertyPrediction", out PropertyPrediction); test.Remove("PropertyPrediction"); object Developmental; test.TryGetValue("Developmental", out Developmental); test.Remove("Developmental"); object DevelopmentalPrediction; test.TryGetValue("DevelopmentalPrediction", out DevelopmentalPrediction); test.Remove("DevelopmentalPrediction"); object RentalProperty; test.TryGetValue("RentalProperty", out RentalProperty); test.Remove("RentalProperty"); // You can register the QueryFactory in the IoC container var query = db.Query("propertydetail").AsInsert(test); SqlKata.SqlResult compiledQuery = compiler.Compile(query); //Inject the Identity in the Compiled Query SQL object var sql = compiledQuery.Sql + "; SELECT @@IDENTITY as ID;"; //Name Binding house the values that the insert query needs var IdentityKey = db.Select <string>(sql, compiledQuery.NamedBindings).FirstOrDefault(); Dictionary <string, object> _PropertyShare = JsonConvert.DeserializeObject <Dictionary <string, object> >(PropertyShare.ToString()); if (_PropertyShare != null) { _PropertyShare.Add("PropertyId", IdentityKey); var resPropertyShare = db.Query("PropertyShare").Insert(_PropertyShare); } List <Dictionary <string, object> > _PropertyPrediction = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(PropertyPrediction.ToString()); if (_PropertyPrediction != null) { foreach (var pp in _PropertyPrediction) { pp.Add("PropertyId", IdentityKey); var resPropertyShare = db.Query("PropertyPrediction").Insert(pp); } } if (_PropertyType == "D") { Dictionary <string, object> _Developmental = JsonConvert.DeserializeObject <Dictionary <string, object> >(Developmental.ToString()); if (_Developmental != null) { _Developmental.Add("PropertyId", IdentityKey); var resDevelopmental = db.Query("Developmental").Insert(_Developmental); } List <Dictionary <string, object> > _DevelopmentalPrediction = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(DevelopmentalPrediction.ToString()); if (_DevelopmentalPrediction != null) { foreach (var dp in _DevelopmentalPrediction) { dp.Add("PropertyId", IdentityKey); var resDevelopmentalPrediction = db.Query("DevelopmentalPrediction").Insert(dp); } } } if (_PropertyType == "R") { Dictionary <string, object> _RentalProperty = JsonConvert.DeserializeObject <Dictionary <string, object> >(RentalProperty.ToString()); if (_RentalProperty != null) { object RentalContract; _RentalProperty.TryGetValue("RentalContract", out RentalContract); List <string> _RentalContract = RentalContract as List <string>; _RentalProperty.Remove("RentalContract"); if (_RentalContract != null && _RentalContract.Count > 0) { //convert and add key } _RentalProperty.Add("PropertyId", IdentityKey); var resRentalProperty = db.Query("RentalProperty").Insert(_RentalProperty); } } bool hasData = true; scope.Commit(); successResponseModel = new SuccessResponse("", hasData, "Record Saved"); } catch (Exception ex) { //Logger.WriteErrorLog(ex); scope.Rollback(); return(new ErrorResponse(ex.Message, HttpStatusCode.BadRequest)); } } return(successResponseModel); }
public static object AddNewRegUser(RequestModel request) { // Setup the connection and compiler var connection = new MySqlConnection(ConfigurationManager.AppSettings["MySqlDBConn"].ToString()); var compiler = new MySqlCompiler(); var db = new QueryFactory(connection, compiler); SuccessResponse successResponseModel = new SuccessResponse(); db.Connection.Open(); using (var scope = db.Connection.BeginTransaction()) { try { var test = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(Convert.ToString(request.RequestData)); object User; test.TryGetValue("User", out User); test.Remove("User"); object Document_SOE; test.TryGetValue("Document_SOE", out Document_SOE); List <string> _Document_SOE = Document_SOE as List <string>; test.Remove("Document_SOE"); if (_Document_SOE != null && _Document_SOE.Count > 0) { //convert and add key } if (User != null) { Dictionary <string, object> _User = JsonConvert.DeserializeObject <Dictionary <string, object> >(User.ToString()); //check if email exists object Email; _User.TryGetValue("Email", out Email); string _Email = Email.ToString(); var response = db.Query("User").Where("Email", _Email).Get(); if (response != null && response.Count() > 0) { //return error return(new SuccessResponse(null, HttpStatusCode.Conflict, "Email already exists")); } object Password; _User.TryGetValue("Password", out Password); string _Password = Password.ToString(); string _PasswordUnhashed = Password.ToString(); _User.Remove("Password"); if (!string.IsNullOrEmpty(_Password)) { //convert and add key _Password = DBManagerUtility._encodeJWT(new Dictionary <string, string>() { { "Password", _Password } }, AppConstants.AppSecretKeyPassword); _User.Add("Password", _Password); } _User.Add("RegistrationConfirmation", "Y"); _User.Add("isVerified", "N"); var query = db.Query("User").AsInsert(_User); SqlKata.SqlResult compiledQuery = compiler.Compile(query); //Inject the Identity in the Compiled Query SQL object var sql = compiledQuery.Sql + "; SELECT @@IDENTITY as ID;"; //Name Binding house the values that the insert query needs var IdentityKey = db.Select <string>(sql, compiledQuery.NamedBindings).FirstOrDefault(); test.Add("UserId", IdentityKey); var resRegUser = db.Query("RegisteredUser").Insert(test); scope.Commit(); //testing Dictionary <string, string> responseData = new Dictionary <string, string>(); #region issue AuthToken var pairs = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", _Email), new KeyValuePair <string, string> ("Password", _PasswordUnhashed), new KeyValuePair <string, string> ("scope", "USER") }; var content = new FormUrlEncodedContent(pairs); ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; using (var client = new HttpClient()) { var responseToken = client.PostAsync(Constants.BaseUrl + "token", content).Result; var responseContent = responseToken.Content.ReadAsStringAsync().Result; responseData = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseContent); } #endregion bool hasData = true; successResponseModel = new SuccessResponse(responseData, hasData); } else { scope.Rollback(); } } catch (Exception ex) { //Logger.WriteErrorLog(ex); scope.Rollback(); return(new ErrorResponse(ex.Message, HttpStatusCode.BadRequest)); } } return(successResponseModel); }