示例#1
0
        public CustomerDto CreateOrUpdateCustomer(CustomerDto dto, int RequestUserId, string TokenKey)
        {
            try
            {
                CheckAuthentication(RequestUserId, TokenKey);
                #region Empty Control
                if (dto.UserName.IsNullOrEmpty())
                {
                    throw new RequestWarningException(ErrorTypeEnum.WarningException, ExceptionCodeHelper.CannotEmptyField, ExceptionMessageHelper.CannotEmptyField("User Name"));
                }
                if (dto.Password.IsNullOrEmpty())
                {
                    throw new RequestWarningException(ErrorTypeEnum.WarningException, ExceptionCodeHelper.CannotEmptyField, ExceptionMessageHelper.CannotEmptyField("Password"));
                }
                if (dto.FullName.IsNullOrEmpty())
                {
                    throw new RequestWarningException(ErrorTypeEnum.WarningException, ExceptionCodeHelper.CannotEmptyField, ExceptionMessageHelper.CannotEmptyField("Full Name"));
                }
                #endregion

                var data = GetAllCachedData <CustomerDto>().ToList();

                #region Field Control
                if (data != null)
                {
                    if (data.Any(q => q.UserName == dto.UserName))
                    {
                        throw new RequestWarningException(ErrorTypeEnum.WarningException, ExceptionCodeHelper.IsInUse, ExceptionMessageHelper.IsInUse("UserName"));
                    }
                }
                #endregion

                var entity = dto.ConvertTo <CustomerEntity>();
                var Pass   = PasswordHelper.GeneratePassword(6);
                entity.Password = (dto.Id > 0)
                    ? PasswordHelper.EncryptData(dto.Password)
                    : PasswordHelper.EncryptData(Pass);
                var conn = Db.CreateConnection(true);
                if (dto.Id > 0)
                {
                    entity.UpdateUser = RequestUserId;
                    entity.UpdateDate = DateTimeHelper.Now;
                    conn.Update(entity, Db._DbTransaction);
                    data.RemoveAt(data.FindIndex(q => q.Id == entity.Id));
                }
                else
                {
                    int Id = conn.Insert(entity, Db._DbTransaction).ToInt();
                    entity = conn.Get <CustomerEntity>(Id);
                }
                var result = entity.ConvertTo <CustomerDto>();
                data.Add(result);
                FillCacheData(data);
                return(result);
            }
            catch (KnownException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                Logger.AddLog(LogTypeEnum.Error, "CustomerManager.CreateOrUpdateCustomer", RequestUserId, ex.Message, dto.ToJson(), ex);
                throw new KnownException(ErrorTypeEnum.UnexpectedExeption, ex.Message, ex);
            }
        }