public async Task<JsonResult> Post(FurnitureModel model) { //validate data if (!ModelState.IsValid) return Json(false); model.Ip = Utilities.GetClientIp(); model.DateTime = DateTime.Now; model.City = MaxMindSingleton.Instance.FindCity(model.Ip); return Json(true); //// google //var client = new DatabaseClient(ConfigurationManager.AppSettings["Gmail"], ConfigurationManager.AppSettings["Password"]); //var dbName = ConfigurationManager.AppSettings["SpreadsheetsMilanHome"]; //var tableName = ConfigurationManager.AppSettings["SheetMilanHome"]; //try //{ // var db = client.GetDatabase(dbName) ?? client.CreateDatabase(dbName); // var table = db.GetTable<FurnitureModel>(tableName) ?? db.CreateTable<FurnitureModel>(tableName); // var row = table.Add(model); // var tcs = new TaskCompletionSource<bool>(); // ThreadPool.QueueUserWorkItem(_ => // { // try // { // row.Update(); // tcs.SetResult(true); // } // catch (Exception exc) { tcs.SetException(exc); } // }); // await tcs.Task; // return Json(true); //} //catch //{ // return Json(false); //} }
public async Task<JsonResult> PostBK(FurnitureModel model) { //validate data if (!ModelState.IsValid) return Json(false); model.Ip = Utilities.GetClientIp(); model.DateTime = DateTime.Now; model.City = MaxMindSingleton.Instance.FindCity(model.Ip); //log it var log = log4net.LogManager.GetLogger("mbnd"); log.Info(JsonConvert.SerializeObject(model, Formatting.Indented)); #region save csv file //var path = Server.MapPath("~/reports/milan-home.csv"); var path = ConfigurationManager.AppSettings["PathReports"] + "milan-home.csv"; var lines = new List<String>(); if (!System.IO.File.Exists(path)) { System.IO.File.Create(path).Close(); } if (System.IO.File.Exists(path)) { try { using (var reader = new StreamReader(path)) { String line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } line = model.Name + "\t" + model.Tel + "\t" + model.Email + "\t" + model.ObjectId + "\t" + model.ObjectTitle + "\t" + model.DateTime + "\t" + model.Ip + "\t" + model.City; lines.Add(line); } } catch (Exception ex) { } using (var writer = new StreamWriter(path, false)) { foreach (var line in lines) { writer.WriteLine(line); } writer.Close(); } } #endregion // sent email to Test #region Send Email try { var mMess = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["Gmail"]) }; mMess.To.Add(new MailAddress(ConfigurationManager.AppSettings["EmailTest"])); mMess.Subject = "Furniture " + model.Name; mMess.IsBodyHtml = true; mMess.Body = ("New request on " + model.DateTime + " , Name: " + model.Name); var smClt = new SmtpClient { EnableSsl = true }; smClt.Send(mMess); } catch { } #endregion return Json(true); // google //var client = new DatabaseClient(ConfigurationManager.AppSettings["Gmail"], ConfigurationManager.AppSettings["Password"]); //var dbName = ConfigurationManager.AppSettings["SpreadsheetsMilanHome"]; //var tableName = ConfigurationManager.AppSettings["SheetMilanHome"]; //try //{ // var db = client.GetDatabase(dbName) ?? client.CreateDatabase(dbName); // var table = db.GetTable<FurnitureModel>(tableName) ?? db.CreateTable<FurnitureModel>(tableName); // var row = table.Add(model); // var tcs = new TaskCompletionSource<bool>(); // ThreadPool.QueueUserWorkItem(_ => // { // try // { // row.Update(); // tcs.SetResult(true); // } // catch (Exception exc) { tcs.SetException(exc); } // }); // await tcs.Task; // return Json(true); //} //catch //{ // return Json(false); //} }