public ViewResult Index(PriceLookup price)
        {
            var connectionString = @"Data Source=" + Server.MapPath("App_Data/prices.s3db");
            var priceDb = new PriceDb(connectionString);
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(price.Upc))
                {
                    priceDb.GetPriceByUpc(price);
                    if(price.Price == null)
                        ModelState.AddModelError("NotFound", "UPC not found");
                }
                else if (!string.IsNullOrEmpty(price.Isbn))
                {
                    var sql = "SELECT Price FROM Prices WHERE Isbn = @isbn";
                    using (var conn = new SQLiteConnection(connectionString))
                    {
                        conn.Open();
                        var sqlCommand = new SQLiteCommand(sql, conn);
                        sqlCommand.Parameters.AddWithValue("isbn", price.Isbn);
                        var result = sqlCommand.ExecuteScalar();
                        if (result == null)
                            ModelState.AddModelError("NotFound", "ISBN not found");
                        else
                            price.Price = (decimal)result;
                        conn.Close();
                    }
                }
                else
                    ModelState.AddModelError("Required", "You must enter a UPC or an ISBN");
            }

            return View(price);
        }
        public ViewResult Index(PriceLookup price)
        {
            var connectionString = @"Data Source=" + Server.MapPath("App_Data/prices.s3db");
            var priceDb = new PriceDb(connectionString);
            var priceSvc = new PriceService(ModelState, priceDb);
            if (ModelState.IsValid)
                price.Price = priceSvc.GetPrice(price);

            return View(price);
        }
 public void GetPriceByUpc(PriceLookup price)
 {
     var sql = "SELECT Price FROM Prices WHERE Upc = @upc";
     using (var conn = new SQLiteConnection(_connectionString))
     {
         conn.Open();
         var sqlCommand = new SQLiteCommand(sql, conn);
         sqlCommand.Parameters.AddWithValue("upc", price.Upc);
         var result = sqlCommand.ExecuteScalar();
         if (result != null)
             price.Price = (decimal)result;
         conn.Close();
     }
 }
 public decimal? GetPrice(PriceLookup price)
 {
     if (!string.IsNullOrEmpty(price.Upc))
     {
         var priceValue = _priceDb.GetPriceByUpc(price.Upc);
         if (priceValue == null)
             _modelState.AddModelError("NotFound", "UPC not found");
         return priceValue;
     }
     else if (!string.IsNullOrEmpty(price.Isbn))
     {
         var priceValue = _priceDb.GetPriceByIsbn(price.Isbn);
         if (price.Price == null)
             _modelState.AddModelError("NotFound", "ISBN not found");
         return priceValue;
     }
     else
         _modelState.AddModelError("Required", "You must enter a UPC or an ISBN");
     return null;
 }
        public ViewResult Index(PriceLookup price)
        {
            var connectionString = @"Data Source=" + Server.MapPath("App_Data/prices.s3db");
            var priceDb = new PriceDb(connectionString);
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(price.Upc))
                {
                    priceDb.GetPriceByUpc(price);
                    if(price.Price == null)
                        ModelState.AddModelError("NotFound", "UPC not found");
                }
                else if (!string.IsNullOrEmpty(price.Isbn))
                {
                    priceDb.GetPriceByIsbn(price);
                    if (price.Price == null)
                        ModelState.AddModelError("NotFound", "ISBN not found");
                }
                else
                    ModelState.AddModelError("Required", "You must enter a UPC or an ISBN");
            }

            return View(price);
        }