Ejemplo n.º 1
0
 // api/Parts
 /// <summary>
 /// Use Connector to connect to DB.
 /// </summary>
 /// <param name="token">The private key.</param>
 /// <returns>A list of parts that was created from the DB on Willie's Server.</returns>
 public IEnumerable<Part> Get (string token)
 {
     if (!token.Equals(key))
     {
         return new List<Part>();
     }
     else
     {
         Connector connector = new Connector();
         return connector.Get("SELECT * Parts");
     }
 }
Ejemplo n.º 2
0
        // api/Parts
        /// <summary>
        /// Use Connector to connect to DB. Formulate query to pass to DB.
        /// </summary>
        /// <param name="year">The year of the part.</param>
        /// <param name="make">The make of the part.</param>
        /// <param name="partName">The name of the part.</param>
        /// <param name="token">The private key.</param>
        /// <returns>A list of parts satisfying the query conditions that was 
        /// created from the DB on Willie's Server.</returns>
        /*public IEnumerable<Part> Get(string year, string make, string partName, string token)
        {
            if (!token.Equals(key))
            {
                return new List<Part>();
            }
            else
            {
                Connector connector = new Connector();
                if (make != null && make.Length > 1)
                {
                    make = make.Substring(0, 1);
                }

                var queryPartName = partName;
                if (queryPartName != null && queryPartName.Contains("'"))
                {
                    queryPartName = queryPartName.Replace("'", "''");
                }

                if (queryPartName != null && !queryPartName.Equals(""))
                {
                    var list = connector.Get("SELECT * FROM Parts WHERE YR = \'" + year + "\' AND "
                        + "PartName = \'" + queryPartName + "\' AND " + "Make like \'" + make + "%\'");

                    return list;
                }
                else
                {
                    return new List<Part>();
                }
            }
        }*/
        
        // This addition was put in to facilitate querying for a range of years, it returns a list of parts for a range of years
        /// 4/23/2016 addition 1 begin

        public IEnumerable<Part> Get(string year, string year2, string make, string partName, string token)
        {
            if (!token.Equals(key))
            {
                return new List<Part>();
            }
            else
            {
                Connector connector = new Connector();
                if (make != null && make.Length > 1)
                {
                    make = make.Substring(0, 1);
                }

                var queryPartName = partName;
                if (queryPartName != null && queryPartName.Contains("'"))
                {
                    queryPartName = queryPartName.Replace("'", "''");
                }

                if (queryPartName != null && !queryPartName.Equals(""))
                {
                    var list = connector.Get("SELECT * FROM Parts WHERE (YR BETWEEN\'" + year + "\' AND \'" + year2 + "\') AND (PartName = \'" + queryPartName + "\') AND (Make like \'" + make + "%\')");

                    return list;
                }
                else
                {
                    return new List<Part>();
                }
            }
        }
Ejemplo n.º 3
0
        /// 4/23/2016 addition 1 end

        // api/Parts
        /// <summary>
        /// Use Connector to connect to DB. Formulate query to pass to DB.
        /// </summary>
        /// <param name="make">The make to check.</param>
        /// <param name="token">The private key.</param>
        /// <returns>A list of valid years for the selected make.</returns>
        public IEnumerable<string> GetYearSpinner(string make, string token)
        {
            if (!token.Equals(key))
            {
                return new List<string>();
            }
            else
            {
                Connector connector = new Connector();
                if (make != null && make.Length > 1)
                {
                    make = make.Substring(0, 1);
                }
                return connector.GetYearSpinner("SELECT DISTINCT YR FROM Parts WHERE Make like \'" + make + "%\'");
            }
        }
Ejemplo n.º 4
0
        // This addition was made so that the PartName list would be populated for the range of years and not just a single year
        //4/23/2016 addition 2 begin
        public IEnumerable<string> GetPartNameSpinner(string year, string year2, string make, string token)
        {
            if (!token.Equals(key))
            {
                return new List<string>();
            }
            else
            {
                Connector connector = new Connector();
                if (make != null && make.Length > 1)
                {
                    make = make.Substring(0, 1);
                }

                var list = connector.GetPartNameSpinner("SELECT DISTINCT PartName FROM Parts WHERE (YR BETWEEN \'" + year + "\' AND \'" + year2 + "\') AND "
                    + "(Make like \'" + make + ")%\'");

                for (int i = 0; i < list.Count; i++)
                {
                    var partName = list[i];

                    TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
                    partName = textInfo.ToTitleCase(partName.ToLower());

                    if (partName != null && !partName.Equals(""))
                    {
                        list[i] = partName;
                    }
                }

                list.Sort();

                return list;
            }
        }
        // api/Payment
        /// <summary>
        /// Verify that payment was successful with PayPal. If successful, use Connector
        /// to connect to DB. Formulate insert statement to pass to DB to store transaction.
        /// Send purchase email to Willie's Cycles. Formulate delete statement to pass to DB to delete part.
        /// </summary>
        /// <param name="year">The year of the part.</param>
        /// <param name="make">The make of the part.</param>
        /// <param name="model">The model of the part.</param>
        /// <param name="partName">The name of the part.</param>
        /// <param name="location">The location of the part.</param>
        /// <param name="pkParts">The ID of the part.</param>
        /// <param name="price">The price of the part.</param>
        /// <param name="transaction">The transaction information as JSON.</param>
        /// <param name="modify">A boolean int indicating whether database should be modified.</param>
        /// <param name="token">The private key.</param>
        /// <returns>A string indicating success or the cause of failure.</returns>
        public string GetPerformPurchase(string year, string make, string model, string partName,
            string location, int pkParts, string price, string transaction, int modify, string token)
        {
            if (!token.Equals(key))
            {
                return "token";
            }
            else
            {
                try
                {
                    dynamic json = JObject.Parse(transaction);
                    string paymentId = json.response.id;

                    var config = ConfigManager.Instance.GetProperties();
                    var accessToken = new OAuthTokenCredential(config).GetAccessToken();
                    var apiContext = new APIContext(accessToken);
                    var payment = Payment.Get(apiContext, paymentId);

                    var paymentString = payment.ConvertToJson();
                    dynamic paymentJson = JObject.Parse(payment.ConvertToJson());
                    string state = paymentJson.state;
                    double amount = paymentJson.transactions[0].related_resources[0].sale.amount.total;
                    string saleState = paymentJson.transactions[0].related_resources[0].sale.state;

                    if (state.Equals("approved") && saleState.Equals("completed") && amount == double.Parse(price.Trim()))
                    {
                        var formattedMake = "";
                        if (make != null && make.Trim().Length > 0)
                        {
                            formattedMake = make.Substring(0, 1);
                        }
                        if (model != null && model.Trim().Length > 0)
                        {
                            formattedMake = formattedMake + "-" + model;
                        }

                        var queryPartName = partName;
                        if (queryPartName != null && queryPartName.Contains("'"))
                        {
                            queryPartName = queryPartName.Replace("'", "''");
                        }

                        if (modify == 0)
                        {
                            bool emailSuccess = sendEmail(year, formattedMake, queryPartName,
                                    location, pkParts, double.Parse(price.Trim()), paymentId);

                            if (emailSuccess)
                            {
                                return "Successfully sent email!";
                            }
                            else
                            {
                                return "Failed to send email.";
                            }
                        }
                        else
                        {
                            Connector connector = new Connector();
                            bool insertSuccess = connector.Insert("INSERT INTO Transactions (YR, Make, PartName, Location, pkParts, Price, PaymentID) "
                                + " VALUES (\'" + year + "\',\'" + formattedMake + "\',\'" + queryPartName + "\'"
                                + ",\'" + location + "\'," + pkParts + ",\'" + price + "\',\'" + paymentId + "\')");

                            if (insertSuccess)
                            {
                                bool emailSuccess = sendEmail(year, formattedMake, partName,
                                    location, pkParts, double.Parse(price.Trim()), paymentId);

                                if (emailSuccess)
                                {
                                    bool deleteSuccess = connector.Delete("DELETE FROM Parts WHERE pkParts = " + pkParts);

                                    if (deleteSuccess)
                                    {
                                        return "Successfully sent email and modified database!";
                                    }
                                    else
                                    {
                                        return "Failed to delete.";
                                    }
                                }
                                else
                                {
                                    return "Failed to send email.";
                                }
                            }
                            else
                            {
                                return "Failed to Insert";
                            }
                        }
                    }
                    else
                    {
                        return "Payment Values Bad";
                    }
                }
                catch (Exception e)
                {
                    return "Exception";
                }
            }
        }