コード例 #1
0
        public async Task DeleteOrganizationProjectServiceTemplate(Guid organizationId, Guid projectServiceTemplateId)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create project service templates in this organization.");

                return;
            }

            ProjectServiceTemplate existingTemplate = organization.GetProjectServiceTemplateById(projectServiceTemplateId);

            if (existingTemplate == null)
            {
                await _domainManagerService.AddNotFound($"The project service templated with id {projectServiceTemplateId} does not exists.");

                return;
            }

            var isBeingUsed = organization.IsTemplateBeingUsed(projectServiceTemplateId);

            if (isBeingUsed)
            {
                await _domainManagerService.AddConflict($"The template with id {projectServiceTemplateId} is being used by some services. You cannot delete it.");

                return;
            }

            //delete template
            existingTemplate.Delete(loggedUserId);

            //delete relationship
            foreach (var item in existingTemplate.Organizations)
            {
                item.Delete(loggedUserId);
            }

            _userRepository.Update(user);

            await _userRepository.SaveChanges();
        }