//method to create a new product
        public bool InsertNewProduct(Product product)
        {
            using (SqlConnection sqlCon = new SqlConnection(Config.ConnString))
            {
                sqlCon.Open();

                var sql = "INSERT INTO Inventory(GameTitle, Category, Platform, AvailableUnits, Cost , Price, Condition, ProductType)" +
                          $"VALUES('{product.GameTitle}', '{product.Category}',  '{product.Platform}', {product.AvailableUnits}," +
                          $"cast({product.Cost} as money),  {product.Price},  '{product.Condition}',  '{product.ProductType}' )";
                try
                {
                    SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
                    sqlCmd.ExecuteNonQuery();
                    return(true);
                }
                catch (Exception ex)
                {
                    var Err = new CreateLogFiles();
                    Err.ErrorLog(Config.PathToData + "err.log", ex.Message);
                    return(false);

                    throw;
                }
            }
        }
        public bool UpdateProductById(Product product)
        {
            using (SqlConnection sqlCon = new SqlConnection(Config.ConnString))
            {
                sqlCon.Open();
                {
                    var sql = $"UPDATE Inventory SET GameTitle = '{product.GameTitle}', " +
                              $"Category = '{product.Category}', " +
                              $"Platform = '{product.Platform}', " +
                              $"AvailableUnits = {product.AvailableUnits}, " +
                              $"Cost = {product.Cost}, " +
                              $"Price = {product.Price}, " +
                              $"Condition = '{product.Condition}', " +
                              $"ProductType = '{product.ProductType}' " +
                              $"WHERE productId = {product.productId}";
                    try
                    {
                        SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
                        sqlCmd.ExecuteNonQuery();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        var Err = new CreateLogFiles();
                        Err.ErrorLog(Config.PathToData + "err.log", ex.Message);
                        return(false);

                        throw;
                    }
                }
            }
        }
        //method to add credit card orders
        public bool CreateCreditCardOrder(Order order)
        {
            CreditCardValidationService card = new CreditCardValidationService();

            if (card.Validate(order.CreditCardNumber))
            {
                var ccEncrypted    = CcOperation.Encrypt(Config.Key, order.CreditCardNumber);
                var lastFourDigits = order.CreditCardNumber.Substring(order.CreditCardNumber.Length - 4, 4);

                using (SqlConnection sqlCon = new SqlConnection(Config.ConnString))
                {
                    sqlCon.Open();
                    try
                    {
                        SqlCommand cmd = new SqlCommand("spCreateCreditCardOrder", sqlCon);

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@Product", order.Product));
                        cmd.Parameters.Add(new SqlParameter("@Quantity", order.Quantity));
                        cmd.Parameters.Add(new SqlParameter("@Condition", order.Condition));
                        cmd.Parameters.Add(new SqlParameter("@Date", order.Date));
                        cmd.Parameters.Add(new SqlParameter("@Total", order.Total));
                        cmd.Parameters.Add(new SqlParameter("@CustomerName", order.CustomerName));
                        cmd.Parameters.Add(new SqlParameter("@CustomerPhoneNumber", order.CustomerPhoneNumber));
                        cmd.Parameters.Add(new SqlParameter("@Email", order.Email));
                        cmd.Parameters.Add(new SqlParameter("@SaleType", order.SaleType));
                        cmd.Parameters.Add(new SqlParameter("@CreditCardName", order.CreditCardName));
                        cmd.Parameters.Add(new SqlParameter("@CreditCardNumber", lastFourDigits));
                        cmd.Parameters.Add(new SqlParameter("@EncryptedCreditCardNumber", ccEncrypted));
                        cmd.Parameters.Add(new SqlParameter("@ExpirationDate", order.ExpirationDate));
                        cmd.Parameters.Add(new SqlParameter("@SecurityCode", order.SecurityCode));

                        cmd.ExecuteNonQuery();

                        var sql2 = $"UPDATE Inventory SET AvailableUnits = AvailableUnits - {order.Quantity} WHERE GameTitle = '{order.Product}'";

                        using (SqlConnection sqlConnection = new SqlConnection(Config.ConnString))
                        {
                            sqlConnection.Open();
                            cmd = new SqlCommand(sql2, sqlConnection);
                            cmd.ExecuteNonQuery();
                        }
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        var Err = new CreateLogFiles();
                        Err.ErrorLog(Config.PathToData + "err.log", ex.Message);
                        return(false);

                        throw;
                    }
                }
            }
            else
            {
                return(false);
            }
        }
        //method to add cash orders
        public bool CreateCashOrder(Order order)
        {
            using (SqlConnection sqlCon = new SqlConnection(Config.ConnString))
            {
                sqlCon.Open();
                try
                {
                    //selecting stored procedure and adding all parameters
                    SqlCommand cmd = new SqlCommand("spCreateCashOrder", sqlCon);

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Product", order.Product));
                    cmd.Parameters.Add(new SqlParameter("@Quantity", order.Quantity));
                    cmd.Parameters.Add(new SqlParameter("@Condition", order.Condition));
                    cmd.Parameters.Add(new SqlParameter("@Date", order.Date));
                    cmd.Parameters.Add(new SqlParameter("@Total", order.Total));
                    cmd.Parameters.Add(new SqlParameter("@CustomerName", order.CustomerName));
                    cmd.Parameters.Add(new SqlParameter("@CustomerPhoneNumber", order.CustomerPhoneNumber));
                    cmd.Parameters.Add(new SqlParameter("@Email", order.Email));
                    cmd.Parameters.Add(new SqlParameter("@SaleType", order.SaleType));

                    cmd.ExecuteNonQuery();
                    //removing items from inventory
                    var sql2 = $"UPDATE Inventory SET AvailableUnits = AvailableUnits - {order.Quantity} WHERE GameTitle = '{order.Product}'";

                    using (SqlConnection sqlConnection = new SqlConnection(Config.ConnString))
                    {
                        sqlConnection.Open();
                        cmd = new SqlCommand(sql2, sqlConnection);
                        cmd.ExecuteNonQuery();
                    }
                    return(true);
                }
                catch (Exception ex)
                {
                    var Err = new CreateLogFiles();
                    Err.ErrorLog(Config.PathToData + "err.log", ex.Message);
                    return(false);

                    throw;
                }
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            string input;

            while (true)
            {
                Console.WriteLine("Type Import to upload data or Retrieve to display data from database");
                input = Console.ReadLine().Trim();

                if (input.ToLower() == "import" || input.ToLower() == "retrieve")
                {
                    break;
                }
            }


            if (input.ToLower() == "import")
            {
                while (true)
                {
                    Console.WriteLine("Type JSON to save data to a JSON file or SQL to save data to the database");
                    input = Console.ReadLine().Trim();
                    if (input.ToLower() == "json" || input.ToLower() == "sql")
                    {
                        break;
                    }
                }


                if (input.ToLower() == "json")
                {
                    Console.WriteLine("json input");
                    try
                    {
                        //string builder necessary to parse csv to JSON format

                        StringBuilder JInventoryrecords = new StringBuilder();
                        StringBuilder JSalesrecords     = new StringBuilder();

                        //transforming into json

                        var JInventorySb = new InventoryManagementService(JInventoryrecords);
                        JInventorySb.ProcessProductsFromCsv(Config.PathToInvetoryFile);

                        var JSalesSb = new InventoryManagementService(JSalesrecords);
                        JSalesSb.ProcesOrdersFromCsv(Config.PathToSalesFile);


                        //making file out of json string

                        var JInventoryFile = new CsvToJsonConverter();
                        JInventoryFile.ProcessCsvFile(JInventoryrecords, Config.PathToData + "JInventoryFile.json");
                        var JSalesFile = new CsvToJsonConverter();
                        JSalesFile.ProcessCsvFile(JSalesrecords, Config.PathToData + "JSalesFile.json");
                    }
                    catch (Exception ex)
                    {
                        var Err = new CreateLogFiles();
                        Err.ErrorLog(Config.PathToData + "err.log", ex.Message);
                        Console.WriteLine("Fatal error : " + ex.Message + ", please find a complete error at ErrorLog file");
                        throw;
                    }
                    Console.WriteLine("Updated database succesfully");
                }
                else if (input.ToLower() == "sql")
                {
                    try
                    {
                        var dropData = new InventoryManagementService();
                        dropData.DropAllData();

                        var CsvProductCharRecords = new InventoryManagementService();
                        CsvProductCharRecords.SaveProductChar(Config.PathToInvetoryFile);

                        var CsvInvetoryRecords = new InventoryManagementService();
                        CsvInvetoryRecords.SaveCsvInventory(Config.PathToInvetoryFile);

                        var CsvSalesRecords = new InventoryManagementService();
                        CsvSalesRecords.SaveCsvOrders(Config.PathToSalesFile);
                    }
                    catch (Exception ex)
                    {
                        var Err = new CreateLogFiles();
                        Err.ErrorLog(Config.PathToData + "err.log", ex.Message);
                        Console.WriteLine("Fatal error : " + ex.Message + ", please find a complete error at ErrorLog file");
                        throw;
                    }
                    Console.WriteLine("Updated database succesfully");
                }
            }
            else if (input == "retrieve")
            {
                StringBuilder records = new StringBuilder();
                Console.WriteLine(DisplayDbData.displayAllData(records).ToString());
            }
        }