public static crm_Contacts ToEntity(this ContactModel model) { var _contactEntity = new crm_Contacts(); AutoMapper.Mapper.CreateMap <ContactModel, crm_Contacts>(); AutoMapper.Mapper.Map(model, _contactEntity); return(_contactEntity); }
public static ContactModel ToModel(this crm_Contacts entity) { var _contactModel = new ContactModel(); AutoMapper.Mapper.CreateMap <crm_Contacts, ContactModel>(); AutoMapper.Mapper.Map(entity, _contactModel); return(_contactModel); }
public static CustomerContactModel GetCustomerInfor(this IRepository <crm_Customers> repository, int id) { var customer = repository.GetRepository <crm_Customers>().Queryable(); var contact = repository.GetRepository <crm_Contacts>().Queryable(); CustomerContactModel query; crm_Contacts contactEntity = null; contactEntity = contact.Where(x => x.CustomerId == id && x.IsDefault == true).FirstOrDefault(); string firstName = contactEntity == null ? "" : contactEntity.FirstName; string lastName = contactEntity == null ? "" : contactEntity.LastName; string address = contactEntity == null ? "" : contactEntity.Address; string contactPhone = contactEntity == null ? "" : contactEntity.ContactPhone; string mobilePhone = contactEntity == null ? "" : contactEntity.MobilePhone; int contactID = contactEntity == null ? 0 : contactEntity.ContactId; query = (from r in customer where r.CustomerId == id select new CustomerContactModel { CustomerId = r.CustomerId, CustomerName = r.CustomerName, MobilePhone = mobilePhone, ContactId = contactID, Email = r.Email, FullName = firstName + " " + lastName, CustomerLogo = r.CustomerLogo, OrgNumber = r.OrgNumber, CreatedDate = r.CreatedDate, PostedAddress = r.PostedAddress, VisitingAddress = r.VisitingAddress, CountryId = r.CountryId, Fax = r.Fax, Description = r.Description, ContactPhone = contactPhone, Address = address, LinkedURL = r.LinkedURL, FacebookURL = r.FacebookURL, GoogleplusURL = r.GoogleplusURL, TwitterURL = r.TwitterURL, Website = r.Website, FirstName = firstName, LastName = lastName, }).FirstOrDefault(); query.TotalContact = contact.Where(x => x.CustomerId == id).Count(); return(query); }
public string AddEditAction(CustomerModel model) { //Variables ActionResultModel resultModel = new ActionResultModel(); crm_Customers customer = null; crm_Contacts contact = null; UserInfo usInfo = System.Web.HttpContext.Current.Session["UserInfo"] as UserInfo; bool isSaveImageSuccess = true; string pathFiles = "/Tenants/" + _userInfo.TenantAlias; long orgNumber = 0; try { //Check case add or edit if (model.CustomerId > 0) { customer = _customerService.GetCustomerByID(model.CustomerId); if (model.ContactId > 0) { contact = _contactService.GetContactDefaultByCusID(model.CustomerId); } } else { customer = new crm_Customers(); } #region Validate data //Check email vaild if (!GlobalFunctions.IsValidEmail(model.Email.Trim())) { resultModel.IsSuccess = 0; resultModel.Message = "Email is invalid!"; return(JsonConvert.SerializeObject(resultModel)); } if (_countryService.GetCountryById(model.CountryId) == null) { resultModel.IsSuccess = 0; resultModel.Message = "Country is not exist!"; return(JsonConvert.SerializeObject(resultModel)); } if (customer == null && model.CustomerId > 0) { resultModel.IsSuccess = 0; resultModel.Message = "Customer is not exist!"; return(JsonConvert.SerializeObject(resultModel)); } if (!long.TryParse(model.OrgNumber, out orgNumber)) { resultModel.IsSuccess = 0; resultModel.Message = "Org number must be numeric!"; return(JsonConvert.SerializeObject(resultModel)); } if (!_customerService.CheckOrgNumberExist(model.CustomerId, model.OrgNumber.Trim())) { resultModel.IsSuccess = 0; resultModel.Message = "Org number is exist!"; return(JsonConvert.SerializeObject(resultModel)); } #endregion #region Set value for customer entity customer.CustomerName = WebUtility.HtmlEncode(model.CustomerName.Trim()); customer.Email = model.Email.Trim(); customer.PostedAddress = WebUtility.HtmlEncode(model.PostedAddress.Trim()); customer.VisitingAddress = WebUtility.HtmlEncode(model.VisitingAddress.Trim()); customer.CountryId = model.CountryId; customer.Fax = WebUtility.HtmlEncode(model.Fax.Trim()); customer.OrgNumber = model.OrgNumber; customer.CreatedDate = DateTime.Now; customer.CreatedBy = usInfo.ID; customer.Description = WebUtility.HtmlEncode(model.Description.Trim()); customer.Website = WebUtility.HtmlEncode(model.Website); customer.LinkedURL = WebUtility.HtmlEncode(model.LinkedURL); customer.FacebookURL = WebUtility.HtmlEncode(model.FacebookURL); customer.TwitterURL = WebUtility.HtmlEncode(model.TwitterURL); customer.GoogleplusURL = WebUtility.HtmlEncode(model.GoogleplusURL); customer.CustomerLogo = _logoModel.FileName != null ? _logoModel.FileName : model.CustomerId > 0?customer.CustomerLogo:""; #endregion #region Set value for contact entity if (model.FirstName.Trim() != string.Empty || model.LastName.Trim() != string.Empty || model.Address.Trim() != string.Empty || model.ContactPhone.Trim() != string.Empty || model.MobilePhone.Trim() != string.Empty) { if (contact != null) { contact.FirstName = model.FirstName.Trim() != string.Empty ? WebUtility.HtmlEncode(model.FirstName.Trim()) : contact.FirstName; contact.LastName = model.LastName.Trim() != string.Empty ? WebUtility.HtmlEncode(model.LastName.Trim()) : contact.LastName; contact.ContactPhone = model.ContactPhone.Trim() != string.Empty ? WebUtility.HtmlEncode(model.ContactPhone.Trim()) : contact.ContactPhone; contact.MobilePhone = model.MobilePhone.Trim() != string.Empty ? WebUtility.HtmlEncode(model.MobilePhone.Trim()) : contact.MobilePhone; contact.Address = model.Address.Trim() != string.Empty ? WebUtility.HtmlEncode(model.Address.Trim()) : contact.Address; } else { contact = new crm_Contacts(); contact.FirstName = WebUtility.HtmlEncode(model.FirstName.Trim()); contact.LastName = WebUtility.HtmlEncode(model.LastName.Trim()); contact.ContactPhone = WebUtility.HtmlEncode(model.ContactPhone.Trim()); contact.MobilePhone = WebUtility.HtmlEncode(model.MobilePhone.Trim()); contact.Address = WebUtility.HtmlEncode(model.Address.Trim()); contact.IsDefault = true; } } #endregion #region Perform save data //Save image try { if (_logoModel != null && !string.IsNullOrEmpty(_logoModel.FileName)) { //move a file from temps file to tenant folder var _sourceFile = Path.Combine(Server.MapPath(_tempFiles), _logoModel.FileName); var _destinationFile = Path.Combine(Server.MapPath(pathFiles), _logoModel.FileName); if (System.IO.File.Exists(_destinationFile)) { System.IO.File.Delete(_destinationFile); } System.IO.File.Move(_sourceFile, _destinationFile); _logoModel = null; } } catch { isSaveImageSuccess = false; } if (isSaveImageSuccess) { //Add if (model.CustomerId <= 0) { using (TransactionScope scope = new TransactionScope()) { _customerService.Insert(customer); _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); if (contact != null) { contact.CustomerId = customer.CustomerId; _contactService.Insert(contact); _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); } scope.Complete(); } } else//Edit { using (TransactionScope scope = new TransactionScope()) { _customerService.Update(customer); _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); if (contact != null) { contact.CustomerId = customer.CustomerId; if (contact.ContactId > 0) { _contactService.Update(contact); } else { _contactService.Insert(contact); } _unitOfWork.SaveChanges(); //_tenantUnitOfWork.SaveChanges(); } scope.Complete(); } } resultModel.IsSuccess = 1; resultModel.Message = "Data were saved successfully!"; _helper.InsertLogActive(_logService, _unitOfWork, "Customers", model.CustomerId <= 0 ? "Insert new customer" : "Update customer", model.CustomerId <= 0 ? 1 : 2, true); } else { resultModel.IsSuccess = 0; resultModel.Message = "Save image unsuccessfully!"; _helper.InsertLogActive(_logService, _unitOfWork, "Customers", "Save avatar image.", 1, false); } #endregion } catch (TransactionAbortedException te) { _helper.InsertLogActive(_logService, _unitOfWork, "Customers", model.CustomerId <= 0? "Insert new customer":"Update customer", model.CustomerId <= 0?1:2, false); } catch (ApplicationException ex) { _helper.InsertLogActive(_logService, _unitOfWork, "Customers", model.CustomerId <= 0 ? "Insert new customer" : "Update customer", model.CustomerId <= 0 ? 1 : 2, false); resultModel.IsSuccess = 0; resultModel.Message = "Data were saved unsuccessfully!"; } return(JsonConvert.SerializeObject(resultModel)); }