Пример #1
0
        public async Task <ActionResult <ProjectAdo> > SaveDraftAdoProject(ProjectAdo dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                _logger.LogInformation(ApiLogEvents.RequestItem, $"{nameof(SaveDraftAdoProject)} Started");

                var repoObj = _mapper.Map <Entity.AdoProject>(dto);
                _repository.Add(repoObj);
                var suc = await _repository.SaveChangesAsync();

                if (suc)
                {
                    var result = _mapper.Map <ProjectAdo>(repoObj);
                    return(CreatedAtRoute(nameof(GetRequestAdoProject),
                                          new { id = result.Id }, result));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, nameof(SaveDraftAdoProject), dto);
                return(StatusCode(StatusCodes.Status500InternalServerError, ex?.Message));
            }
        }
        public async Task <List <AdoProject> > Handle(CreateApprovedAdoProjectIdsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                _adoProjectRepository.DatabaseContext.ChangeTracker.Clear();
                _adoProjectHistoryRepository.DatabaseContext.ChangeTracker.Clear();

                List <AdoProject> created  = new();
                StringBuilder     warnings = new();
                if (request.Approved.IsNullOrEmptyCollection())
                {
                    return(null);
                }
                AdoProject origEntity = null;
                foreach (var id in request.Approved)
                {
                    try
                    {
                        origEntity = null;
                        origEntity = await _adoProjectRepository.GetId(id);

                        //1. create Proj
                        var mapped       = _mapper.Map <AdoModels.ProjectAdo>(origEntity);
                        var createdModel = await _projectAdoServices.CreateProject(mapped);

                        //2. Update Db
                        var createdEntity = _mapper.Map <AdoProject>(createdModel);
                        createdEntity.Status = origEntity.Status;

                        if (createdModel.State == Microsoft.TeamFoundation.Core.WebApi.ProjectState.New ||
                            createdModel.State == Microsoft.TeamFoundation.Core.WebApi.ProjectState.WellFormed)
                        {
                            createdEntity.Status = Status.Completed;
                            created.Add(createdEntity);
                        }

                        _adoProjectRepository.Update(createdEntity, request.UserId);
                        if (await _adoProjectRepository.SaveChangesAsync() && createdEntity.Status == Status.Completed)
                        {
                            //3. create record in history DB
                            await _adoProjectHistoryRepository.Create(origEntity.Id, IAdoProjectHistoryRepository.Operation_Request_Completed, request.UserId);
                        }
                    }
                    catch (Exception ex)
                    {
                        warnings.Append($"Error approving Id: {origEntity?.Id}, {nameof(origEntity.Name)}: {origEntity?.Name}, {ex.Message}");
                    }
                }
                if (warnings.Length > 0)
                {
                    _logger.LogWarning($"{nameof(CreateApprovedAdoProjectIdsCommandHandler)} warnings to report: {warnings}");
                }

                return(created);
            }
            catch (Exception ex)
            {
                //this shouldn't stop the API from doing else so this can be logged
                _logger.LogError($"{nameof(CreateApprovedAdoProjectIdsCommandHandler)} failed due to: {ex.Message}");
            }
            return(null);
        }