// Overide hàm Validate cho đặc thù riêng của entity Customer
 protected override void CustomValidate(Customer entity)
 {
     if (entity is Customer)
     {
         // Xác định property nào thực hiện validate
         var customer = entity as Customer;
         // Check các thông tin bắt buộc nhập:
         CustomerException.CheckCustomerCodeEmpty(customer.CustomerCode);
         CustomerException.CheckFullNameEmpty(customer.FullName);
         CustomerException.CheckPhoneNumberEmpty(customer.PhoneNumber);
         CustomerException.CheckEmailEmpty(customer.Email);
         // Check trùng thông tin:
         // Trùng mã khách hàng
         if (_customerRepository.CheckCustomerCodeExist(customer.CustomerCode) == true)
         {
             throw new CustomerException("Mã khách hàng đã tồn tại trên hệ thống!.");
         }
         // Trùng số điện thoại
         if (_customerRepository.CheckPhoneNumnerExits(customer.PhoneNumber) == true)
         {
             throw new CustomerException("Số điện thoại đã tồn tại trên hệ thống!.");
         }
         // Trùng Email
         if (_customerRepository.CheckEmailExists(customer.Email) == true)
         {
             throw new CustomerException("Email đã tồn tại trên hệ thống!.");
         }
     }
 }
        /// <summary>
        /// Insert 1 bản ghi vào database, nhận dữ liệu từ customerRepository, validate dữ liệu, rồi đẩy dữ liệu về controller
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public int Post(Customer customer)
        {
            //Validate dữ liệu

            //Kiểm tra xem customerCode có null hay không (phía client)
            CustomerException.CheckNullCustomerCode(customer.CustomerCode);
            //static là gọi luôn, không cần khởi tạo phương thức gọi đến nó
            //Kiểm tra xem fullName có null hay không (phía client)

            //Kiểm tra xem customerCode đã tồn tại chưa (phía server) (duplicate)
            var isExists = _customerRepository.CheckDuplicateCustomerCode(customer.CustomerCode);

            if (isExists == true)
            {
                throw new CustomerException("MÃ khách hàng đã tồn tại trên hệ thống!.");
            }

            //Kiểm tra email hợp lệ không (phía client, không cần kết nối database)
            CustomerException.CheckValidEmail(customer.Email);

            //Kiểm tra số điện thoại hợp lệ không
            CustomerException.CheckValidPhoneNumber(customer.PhoneNumber);


            var rowsAffect = _customerRepository.Post(customer);

            return(rowsAffect);
        }
        /// <summary>
        /// Update 1 bản ghi vào database dựa theo id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="customer"></param>
        /// <returns></returns>
        public int Put(Guid id, Customer customer)
        {
            //Validate dữ liệu
            //Kiểm tra email hợp lệ
            CustomerException.CheckValidEmail(customer.Email);
            //Kiểm tra số điện thoại hợp lệ
            CustomerException.CheckValidPhoneNumber(customer.PhoneNumber);

            var rowsAffect = _customerRepository.Put(id, customer);

            return(rowsAffect);
        }
Example #4
0
        /// <summary>
        /// Thêm khách hàng
        /// </summary>
        /// <param name="customer">Thông tin khách hàng</param>
        /// <returns>Số bản ghi thêm thành công</returns>
        /// CreatedBy: LMDuc (20/04/2021)
        public int Insert(Customer customer)
        {
            //// Validate dữ liệu:
            ////- check các thông tin bắt buộc nhập
            CustomerException.CheckCustomerCodeEmpty(customer.CustomerCode);
            //// - check mã có trùng hay không?
            if (CheckCustomerCodeExists(customer.CustomerCode))
            {
                throw new CustomerException("MÃ khách hàng đã tồn tại trên hệ thống.");
            }

            var rowAffect = _customerRepository.Insert(customer);

            return(rowAffect);
        }
        protected override void Validate(Customer entity, bool isInsert)
        {
            //base.Validate(entity); //Nhận mã từ base
            if (entity is Customer) //Ép kiểu
            {
                var customer = entity as Customer;
                if (isInsert == false)
                {
                    CustomerException.CheckNullCustomerId(entity.CustomerId);
                }
                else
                {
                    //Kiểm tra xem customerCode đã tồn tại chưa (phía server) (duplicate)
                    var isCustomerCodeExists = _customerRepository.CheckDuplicateCustomerCode(customer.CustomerCode);
                    if (isCustomerCodeExists == true)
                    {
                        throw new CustomerException("Mã khách hàng đã tồn tại trên hệ thống!.");
                    }

                    //Kiểm tra email đã tồn tại chưa (phía server)
                    var isEmailExists = _customerRepository.CheckDuplicateEmail(customer.CustomerCode);
                    if (isEmailExists == true)
                    {
                        throw new CustomerException("Email đã tồn tại trên hệ thống!.");
                    }

                    //Kiểm tra số điện thoại đã tồn tại chưa (phía server)
                    var isPhoneNumberExists = _customerRepository.CheckDuplicatePhoneNumber(customer.CustomerCode);
                    if (isPhoneNumberExists == true)
                    {
                        throw new CustomerException("Số điện thoại đã tồn tại trên hệ thống!.");
                    }
                }

                //Kiểm tra xem customerCode có null hay không (phía client)
                CustomerException.CheckNullCustomerCode(customer.CustomerCode);
                //static là gọi luôn, không cần khởi tạo phương thức gọi đến nó
                //Kiểm tra xem id có null hay không (phía client)

                //Kiểm tra email hợp lệ không (phía client, không cần kết nối database)
                CustomerException.CheckValidEmail(customer.Email);

                //Kiểm tra số điện thoại hợp lệ không
                CustomerException.CheckValidPhoneNumber(customer.PhoneNumber);
            }
        }