/// <summary>
        /// Adds a request for a particular stock to the database.
        /// </summary>
        /// <param name="stock">
        /// Stock info such as price and phone number to message.
        /// </param>
        /// <returns>
        /// True if adding the request was successful, otherwise false.
        /// </returns>
        public bool AddRequest(Stock stock)
        {
            bool success = true;

            try
            {
                if (limit.IsOverLimit(stock.Phone))
                {
                    return(false);
                }

                var request = new RequestRecord
                {
                    RequestId     = stock.RequestId,
                    Price         = stock.Price,
                    TwilioBinding = Guid.NewGuid().ToString()
                };

                twilio.BindUser(request.TwilioBinding, stock.Phone);

                context.Requests.Add(request);
                limit.Increment(stock.Phone);
                context.SaveChanges();
            }
            catch (DbUpdateException dbException)
            {
                var exception = (Npgsql.PostgresException)dbException.InnerException;
                Console.WriteLine(exception.SqlState);
                success = false;
            }
            return(success);
        }
Exemple #2
0
        /// <summary>
        /// Retrieve matching companies based on the query.
        /// </summary>
        /// <param name="query">
        /// A searchphrase that matches against symbols and company names.
        /// </param>
        /// <returns>A collection of companies that match the query.</returns>
        public IEnumerable <Company> SearchCompanies(Query query)
        {
            var table = context.Companies;
            IEnumerable <Company> companies = new List <Company>();

            if (query.SearchPhrase.Length == 0)
            {
                return(null);
            }

            var searchPhraseBlocks = query.SearchPhrase.Split(" ");
            var newSearchPhrase    = new StringBuilder();

            //Capitalizes the searchphrase to match against database
            foreach (var block in searchPhraseBlocks)
            {
                var newBlock = block.Substring(0, 1).ToUpper() + block.Substring(1) + " ";
                newSearchPhrase.Append(newBlock);
            }
            companies = table
                        .Where(c => c.Symbol.Contains(query.SearchPhrase.ToUpper()) || c.Name.Contains(newSearchPhrase.ToString().TrimEnd()))
                        .Take(5)
                        .AsEnumerable();
            context.SaveChanges();

            return(companies);
        }
Exemple #3
0
        /// <summary>
        /// Adds a record for a user's request.
        /// </summary>
        /// <param name="phone">
        /// U.S. phone number (e.g. +15551234567)
        /// </param>
        /// <returns>True if successful, otherwise false.</returns>
        public bool AddEntry(string phone)
        {
            var newEntry = new LimitCount
            {
                PhoneHash = MakeHash(phone),
                Count     = 0,
                Date      = DateTime.UtcNow.ToString()
            };

            try
            {
                context.LimitCounts.Add(newEntry);
                context.SaveChanges();
                return(true);
            }
            catch (DbUpdateException dbException)
            {
                var exception = (Npgsql.PostgresException)dbException.InnerException;
                Console.WriteLine(exception.Message);
                return(false);
            }
        }