public async Task <IActionResult> AddLabel(Guid token, Guid projectUid, string labelKey) { var result = new CommonResult(); if (token.IsEmptyGuid() || projectUid.IsEmptyGuid() || labelKey.IsEmpty()) { result.Messages.Add("some parameters are missing! (token, projectUid, labelKey)"); return(Json(result)); } var request = new TokenValidateRequest(projectUid, token); var response = await _integrationService.ValidateToken(request); if (response.Status.IsNotSuccess) { result.Messages = response.ErrorMessages; result.Messages.Add("unauthorized"); return(StatusCode(401, result)); } var labelCreateWithTokenRequest = new LabelCreateWithTokenRequest(token, projectUid, labelKey); var labelsResponse = await _labelService.CreateLabel(labelCreateWithTokenRequest); if (labelsResponse.Status.IsNotSuccess) { response.ErrorMessages = labelsResponse.ErrorMessages; return(Json(result)); } result.IsOk = true; return(Json(result)); }
public async Task <LabelCreateResponse> CreateLabel(LabelCreateWithTokenRequest request) { var response = new LabelCreateResponse(); var project = await _projectRepository.Select(x => x.Uid == request.ProjectUid && x.IsActive); if (project.IsNotExist()) { response.SetInvalidBecauseNotFound("project"); return(response); } var now = DateTime.UtcNow; var token = await _tokenRepository.Select(x => x.AccessToken == request.Token && x.ExpiresAt > now); if (token.IsNotExist()) { response.SetInvalidBecauseNotFound("token"); return(response); } if (token.OrganizationId != project.OrganizationId) { response.SetInvalid(); return(response); } if (await _organizationRepository.Any(x => x.Id == project.OrganizationId && !x.IsActive)) { response.SetInvalidBecauseNotFound("organization"); 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.CreateEntity(request.LabelKey, project); var uowResult = await _labelUnitOfWork.DoCreateWork(token.CreatedBy, label); if (uowResult) { response.Item = _labelFactory.CreateDtoFromEntity(label); response.Status = ResponseStatus.Success; return(response); } response.SetFailed(); return(response); }
public static LabelCreateWithTokenRequest GetLabelCreateWithTokenRequest() { var request = new LabelCreateWithTokenRequest(UidOne, UidOne, StringOne); return(request); }