Exemple #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);
            }
        }
Exemple #2
0
 public CustomerDto GetSingleCustomer(CustomerDto request)
 {
     try
     {
         var data      = GetAllCachedData <CustomerDto>().AsQueryable();
         var predicate = PredicateBuilderHelper.True <CustomerDto>();
         if (request.Id.HasValue)
         {
             predicate = predicate.And(q => q.Id == request.Id);
         }
         predicate = predicate.And(q => q.ActivationStatus == (int)ActivationStatusType.Active);
         if (!request.UserName.IsNullOrEmpty())
         {
             predicate = predicate.And(q => q.UserName.Contains(request.UserName));
         }
         if (!request.FullName.IsNullOrEmpty())
         {
             predicate = predicate.And(q => q.FullName.Contains(request.FullName));
         }
         var result = data.Where(predicate).AsEnumerable().FirstOrDefault();
         return(result);
     }
     catch (KnownException ex)
     {
         throw ex;
     }
     catch (Exception ex)
     {
         Logger.AddLog(LogTypeEnum.Error, "CustomerManager.GetSingleCustomer", null, ex.Message, request.ToJson(), ex);
         throw new KnownException(ErrorTypeEnum.UnexpectedExeption, ex.Message, ex);
     }
 }