public async Task<JsonResult> PostNewStarLand(CommonModel model) { //validate data if (!ModelState.IsValid) return Json(false); model.Ip = Utilities.GetClientIp(); model.DateTime = DateTime.Now; //var client = new DatabaseClient(ConfigurationManager.AppSettings["Gmail"], ConfigurationManager.AppSettings["Password"]); var dbName = model.Project; //var tableName = "Responses"; //log it var log = log4net.LogManager.GetLogger("mbnd"); log.Info(JsonConvert.SerializeObject(model, Formatting.Indented)); #region save csv file // Create folder var exists = Directory.Exists(ConfigurationManager.AppSettings["PathReports"] + dbName); if (!exists) { Directory.CreateDirectory(ConfigurationManager.AppSettings["PathReports"] + dbName); } var path = ConfigurationManager.AppSettings["PathReports"] + dbName + "/" + dbName + ".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.Project + "\t" + model.Name + "\t" + model.Tel + model.DateTime + "\t" + model.Ip; 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 Customer #region Send Email try { if (!String.IsNullOrEmpty(model.EmailCustomer)) { var mMess = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["Gmail"]) }; mMess.To.Add(model.EmailCustomer); mMess.Subject = "[muabannhadat.vn] Thông tin khách hàng"; mMess.IsBodyHtml = true; mMess.Body = RenderRazorViewToString("TemplateContactNewStarLand", model); var smClt = new SmtpClient { EnableSsl = true }; smClt.Send(mMess); return Json(true); } } catch { return Json(false); } #endregion #region store mongodb var repo = new Repository<CommonModel>("NewStarLand"); repo.Insert(model); #endregion return Json(true); }
public async Task<JsonResult> CommonContact(CommonModel model) { // validate data if (!ModelState.IsValid) return Json(false); model.Ip = Utilities.GetClientIp(); model.DateTime = DateTime.Now; // log it var log = log4net.LogManager.GetLogger("mbnd"); log.Info(JsonConvert.SerializeObject(model, Formatting.Indented)); #region store database _sql.Open(); _sql.Query<int>("XInsertCommonContact", new { Project = model.Project, DbCollection = model.DbCollection, Name = model.Name, Tel = model.Tel, Email = model.Email, Ip = model.Ip, EmailCustomer = model.EmailCustomer, Type = model.Type, Room = model.Room, Finance = model.Finance }, commandType: CommandType.StoredProcedure); _sql.Close(); #endregion #region save csv file // Create folder var exists = Directory.Exists(ConfigurationManager.AppSettings["PathReports"] + model.DbCollection); if (!exists) { Directory.CreateDirectory(ConfigurationManager.AppSettings["PathReports"] + model.DbCollection); } var path = ConfigurationManager.AppSettings["PathReports"] + model.DbCollection + "/" + model.DbCollection + ".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.Project + "\t" + model.Name + "\t" + model.Tel + "\t" + model.Email + "\t" + model.EmailCustomer + "\t" + model.DateTime + "\t" + model.Ip; if (!string.IsNullOrEmpty(model.Type)) { line += "\t" + model.Type; } if (!string.IsNullOrEmpty(model.Room)) { line += "\t" + model.Room; } if (!string.IsNullOrEmpty(model.Finance)) { line += "\t" + model.Finance; } lines.Add(line); } } catch (Exception) { } using (var writer = new StreamWriter(path, false)) { foreach (var line in lines) { writer.WriteLine(line); } writer.Close(); } } #endregion // sent email to Customer #region Send Email try { if (!String.IsNullOrEmpty(model.EmailCustomer)) { var mMess = new MailMessage { From = new MailAddress(ConfigurationManager.AppSettings["Gmail"]) }; mMess.To.Add(model.EmailCustomer); mMess.Subject = "[muabannhadat.vn] Thông tin khách hàng"; mMess.IsBodyHtml = true; mMess.Body = RenderRazorViewToString("TemplateCommonContact", model); var smClt = new SmtpClient { EnableSsl = true }; smClt.Send(mMess); return Json(true); } } catch { return Json(false); } #endregion return Json(true); }