Esempio n. 1
0
        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));
        }
 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);
 }
Esempio n. 3
0
        public ViewResult Index(PriceLookup priceLookup)
        {
            if (ModelState.IsValid)
            {
                priceLookup.Price = _priceService.GetPrice(priceLookup);
            }

            return(View(priceLookup));
        }
        public ViewResult Index(PriceLookup price)
        {
            var connectionString = @"Data Source=" + Server.MapPath("App_Data/prices.s3db");

            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(price.Upc))
                {
                    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)
                        {
                            ModelState.AddModelError("NotFound", "UPC not found");
                        }
                        else
                        {
                            price.Price = (decimal)sqlCommand.ExecuteScalar();
                        }
                        conn.Close();
                    }
                }
                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)sqlCommand.ExecuteScalar();
                        }
                        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));
        }
Esempio n. 6
0
        public void GetPriceByIsbn(PriceLookup price)
        {
            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)
                {
                    price.Price = (decimal)result;
                }
                conn.Close();
            }
        }
Esempio n. 7
0
 public decimal?GetPrice(PriceLookup priceLookup)
 {
     if (!string.IsNullOrEmpty(priceLookup.Upc))
     {
         if (_priceDb.DoesUpcExist(priceLookup.Upc))
         {
             return(_priceDb.GetPriceByUpc(priceLookup.Upc));
         }
         _modelState.AddModelError("NotFound", "UPC not found");
     }
     else if (!string.IsNullOrEmpty(priceLookup.Isbn))
     {
         if (_priceDb.DoesIsbnExist(priceLookup.Isbn))
         {
             return(_priceDb.GetPriceByIsbn(priceLookup.Isbn));
         }
         _modelState.AddModelError("NotFound", "ISBN not found");
     }
     else
     {
         _modelState.AddModelError("Required", "You must enter a UPC or an ISBN");
     }
     return(null);
 }