Ejemplo n.º 1
0
 public Rating(Deal aboutDeal, User aboutUser, bool isPositive)
 {
     AboutDeal = aboutDeal.Id;
     AboutUser = aboutUser.Id;
     IsPositive = isPositive;
     ExpirationDate = aboutDeal.ExpirationDate;
 }
Ejemplo n.º 2
0
        public async Task<int> GetRating(Deal deal)
        {
            //find all the ratings that match this deal
            var filterD = Builders<Rating>.Filter.Eq("AboutDeal", deal.Id);
            var cursor = await ratingCollection.FindAsync<Rating>(filterD);
            var ratings = await cursor.ToListAsync();

            return ratings.Sum(r => r.IsPositive ? 1 : -1);
        }
Ejemplo n.º 3
0
 private async Task AddOtherTestDeal()
 {
     var testDeal = new Deal
     {
         ProductName = "a pear of socks",
         Price = 0.25m,
         StoreName = "Amazon",
         ZipCode = "9876",
         ExpirationDate = new DateTime(2017, 2, 8, 18, 20, 1)
     };
     await db.AddDeal(testDeal);
 }
Ejemplo n.º 4
0
 private async Task AddTestDeal()
 {
     var testDeal = new Deal
     {
         ProductName = "Nintendo 3DS",
         Price = 50.00m,
         StoreName = "Amazon",
         ZipCode = "1234",
         ExpirationDate = new DateTime(2016, 5, 18, 6, 32, 0)
     };
     await db.AddDeal(testDeal);
 }
Ejemplo n.º 5
0
 public async Task AddDeal(Deal deal)
 {
     try
     {
         await dealCollection.InsertOneAsync(deal);
     }
     catch (MongoWriteException ex)
     {
         if (ex.Message.Contains("E11000"))
         {
             throw new AlreadyExistsException("A deal with the same unique information has already been added.", ex);
         }
     }
 }
Ejemplo n.º 6
0
        public async Task RateDeal(Deal whatDeal, User asUser, bool isPositive)
        {
            //find a rating if it already exists, if not, make a new rating
            var filterD = Builders<Rating>.Filter.Eq("AboutDeal", whatDeal.Id);
            var filterU = Builders<Rating>.Filter.Eq("AboutUser", asUser.Id);
            var filterFinal = filterD & filterU;

            var updateDef = Builders<Rating>.Update
                .Set("AboutDeal", whatDeal.Id)
                .Set("AboutUser", asUser.Id)
                .Set("IsPositive", isPositive)
                .Set("ExpirationDate", whatDeal.ExpirationDate);
            await ratingCollection.UpdateOneAsync(filterFinal, updateDef, new UpdateOptions { IsUpsert = true });
        }
Ejemplo n.º 7
0
 public async Task RatePositive()
 {
     //register
     await RegUserTestA();
     //login
     var apikey = await LoginUserTestA();
     //post deal
     var testDeal = new Deal
     {
         ProductName = "Nintendo 3DS",
         Price = 50.00m,
         StoreName = "Amazon",
         ZipCode = "1234",
         ExpirationDate = new DateTime(2016, 5, 18, 6, 32, 0)
     };
     var testUser = db.GetUserFromApiKey(apikey);
     testDeal.SetPostedBy(testUser);
     await db.AddDeal(testDeal);
     //like the deal
     await db.RateDeal(testDeal, testUser, true);
     //make sure it's still liked
     var rating = await db.GetRating(testDeal);
     Assert.That(rating, Is.EqualTo(1));
 }
Ejemplo n.º 8
0
        [Timeout(1000*60*2)] //better be cleaned up in 2 minutes
        public async Task TestDealExpire()
        {
            var testDeal = new Deal
            {
                ProductName = "almost-expired milk",
                Price = 0.05m,
                StoreName = "Walmart",
                ZipCode = "9876",
                ExpirationDate = DateTime.Now.AddSeconds(5)
            };
            await db.AddDeal(testDeal);
            await db.RateDeal(testDeal, new User("user", "pass"), true);

            //see if it's still there
            var found = db.GetSpecificDeal(testDeal.StoreName, testDeal.ProductName, testDeal.ExpirationDate, testDeal.Price);
            Assert.That(found, Is.Not.Null);

            var startedChecking = DateTime.Now;

            Deal foundTwo;
            do
            {
                //wait a while
                await Task.Delay(new TimeSpan(0, 0, 1));

                //it should be gone
                foundTwo =
                    await
                        db.GetSpecificDeal(testDeal.StoreName, testDeal.ProductName, testDeal.ExpirationDate,
                            testDeal.Price);
            } while (foundTwo != null);

            var timeTaken = DateTime.Now - startedChecking;
            Console.WriteLine($"Should have taken 5 seconds but {timeTaken.TotalMilliseconds} ms is fine too");
        }
Ejemplo n.º 9
0
        public IActionResult Get(string productName = "", string storeName = "", int zipcode = -1)
        {
            try
            {
                if (String.IsNullOrEmpty(Authorize(Request)))
                {
                    return HttpBadRequest();
                }

                MySqlConnection connection = new MySqlConnection(connectionString);
                connection.Open();
                MySqlCommand comm = connection.CreateCommand();

                // The base query: select all columns of all entries in the DEAL table
                comm.CommandText = "SELECT d.* FROM DEAL_T d WHERE d.EXPIRATION_DATE > CURDATE()";

                // Depending on the query parameters, we want to append restrictive clauses onto the query
                // to narrow down the search
                if (!String.IsNullOrEmpty(productName) || !String.IsNullOrEmpty(storeName) || zipcode > 0)
                {
                    bool hasClause = false;

                    if (!String.IsNullOrEmpty(productName))
                    {
                        // This clause limits the results to deals whose product's name equals the productname query
                        // parameter
                        comm.CommandText += " AND ";
                        comm.CommandText += " d.FK_PRODUCT_ID in (SELECT PK_PRODUCT_ID FROM PRODUCT_T p WHERE p.PRODUCT_NAME=@product_name)";
                        comm.Parameters.AddWithValue("@product_name", productName);
                        hasClause = true;
                    }

                    if (!String.IsNullOrEmpty(storeName))
                    {
                        // This clause limits the results to deals whose store's name equals the storename
                        // query parameter
                        comm.CommandText += " AND ";
                        comm.CommandText += " d.FK_STORE_ID in (SELECT PK_STORE_ID FROM STORE_T s WHERE s.STORE_NAME=@store_name)";
                        comm.Parameters.AddWithValue("@store_name", storeName);
                        hasClause = true;
                    }

                    if (zipcode > 0)
                    {
                        // This clause limits the results to deals whose store's zip code equals the zipcode query
                        // parameter
                        comm.CommandText += " AND ";
                        comm.CommandText += " d.FK_STORE_ID in (SELECT PK_STORE_ID FROM STORE_T s WHERE s.ZIP_CODE=@zip_code)";
                        comm.Parameters.AddWithValue("@zip_code", zipcode);
                        hasClause = true;
                    }
                }

                // List to return to the user
                List<Deal> deals = new List<Deal>();
                // This while loop reads all of the results
                MySqlDataReader reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    // Create a new Deal object and assign to its fields the 
                    // values in each of the 6 columns. The values in the columns
                    // have to be read based on their data types
                    Deal deal = new Deal();
                    deal.DealId = reader.GetInt32(0);
                    deal.UserId = reader.GetInt32(1);
                    deal.ProductId = reader.GetInt32(2);
                    deal.StoreId = reader.GetInt32(3);
                    deal.Price = reader.GetFloat(4);
                    deal.ExpirationDate = reader.GetDateTime(5);
                    deals.Add(deal);
                }

                // Close the database connection and return the list of deals. This list will automatically
                // get serialized into JSON (as will all object returns).
                connection.Close();
                return new ObjectResult(deals);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.StackTrace);
                return HttpBadRequest();
            }
        }
Ejemplo n.º 10
0
 private async Task SetRating(Deal deal)
 {
     var rating = await GetRating(deal);
     deal.Rating = rating;
 }