package io.reflectoring.coderadar.core.projectadministration.service.analyzerconfig;

import io.reflectoring.coderadar.core.projectadministration.domain.AnalyzerConfiguration;
import io.reflectoring.coderadar.core.projectadministration.port.driven.analyzerconfig.AddAnalyzerConfigurationPort;
import io.reflectoring.coderadar.core.projectadministration.port.driven.project.GetProjectPort;
import io.reflectoring.coderadar.core.projectadministration.port.driver.analyzerconfig.AddAnalyzerConfigurationCommand;
import io.reflectoring.coderadar.core.projectadministration.port.driver.analyzerconfig.AddAnalyzerConfigurationUseCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AddAnalyzerConfigurationService implements AddAnalyzerConfigurationUseCase {

  private final AddAnalyzerConfigurationPort addAnalyzerConfigurationPort;
  private final GetProjectPort getProjectPort;

  @Autowired
  public AddAnalyzerConfigurationService(
      AddAnalyzerConfigurationPort addAnalyzerConfigurationPort, GetProjectPort getProjectPort) {
    this.addAnalyzerConfigurationPort = addAnalyzerConfigurationPort;
    this.getProjectPort = getProjectPort;
  }

  @Override
  public Long add(AddAnalyzerConfigurationCommand command) {
    AnalyzerConfiguration analyzerConfiguration = new AnalyzerConfiguration();
    analyzerConfiguration.setEnabled(command.getEnabled());
    analyzerConfiguration.setAnalyzerName(command.getAnalyzerName());
    analyzerConfiguration.setProject(getProjectPort.get(command.getProjectId()));
    return addAnalyzerConfigurationPort.add(analyzerConfiguration);
  }
}

@@ -143,36 +143,189 @@
 ion.
-domain.AnalyzerConfiguration
+ProjectNotFound;%0Aimport io.reflectoring.coderadar.core.projectadministration.domain.AnalyzerConfiguration;%0Aimport io.reflectoring.coderadar.core.projectadministration.domain.Project
 ;%0Aim
@@ -531,24 +531,123 @@
 rojectPort;%0A
+import io.reflectoring.coderadar.core.projectadministration.port.driven.project.UpdateProjectPort;%0A
 import io.re
@@ -874,16 +874,43 @@
 seCase;%0A
+import java.util.Optional;%0A
 import o
@@ -1187,16 +1187,69 @@
 onPort;%0A
+  private final UpdateProjectPort updateProjectPort;%0A
   privat
@@ -1407,16 +1407,65 @@
 ionPort,
+%0A      UpdateProjectPort updateProjectPort,%0A     
  GetProj
@@ -1560,16 +1560,64 @@
 onPort;%0A
+    this.updateProjectPort = updateProjectPort;%0A
     this
@@ -1941,89 +1941,294 @@
 ));%0A
+%0A
     
-analyzerConfiguration.setProject(getProjectPort.get(command.getProjectId()));
+Optional%3CProject%3E project = getProjectPort.get(command.getProjectId());%0A%0A    if (project.isPresent()) %7B%0A      project.get().getAnalyzerConfigurations().add(analyzerConfiguration);%0A      updateProjectPort.update(project.get());%0A    %7D else %7B%0A      throw new ProjectNotFound();%0A    %7D%0A
 %0A   

Adapt the return value of the GetProjectPort