Пример #1
0
        public async Task <LabelCreateResponse> CreateLabel(LabelCreateRequest request)
        {
            var response = new LabelCreateResponse();

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

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

            var project = await _projectRepository.Select(x => x.Uid == request.ProjectUid);

            if (project.IsNotExist())
            {
                response.SetInvalidBecauseNotFound("project");
                return(response);
            }

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

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

            if (await _labelRepository.Any(x => x.Key == request.LabelKey && x.ProjectId == project.Id))
            {
                response.ErrorMessages.Add("label_key_must_be_unique");
                response.Status = ResponseStatus.Failed;
                return(response);
            }

            var label     = _labelFactory.CreateEntityFromRequest(request, project);
            var uowResult = await _labelUnitOfWork.DoCreateWork(request.CurrentUserId, label);

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

            response.SetFailed();
            return(response);
        }