Example #1
0
        //public QuoteRec  Post([FromBody] QuoteRec submittedQuote)
        //{
        //    System.Diagnostics.Debug.WriteLine(submittedQuote.ToString());
        //    QuoteRec qr = submittedQuote;
        //    AddQuote(qr);
        //    return qr;
        //}

        // public List<QuoteRec> Post([FromBody] string RowKey, [FromBody] bool VoteUp)

        public List <QuoteRec> Post(string RowKey, bool VoteUp)
        {
            List <QuoteRec> QuoteRecs = QuoteDB.GetQuoteList();
            QuoteRec        qr        = QuoteRecs.Find(q => q.RowKey == RowKey);

            if (qr == null)
            {
                return(ReturnNotFound());
            }

            if (VoteUp)
            {
                qr.VoteUp++;
            }
            else
            {
                qr.VoteDown++;
            }
            List <QuoteRec> quoterecs = new List <QuoteRec>()
            {
                qr
            };

            // WIP update Table Store data, and to be really cool, update it in the background after returning to the browser

            return(quoterecs);
        }
        private bool RecreateTableStorage()
        {
            CloudTable tableReference = QuoteDB.GetQuotesTableReference();
            DateTime   dtTimeout      = DateTime.Now.AddSeconds(20);

            try
            {
                tableReference.Create();
            }
            catch (StorageException ex)
            {
                if ((ex.RequestInformation.HttpStatusCode == 409) &&
                    (ex.RequestInformation.ExtendedErrorInformation.ErrorCode.Equals(TableErrorCodeStrings.TableBeingDeleted)) &&
                    (DateTime.Now < dtTimeout)
                    )
                {
                    Thread.Sleep(2000);// The table is currently being deleted. Try again until it works.
                }
                else
                {
                    new LogException(ex);
                    throw new TimeoutException("Timeout waiting for empty database", ex);
                }
            }
            catch (Exception ex)
            {
                new LogException(ex);
                throw;
            }

            return(AddQuotesToNewTable());
        }
Example #3
0
        public List <QuoteRec> Get(string id, int maxToReturn)
        {
            List <QuoteRec> returned = new List <QuoteRec>();

            if (id == null || id == "")
            {
                id = " ";
            }
            id = id.ToLower();

            List <QuoteRec> QuoteRecs = QuoteDB.GetQuoteList();

            if (QuoteRecs == null)
            {
                return(ReturnNotFound());
            }

            // WIP there has to be a better string ignore case comparison
            var results = QuoteRecs
                          .Where(q => q.Quote.ToLower().Contains(id) == true || q.Author.ToLower().Contains(id))
                          .OrderBy(r => Guid.NewGuid())
                          .Take(maxToReturn);

            ;

            foreach (var qr in results)
            {
                returned.Add(qr);
            }

            return(returned);
        }
Example #4
0
        public ActionResult Browse()
        {
            QuoteDB qdb = new QuoteDB();

            ViewData.Model = qdb.GetQuotes();
            return(View());
        }
Example #5
0
        //
        // GET: /Quotes/

        public ActionResult Index()
        {
            QuoteDB qdb = new QuoteDB();

            ViewData.Model = qdb.GetQuotes().Reverse();
            return(View());
        }
Example #6
0
        public List <QuoteRec> Get()
        {
            List <QuoteRec> returned = new List <QuoteRec>();

            List <QuoteRec> QuoteRecs = QuoteDB.GetQuoteList();
            QuoteRec        qr        = QuoteRecs[random.Next(0, QuoteRecs.Count)];

            returned.Add(qr);
            return(returned);
        }
Example #7
0
        public ActionResult Index(int? id)
        {
            QuoteDB qdb = new QuoteDB();

            if (!id.HasValue)
                id = 1;

            ViewData.Model = qdb.GetQuote(id.Value);

            return View();
        }
Example #8
0
        public ActionResult Index(int?id)
        {
            QuoteDB qdb = new QuoteDB();

            if (!id.HasValue)
            {
                id = 1;
            }

            ViewData.Model = qdb.GetQuote(id.Value);

            return(View());
        }
        private bool AddQuotesToNewTable()
        {
            bool Worked = false;

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            try
            {
                CloudTable tableReference = QuoteDB.GetQuotesTableReference();

                //
                // this process of adding batchs of 90 came from an error
                // when I created all 220 entries in one batch
                // error received: "Unexpected response code for operation : 99"
                // solution option: http://stackoverflow.com/questions/18170920/azure-table-storage-error-unexpected-response-code-for-operation-99
                //

                var quoteList = BasicQuotes.GetQuoteList();
                int CntQuote  = 0;
                foreach (var chunk in quoteList.Chunk(90))
                {
                    TableBatchOperation batchOperation = new TableBatchOperation();

                    foreach (var quote in chunk)
                    {
                        var      thisQuote = BasicQuotes.BuildQuote(quote);
                        QuoteRec q         = new QuoteRec {
                            Author = thisQuote.Author, Quote = thisQuote.Quote, ModeratorApproved = true, QuoteSubmitter = Guid.Empty
                        };
                        QuoteTableEntity qte = new QuoteTableEntity {
                            PartitionKey = "quoter", RowKey = Guid.NewGuid().ToString("N"), idxQuoteAsLoaded = ++CntQuote
                        };
                        qte.StoreQuoteRecord(q);
                        batchOperation.Insert(qte);
                    }
                    tableReference.ExecuteBatch(batchOperation);
                }
                Worked = true;
            }
            catch (Exception ex)
            {
                new LogException(ex);
            }
            finally
            {
                sw.Stop();
                System.Diagnostics.Debug.WriteLine(">>>> Table Load times:" + sw.Elapsed.ToString());
            }
            return(Worked);
        }
        private List <QuoteRec> GetRecordsFromDatabase(  )
        {
            List <QuoteRec> results             = new List <QuoteRec>();
            CloudTable      tableReference      = QuoteDB.GetQuotesTableReference();
            TableQuery <QuoteTableEntity> query = new TableQuery <QuoteTableEntity>();

            foreach (QuoteTableEntity qte in tableReference.ExecuteQuery(query))
            {
                QuoteRec qr = qte.ExtractQuoteRecord();
                qr.Reference = qr.Reference ?? "";
                qr.Email     = qr.Email ?? "";
                results.Add(qr);
            }
            return(results);
        }
Example #11
0
        private void LoadFullQuotesFromDatabase()
        {
            try
            {
                List <QuoteRec> quoteRecs = new List <QuoteRec> {
                    new QuoteRec {
                        Author = "DB", Quote = "loading data "
                    }
                };
                Application["QuoteDBDataCache"] = quoteRecs;

                var qtr = QuoteDB.GetQuotesTableReference();

                // Construct the query operation for all entities
                TableQuery <QuoteTableEntity> query = new TableQuery <QuoteTableEntity>();
                // note: WIP: might need to do this , like this: TableQuery<QuoteTableEntity> query = new TableQuery<QuoteTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Smith"));

                var results = qtr.ExecuteQuery(query);
                List <QuoteTableEntity> quoteTableEntitiess = results.ToList();
                quoteRecs = new List <QuoteRec>();
                foreach (QuoteTableEntity qte in quoteTableEntitiess)
                {
                    QuoteRec qr = qte.ExtractQuoteRecord();
                    quoteRecs.Add(qr);
                }
                Application["QuoteDBDataCache"] = quoteRecs;
            }
            catch (Microsoft.WindowsAzure.Storage.StorageException ex)
            {
                new LogException(ex);
                List <QuoteRec> quoteRecs = new List <QuoteRec> {
                    new QuoteRec {
                        Author = "DB", Quote = "Database error loading data"
                    }
                };
                Application["QuoteDBDataCache"] = quoteRecs;
            }
            catch (Exception ex)
            {
                new LogException(ex, true);
            }
            finally
            {
            }
        }
        public HttpResponseMessage Delete()
        {
            var code = HttpStatusCode.InternalServerError;
            var rsp  = "";

            try
            {
                CloudTable tableReference = QuoteDB.GetQuotesTableReference();
                tableReference.DeleteIfExists();
                code = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                new LogException(ex);
                rsp = ex.ToString();
            }
            finally
            {
            }
            return(Request.CreateResponse(code, rsp));
        }
        private List <QuoteRec> GetRecordsFromDatabase(string filter)
        {
            List <QuoteRec> results = new List <QuoteRec>();

            CloudTable tableReference = QuoteDB.GetQuotesTableReference();

            // WIP this is poor performance cause it pulls all data from Table Space before it checks for contents
            // WIP this is poor performance cause it pulls all data from Table Space before it checks for contents
            // WIP this is poor performance cause it pulls all data from Table Space before it checks for contents
            TableQuery <QuoteTableEntity> query = new TableQuery <QuoteTableEntity>();

            filter = filter.ToLower();
            foreach (QuoteTableEntity qte in tableReference.ExecuteQuery(query))
            {
                if (qte.ActualQuoteRec.ToLower().Contains(filter))
                {
                    QuoteRec qr = qte.ExtractQuoteRecord();
                    results.Add(qr);
                }
            }

            // WIP next attempt, review how to get context and reference table
            //   http://msdn.microsoft.com/en-us/library/azure/dd894039.aspx
            //


            //// WIP: this returns 501 Not Supported from Azure
            //TableQuery<QuoteTableEntity> tq = tableReference.CreateQuery<QuoteTableEntity>();
            //var selected = tq.Where( d=>d.ActualQuoteRec.Contains(filter));
            //foreach (var qtr in selected)
            //{
            //    results.Add( (QuoteRec)qtr.ExtractQuoteRecord());
            //}

            return(results);
        }
Example #14
0
 //
 // GET: /Quotes/
 public ActionResult Index()
 {
     QuoteDB qdb = new QuoteDB();
     ViewData.Model = qdb.GetQuotes().Reverse();
     return View();
 }
Example #15
0
 public ActionResult Browse()
 {
     QuoteDB qdb = new QuoteDB();
     ViewData.Model = qdb.GetQuotes();
     return View();
 }