Esempio n. 1
0
        public async Task <IHttpActionResult> GetDayHistory(int offset, int limit, string day)
        {
            object result = null;

            if (this.IsAuthenticated())
            {
                var userId = this.GetUserId();

                // RowKeys are inverse to time value.
                var dayInvertedKey = KeyUtils.LocalTimeToInvertedKey(day);
                if (String.IsNullOrEmpty(dayInvertedKey))
                {
                    throw new ArgumentOutOfRangeException(day);
                }
                // Include the entire day.
                var dayRowKey  = dayInvertedKey.Substring(0, 6);
                var rowKeyFrom = dayRowKey + "000000";
                var rowKeyTo   = dayRowKey + "235959";

                var    filterRowFrom     = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, rowKeyFrom);
                var    filterRowTo       = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual, rowKeyTo);
                string combinedRowFilter = TableQuery.CombineFilters(filterRowFrom, TableOperators.And, filterRowTo);

                result = await GetHistoryResources(userId, combinedRowFilter, offset, limit);
            }

            return(Ok(result));
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> PostResourceView([FromUri] int id, [FromBody] JObject value)
        {
            // A suggestion may have resourceId == null(0?) when the resource is made on the client in code and points to Google

            // TODO. Idea: Get the unauthenicated user's session cookie and log views for the session in a separate datastore.
            var userId = this.GetUserId(); // GetUserId() returns 0 for an unauthenticated user. That's fine. We log every view.

            var logEnity = new DynamicTableEntity(KeyUtils.GetCurrentTimeKey(), KeyUtils.IntToKey(userId));

            logEnity["Json"] = new EntityProperty(value.ToString());
            var logTask = AzureStorageUtils.InsertEntityAsync(AzureStorageUtils.TableNames.LibraryLog, logEnity);

            if (userId != 0 && id != 0)
            {
                var viewTask = DapperHelper.ExecuteResilientlyAsync("dbo.libPostResourceView",
                                                                    new
                {
                    UserId     = userId,
                    ResourceId = id,
                },
                                                                    CommandType.StoredProcedure);

                // We use KeyUtils.LocalTimeToInvertedKey() to keep the local time and to order last records first for retrieval.
                var localTime = (string)value["localTime"];
                var rowKey    = KeyUtils.LocalTimeToInvertedKey(localTime);

                if (String.IsNullOrEmpty(rowKey))
                {
                    throw new ArgumentOutOfRangeException(localTime);
                }

                var historyEntity = new LibraryHistoryEntity
                {
                    PartitionKey = KeyUtils.IntToKey(userId),
                    RowKey       = rowKey,
                    ResourceId   = id,
                };
                var historyTask = AzureStorageUtils.InsertEntityAsync(AzureStorageUtils.TableNames.LibraryHistory, historyEntity);

                // Do all the tasks in parallel.
                await Task.WhenAll(viewTask, historyTask, logTask);
            }
            else
            {
                await logTask;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }