Exemple #1
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);
        }
 public static TimeRecord ToTimeRecord(this TimeRecordEntity source)
 {
     return(new TimeRecord
     {
         Id = source.Id,
         EmployeeFullName = source.Employee.FullName,
         ProjectName = source.Issue.Project.Name,
         ProjectVersionName = source.Issue.ProjectVersion?.Name,
         ResourceNames = source.Resources.Select(i => i.Resource.Name).ToArray(),
         Activity = source.Activity,
         Comments = source.Comments,
         Date = source.Date,
         Hours = source.Hours,
         IssueId = source.Issue.ExternalId
     });
 }