Exemplo n.º 1
0
        public String giftFriend(int frID)
        {
            using (SQLiteCommand select = new SQLiteCommand())
            {
                //first get the Friend List
                select.CommandText = "SELECT list from friends WHERE ID=@id ";
                select.Parameters.AddWithValue("id", frID);
                int frList = Int32.Parse(db.selectScalar(select).ToString());

                select.CommandText = "SELECT gift_list from frlist WHERE ID=@id";
                select.Parameters.AddWithValue("id", frList);
                int giftList = Int32.Parse(db.selectScalar(select).ToString());

                List <Gift> gifts = new List <Gift>();  //Add the retrived gifts from the same giftlist to later pick from them
                select.CommandText = "SELECT name, weight from gift WHERE gift_list = @id";
                select.Parameters.AddWithValue("id", giftList);
                using (SQLiteConnection dbConnection = new SQLiteConnection("Data source=GiftRandom.sqlite; Version=3;"))
                {
                    select.Connection = dbConnection;
                    try
                    {
                        dbConnection.Open();
                        select.Connection = dbConnection;
                        using (SQLiteDataReader reader = select.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Gift gift = new Gift();
                                gift.weight = Int32.Parse(reader["weight"].ToString());
                                gift.name   = reader["name"].ToString();
                                gifts.Add(gift);
                            }
                            reader.Close();
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }

                select.CommandText = "SELECT SUM (weight) from gift WHERE gift_list=@id";
                select.Parameters.AddWithValue("id", giftList);
                int totalWeight = 0;
                try
                {
                    totalWeight = Int32.Parse(db.selectScalar(select).ToString());
                }
                catch (Exception)
                {
                    //there is no gifts in the assigned gift list
                    return("Please Add Gift First");
                }

                Gift selectedGift = RandomGiftSelector(gifts, totalWeight);

                //update that this friend was recently gifted
                if (selectedGift != null)
                {
                    using (SQLiteCommand update = new SQLiteCommand())
                    {
                        update.CommandText = "UPDATE friends SET last_gifted = @last_gifted WHERE ID=@id";
                        update.Parameters.AddWithValue("last_gifted", DateTime.Now.Date);
                        update.Parameters.AddWithValue("id", frID);
                        db.updateProcedure(update);
                        return(selectedGift.name);
                    }
                }
            }
            return(null);
        }