Beispiel #1
0
        /// <summary>
        /// Set (add, update, delete) customer value.
        /// </summary>
        /// <param name="request">Customer request message.</param>
        /// <returns>Customer response message.</returns>
        public CustomerResponse SetCustomers(CustomerRequest request)
        {
            CustomerResponse response = new CustomerResponse();

            response.CorrelationId = request.RequestId;

            // Validate client tag, access token, and user credentials
            if (!ValidRequest(request, response, Validate.All))
            {
                return(response);
            }

            // Transform customer data transfer object to customer business object
            Customer customer = Mapper.FromDataTransferObject(request.Customer);

            // Validate customer business rules

            if (request.Action != "Delete")
            {
                if (!customer.Validate())
                {
                    response.Acknowledge = AcknowledgeType.Failure;

                    foreach (string error in customer.ValidationErrors)
                    {
                        response.Message += error + Environment.NewLine;
                    }

                    return(response);
                }
            }

            // Run within the context of a database transaction. Currently commented out.
            // The Decorator Design Pattern.
            //using (TransactionDecorator transaction = new TransactionDecorator())
            {
                if (request.Action == "Create")
                {
                    customerDao.InsertCustomer(customer);
                    response.Customer     = Mapper.ToDataTransferObject(customer);
                    response.RowsAffected = 1;
                }
                else if (request.Action == "Update")
                {
                    response.RowsAffected = customerDao.UpdateCustomer(customer);
                    response.Customer     = Mapper.ToDataTransferObject(customer);
                }
                else if (request.Action == "Delete")
                {
                    CustomerCriteria criteria = request.Criteria as CustomerCriteria;
                    Customer         cust     = customerDao.GetCustomer(criteria.CustomerId);

                    response.RowsAffected = customerDao.DeleteCustomer(cust);
                }

                //transaction.Complete();
            }

            return(response);
        }
Beispiel #2
0
 public void InsertCustomer(Customer customer)
 {
     customerDao.InsertCustomer(customer);
 }
 public int InsertCustomer(CustomerBO customer)
 {
     return(customerDao.InsertCustomer(customer));
 }
        /// <summary>
        /// Sets the customers.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public CustomerResponse SetCustomers(CustomerRequest request)
        {
            var response       = new CustomerResponse();
            var customerEntity = request.Customer;

            if (request.Action != PersistType.Delete)
            {
                if (!customerEntity.Validate())
                {
                    foreach (string error in customerEntity.ValidationErrors)
                    {
                        response.Message += error + Environment.NewLine;
                    }
                    response.Acknowledge = AcknowledgeType.Failure;
                    return(response);
                }
            }
            try
            {
                if (request.Action == PersistType.Delete)
                {
                    var customerForDelete = CustomerDao.GetCustomerById(request.CustomerId);
                    response.Message = CustomerDao.DeleteCustomer(customerForDelete);
                }
                else
                {
                    var customer = CustomerDao.GetCustomerByCode(customerEntity.CustomerCode);
                    if (request.Action == PersistType.Insert)
                    {
                        if (customer != null)
                        {
                            response.Acknowledge = AcknowledgeType.Failure;
                            response.Message     = @"Mã khách hàng " + customer.CustomerCode + @" đã tồn tại !";
                            return(response);
                        }
                        customerEntity.CustomerId = CustomerDao.InsertCustomer(customerEntity);
                        if (customerEntity.CustomerId != 0)
                        {
                            AutoNumberListDao.UpdateIncreateAutoNumberListByValue("Customer");
                        }
                        response.Message = null;
                    }
                    if (request.Action == PersistType.Update)
                    {
                        if (customer != null)
                        {
                            if (customer.CustomerId != customerEntity.CustomerId)
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                response.Message     = @"Mã khách hàng " + customer.CustomerCode + @" đã tồn tại !";
                                return(response);
                            }
                        }
                        response.Message = CustomerDao.UpdateCustomer(customerEntity);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Acknowledge = AcknowledgeType.Failure;
                response.Message     = ex.Message;
                return(response);
            }

            response.CustomerId  = customerEntity != null ? customerEntity.CustomerId : 0;
            response.Acknowledge = response.Message != null ? AcknowledgeType.Failure : AcknowledgeType.Success;
            return(response);
        }