Пример #1
0
        public async Task <ProjectCreateResponse> CreateProject(ProjectCreateRequest request)
        {
            var response = new ProjectCreateResponse();

            var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId);

            if (!currentUser.IsAdmin)
            {
                response.SetInvalid();
                return(response);
            }

            if (await _organizationRepository.Any(x => x.Id == currentUser.OrganizationId && !x.IsActive))
            {
                response.SetInvalidBecauseParentNotActive();
                return(response);
            }

            if (await _projectRepository.Any(x => x.Name == request.ProjectName &&
                                             x.OrganizationId == currentUser.OrganizationId))
            {
                response.ErrorMessages.Add("project_name_must_be_unique");
                response.Status = ResponseStatus.Failed;
                return(response);
            }

            var entity    = _projectFactory.CreateEntityFromRequest(request, currentUser.Organization);
            var uowResult = await _projectUnitOfWork.DoCreateWork(request.CurrentUserId, entity);

            if (uowResult)
            {
                response.Item   = _projectFactory.CreateDtoFromEntity(entity);
                response.Status = ResponseStatus.Success;
                return(response);
            }

            response.SetFailed();
            return(response);
        }
Пример #2
0
        public async Task <LabelChangeActivationResponse> ChangeActivation(LabelChangeActivationRequest request)
        {
            var response = new LabelChangeActivationResponse();

            var label = await _labelRepository.Select(x => x.Uid == request.LabelUid);

            if (label.IsNotExist())
            {
                response.SetInvalid();
                response.ErrorMessages.Add("label_not_found");
                return(response);
            }

            var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId);

            if (label.OrganizationId != currentUser.OrganizationId)
            {
                response.SetInvalid();
                return(response);
            }

            if (await _organizationRepository.Any(x => x.Id == label.OrganizationId && !x.IsActive))
            {
                response.SetInvalid();
                response.ErrorMessages.Add("organization_not_found");
                return(response);
            }

            if (await _projectRepository.Any(x => x.Id == label.ProjectId && !x.IsActive))
            {
                response.SetInvalid();
                response.ErrorMessages.Add("project_not_found");
                return(response);
            }

            var updatedEntity = _labelFactory.UpdateEntityForChangeActivation(label);
            var result        = await _labelRepository.Update(request.CurrentUserId, updatedEntity);

            if (result)
            {
                response.Item   = _labelFactory.CreateDtoFromEntity(updatedEntity);
                response.Status = ResponseStatus.Success;
                return(response);
            }

            response.SetFailed();
            return(response);
        }
Пример #3
0
        public async Task <int> CreateAsync(CreateProjectRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (_projectRepository.Any(x => x.Name.Equals(request.Name)))
            {
                throw new ValidationException($"Error : {request.Name} already exits.");
            }

            var employeeIds = request.EmployeeIds;

            var project = new Project
            {
                Name = request.Name
            };

            if (employeeIds != null & employeeIds.Any())
            {
                employeeIds = employeeIds.Distinct().ToList();

                if (_employeeRepository.Count(x => employeeIds.Contains(x.Id)) != employeeIds.Count)
                {
                    throw new ValidationException("Error : Some employees do not exist.");
                }
            }

            project.ProjectEmployees = employeeIds.Select(x => new ProjectEmployee {
                EmployeeId = x
            }).ToList();

            _projectRepository.Add(project);

            return(await _unitOfWork.SaveChangesAsync());
        }
 private bool ProjectExists(int id)
 {
     return(_projectRepository.Any(e => e.ProjectId == id));
 }