Example #1
0
        public async Task <User> GetUserAsync(string token)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }

            var jwt       = new JwtSecurityTokenHandler().ReadJwtToken(token);
            var claims    = jwt.Claims.ToList();
            var username  = claims.FirstOrDefault(x => x.Type == "unique_name")?.Value?.Split('#').Last();
            var firstName = claims.FirstOrDefault(x => x.Type == "given_name")?.Value;

            try
            {
                var table = await TableStorage.CreateTableAsync(_configuration, UserTableName);

                TableOperation retrieveOperation = TableOperation.Retrieve <UserEntity>(firstName, username);
                TableResult    result            = await table.ExecuteAsync(retrieveOperation);

                UserEntity user = result.Result as UserEntity;

                if (user == null)
                {
                    Log.Logger.Error("Cannot find user {firstName}, {username}.", firstName, username);
                }

                return(_mapper.Map <UserEntity, User>(user));
            }
            catch (StorageException e)
            {
                Log.Logger.Error(e, "Error when getting user from storage.");
                throw e;
            }
        }
Example #2
0
        public async Task <bool> RegisterTimeRecordsAsync(string token, TimeRecord[] timeRecords)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }

            if (timeRecords == null)
            {
                throw new ArgumentNullException("timeRecords");
            }

            var user = await _authService.GetUserAsync(token);

            if (user == null)
            {
                Log.Logger.Error("Cannot find user with provided token.");
                return(false);
            }

            var username = user.Username;

            try
            {
                var table = await TableStorage.CreateTableAsync(_configuration, TimeRecordTableName);

                TableBatchOperation batch = new TableBatchOperation();

                foreach (var timeRecord in timeRecords)
                {
                    timeRecord.RecordedBy = username;

                    if (!timeRecord.RecordedTime.HasValue)
                    {
                        timeRecord.RecordedTime = DateTime.UtcNow;
                    }

                    timeRecord.ModifiedTime = DateTime.UtcNow;
                    var entity = new TimeRecordEntity(timeRecord);
                    batch.InsertOrMerge(entity);
                }

                await table.ExecuteBatchAsync(batch);

                return(true);
            }
            catch (StorageException e)
            {
                Log.Logger.Error(e, "Error when insert/merge time record to storage.");
            }

            return(false);
        }
Example #3
0
        private async Task <bool> InsertUserAsync(User user)
        {
            try
            {
                user.Username = user.Email;
                var entity = new UserEntity(user);
                var table  = await TableStorage.CreateTableAsync(_configuration, UserTableName);

                TableOperation insert = TableOperation.Insert(entity);

                TableResult result = await table.ExecuteAsync(insert);

                return(result.HttpStatusCode == 200 || result.HttpStatusCode == 204);
            }
            catch (StorageException e)
            {
                Log.Logger.Error(e, "Error when register user to storage.");
                throw e;
            }
        }
Example #4
0
        private async Task <bool> InsertUserAsync(User user)
        {
            try
            {
                user.Username = string.Concat(user.Email.Split('@').First(), "@", _configuration.GetValue <string>("AzureAd:Domain"));

                var entity = new UserEntity(user);
                var table  = await TableStorage.CreateTableAsync(_configuration, UserTableName);

                TableOperation insert = TableOperation.Insert(entity);

                TableResult result = await table.ExecuteAsync(insert);

                return(result.HttpStatusCode == 200 || result.HttpStatusCode == 204);
            }
            catch (StorageException e)
            {
                Log.Logger.Error(e, "Error when register user to storage.");
                throw e;
            }
        }
Example #5
0
        public async Task <TimeRecord[]> GetTimeRecordsReportByUserAsync(string token, DateTime datetime)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }

            var now = DateTime.UtcNow;

            if (datetime < now.AddDays(-30))
            {
                throw new ArgumentException("Cannot get data later than 30 days.");
            }

            var user = await _authService.GetUserAsync(token);

            if (user == null)
            {
                return(new TimeRecord[] { });
            }

            var username = user.Username;
            // yyyy'-'MM'-'dd HH':'mm':'ss'Z'
            var startRowKey = DateTime.Parse($"{datetime.Year}-{datetime.Month.ToString("D2")}-{datetime.Day.ToString("D2")} 00:00:00")
                              .Ticks.ToString();
            var endRowKey = DateTime.Parse($"{datetime.Year}-{datetime.Month.ToString("D2")}-{datetime.Day.ToString("D2")} 23:59:59")
                            .Ticks.ToString();

            try
            {
                var table = await TableStorage.CreateTableAsync(_configuration, TimeRecordTableName);

                // Create the range query using the fluid API
                TableQuery <TimeRecordEntity> rangeQuery = new TableQuery <TimeRecordEntity>().Where(
                    TableQuery.CombineFilters(
                        TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, username),
                        TableOperators.And,
                        TableQuery.CombineFilters(
                            TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual,
                                                               startRowKey),
                            TableOperators.And,
                            TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual,
                                                               endRowKey))));

                var entities = await table.ExecuteQueryAsync(rangeQuery);

                var records = entities
                              .Select(g => new TimeRecord
                {
                    RecordedTime = g.RecordedTime,
                    ModifiedTime = g.ModifiedTime,
                    Description  = g.Description,
                    TotalMinutes = g.TotalMinutes
                }).ToArray();

                return(records);
            }
            catch (StorageException e)
            {
                Log.Logger.Error(e, "Error when query data from storage.");
            }

            return(new TimeRecord[] { });
        }
Example #6
0
        public async Task <DailyTimeByUser[]> GetMonthlyReportByUserAsync(string token, int year)
        {
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentNullException("token");
            }

            if (year < MinYear)
            {
                throw new ArgumentException("Cannot get data from year sooner than 2010.");
            }

            var user = await _authService.GetUserAsync(token);

            if (user == null)
            {
                return(new DailyTimeByUser[] { });
            }

            var username = user.Username;

            // yyyy'-'MM'-'dd HH':'mm':'ss'Z'
            var startRowKey = DateTime.Parse($"{year}-01-01 00:00:00").Ticks.ToString();
            var endRowKey   = DateTime.Parse($"{year}-12-31 23:59:59").Ticks.ToString();

            try
            {
                var table = await TableStorage.CreateTableAsync(_configuration, TimeRecordTableName);

                // Create the range query using the fluid API
                TableQuery <TimeRecordEntity> rangeQuery = new TableQuery <TimeRecordEntity>().Where(
                    TableQuery.CombineFilters(
                        TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, username),
                        TableOperators.And,
                        TableQuery.CombineFilters(
                            TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual,
                                                               startRowKey),
                            TableOperators.And,
                            TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual,
                                                               endRowKey))));

                var entities = await table.ExecuteQueryAsync(rangeQuery);

                var records = entities
                              .GroupBy(x => $"{x.RecordedTime?.Month}-{x.RecordedTime?.Day}")
                              .Select(g => new DailyTimeByUser
                {
                    Description  = string.Join(" | ", g.Select(x => x.Description + " " + FormatDuration(x.TotalMinutes))),
                    TotalMinutes = g.Sum(r => r.TotalMinutes),
                    DayInMonth   = g.First().RecordedTime.Value.Day,
                    MonthInYear  = g.First().RecordedTime.Value.Month
                }).OrderByDescending(x => x.DayInMonth).ToArray();

                return(records);
            }
            catch (StorageException e)
            {
                Log.Logger.Error(e, "Error when query data from storage.");
            }

            return(new DailyTimeByUser[] { });
        }