コード例 #1
0
        /// <summary>
        ///     Processes a single project task.
        /// </summary>
        /// <remarks>
        ///     First this syncs the task with the data store, then
        ///     the task is executed, then the success or failure is
        ///     synced with the data store.
        /// </remarks>
        /// <param name="task">The project task.</param>
        /// <param name="token">The cancellation token.</param>
        public async Task ProcessProjectTaskAsync(ProjectTask task, CancellationToken token)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            // TODO This should be transactional --> scope?
            var taskCreatedId = await _projectTaskRepository.CreateAsync(task, token);

            try
            {
                await(task.ProjectTaskType switch
                {
                    ProjectTaskType.UploadAudio => _uploadAudioExecuter.ExecuteUploadAudioAsync(task, token),
                    ProjectTaskType.BackupFull => throw new NotImplementedException(),
                    _ => throw new InvalidOperationException(nameof(task.ProjectTaskType)),
                });

                await _projectTaskRepository.MarkStatusAsync(taskCreatedId, ProjectTaskStatus.Done, token);

                _logger.LogTrace($"Executed project task {task.Id} of type {task.ProjectTaskType}");
            }
コード例 #2
0
        public async Task <ActionResult <ProjectTaskViewModel> > CreateItem([FromForm] ProjectTaskViewModel item)
        {
            try
            {
                if (item.ProjectTaskId > 0)
                {
                    return(BadRequest("Идентификатор записи должен быть равен 0"));
                }

                var project = await ProjectByIdAsync(item.ProjectId);

                if (project == null)
                {
                    return(BadRequest("Выбранный проект не найден"));
                }

                var group = await GroupByIdAsync(item.GroupId);

                if (group == null)
                {
                    return(BadRequest("Выбранная группа не найдена"));
                }

                var taskType = await TaskTypeByIdAsync(item.TaskTypeId);

                if (taskType == null)
                {
                    return(BadRequest("Выбранный тип задачи не найден"));
                }

                var taskStatus = await ProjectTaskStatusByIdAsync(item.ProjectTaskStatusId);

                if (taskStatus == null)
                {
                    return(BadRequest("Выбранный статус не найден"));
                }

                var block = await BlockByIdAsync(item.BlockId);

                var newItem = _mapper.Map <ProjectTask>(item);

                newItem.Project    = project;
                newItem.Group      = group;
                newItem.TaskStatus = taskStatus;
                newItem.TaskType   = taskType;

                if (block != null)
                {
                    newItem.Block = block;
                }

                newItem.DateCreated = DateTime.Now;

                var userCreated = (await _userRepository.FindAsync(e => !e.Deleted && e.Login.ToLower().Equals(_userService.userLogin.ToLower()))).SingleOrDefault();
                newItem.LoginCreated = userCreated != null ? userCreated.ShortName : _userService.userLogin;

                newItem.ProjectTaskPerformers = new List <ProjectTaskPerformer>();

                if (!string.IsNullOrEmpty(item.Users))
                {
                    var userIds = item.Users.Split(",").Select(e => int.Parse(e)).ToList();
                    foreach (int userId in userIds)
                    {
                        var user = await _userRepository.FindByIdAsync(userId);

                        newItem.ProjectTaskPerformers.Add(new ProjectTaskPerformer()
                        {
                            User = user
                        });
                    }
                }

                await _projectTaskRepository.CreateAsync(newItem);

                // формируем почтовые уведомления
                await _mailRepository.CreateMailsForNewPerformersAsync(newItem.ProjectTaskPerformers.Select(e => e.User).ToList(), newItem);

                return(_mapper.Map <ProjectTaskViewModel>(newItem));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }