diff --git a/src/main/java/com/olympus/apollo/controllers/FeApi/ApplicationController.java b/src/main/java/com/olympus/apollo/controllers/FeApi/ApplicationController.java new file mode 100644 index 0000000..97a8b30 --- /dev/null +++ b/src/main/java/com/olympus/apollo/controllers/FeApi/ApplicationController.java @@ -0,0 +1,45 @@ +package com.olympus.apollo.controllers.FeApi; + +import com.olympus.apollo.services.ApplicationService; +import com.olympus.dto.ApplicationDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/fe-api/applications") +public class ApplicationController { + + private static final Logger logger = LoggerFactory.getLogger(ApplicationController.class); + + @Autowired + private ApplicationService applicationService; + + @GetMapping("") + public ResponseEntity> getAllApplications() { + try { + List applications = applicationService.getAllApplications(); + return ResponseEntity.ok(applications); + } catch (Exception e) { + logger.error("Error fetching applications", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @GetMapping("/{id}") + public ResponseEntity getApplicationById(@PathVariable String id) { + try { + return applicationService.getApplicationById(id) + .map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build()); + } catch (Exception e) { + logger.error("Error fetching application by id: {}", id, e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } +} diff --git a/src/main/java/com/olympus/apollo/controllers/FeApi/RepositoryController.java b/src/main/java/com/olympus/apollo/controllers/FeApi/RepositoryController.java new file mode 100644 index 0000000..6920f4e --- /dev/null +++ b/src/main/java/com/olympus/apollo/controllers/FeApi/RepositoryController.java @@ -0,0 +1,111 @@ +package com.olympus.apollo.controllers.FeApi; + +import com.olympus.apollo.services.RepositoryManagementService; +import com.olympus.dto.RepositoryCreateDTO; +import com.olympus.dto.RepositoryResponseDTO; +import com.olympus.dto.RepositoryUpdateDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController("feApiRepositoryController") +@RequestMapping("/fe-api/repositories") +public class RepositoryController { + + private static final Logger logger = LoggerFactory.getLogger(RepositoryController.class); + + @Autowired + private RepositoryManagementService repositoryService; + + @GetMapping("") + public ResponseEntity> getAllRepositories() { + try { + List repositories = repositoryService.getAllRepositories(); + return ResponseEntity.ok(repositories); + } catch (Exception e) { + logger.error("Error fetching repositories", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @GetMapping("/{id}") + public ResponseEntity getRepositoryById(@PathVariable String id) { + try { + return repositoryService.getRepositoryById(id) + .map(ResponseEntity::ok) + .orElse(ResponseEntity.notFound().build()); + } catch (Exception e) { + logger.error("Error fetching repository by id: {}", id, e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @GetMapping("/by-application/{applicationId}") + public ResponseEntity> getRepositoriesByApplication(@PathVariable String applicationId) { + try { + List repositories = repositoryService.getRepositoriesByApplicationId(applicationId); + return ResponseEntity.ok(repositories); + } catch (Exception e) { + logger.error("Error fetching repositories by application: {}", applicationId, e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @GetMapping("/by-project/{projectName}") + public ResponseEntity> getRepositoriesByProject(@PathVariable String projectName) { + try { + List repositories = repositoryService.getRepositoriesByProjectName(projectName); + return ResponseEntity.ok(repositories); + } catch (Exception e) { + logger.error("Error fetching repositories by project: {}", projectName, e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @PostMapping("") + public ResponseEntity createRepository(@RequestBody RepositoryCreateDTO createDTO) { + try { + RepositoryResponseDTO created = repositoryService.createRepository(createDTO); + return ResponseEntity.status(HttpStatus.CREATED).body(created); + } catch (IllegalArgumentException e) { + logger.warn("Invalid repository creation request: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (Exception e) { + logger.error("Error creating repository", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @PutMapping("/{id}") + public ResponseEntity updateRepository(@PathVariable String id, @RequestBody RepositoryUpdateDTO updateDTO) { + try { + RepositoryResponseDTO updated = repositoryService.updateRepository(id, updateDTO); + return ResponseEntity.ok(updated); + } catch (IllegalArgumentException e) { + logger.warn("Invalid repository update request: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (Exception e) { + logger.error("Error updating repository: {}", id, e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteRepository(@PathVariable String id) { + try { + repositoryService.deleteRepository(id); + return ResponseEntity.ok().build(); + } catch (IllegalArgumentException e) { + logger.warn("Invalid repository deletion request: {}", e.getMessage()); + return ResponseEntity.badRequest().body(e.getMessage()); + } catch (Exception e) { + logger.error("Error deleting repository: {}", id, e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } +} diff --git a/src/main/java/com/olympus/apollo/repository/ApplicationRepository.java b/src/main/java/com/olympus/apollo/repository/ApplicationRepository.java new file mode 100644 index 0000000..e4d0309 --- /dev/null +++ b/src/main/java/com/olympus/apollo/repository/ApplicationRepository.java @@ -0,0 +1,15 @@ +package com.olympus.apollo.repository; + +import com.olympus.model.Application; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ApplicationRepository extends MongoRepository { + + @Query("{ 'internal_name' : ?0 }") + List findByInternalName(String internalName); +} diff --git a/src/main/java/com/olympus/apollo/repository/KsGitInfoOpensearchRepository.java b/src/main/java/com/olympus/apollo/repository/KsGitInfoOpensearchRepository.java new file mode 100644 index 0000000..e1df5f4 --- /dev/null +++ b/src/main/java/com/olympus/apollo/repository/KsGitInfoOpensearchRepository.java @@ -0,0 +1,24 @@ +package com.olympus.apollo.repository; + +import com.olympus.model.apollo.KsGitInfoOpensearch; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface KsGitInfoOpensearchRepository extends MongoRepository { + + List findByApplicationId(String applicationId); + + List findByProjectName(String projectName); + + List findByIngestionStatus(String ingestionStatus); + + @Query("{ 'applicationId': ?0, 'projectName': ?1 }") + List findByApplicationIdAndProjectName(String applicationId, String projectName); + + Optional findByGitUrlAndBranch(String gitUrl, String branch); +} diff --git a/src/main/java/com/olympus/apollo/services/ApplicationService.java b/src/main/java/com/olympus/apollo/services/ApplicationService.java new file mode 100644 index 0000000..57c4f29 --- /dev/null +++ b/src/main/java/com/olympus/apollo/services/ApplicationService.java @@ -0,0 +1,41 @@ +package com.olympus.apollo.services; + +import com.olympus.apollo.repository.ApplicationRepository; +import com.olympus.dto.ApplicationDTO; +import com.olympus.model.Application; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +public class ApplicationService { + + private static final Logger logger = LoggerFactory.getLogger(ApplicationService.class); + + @Autowired + private ApplicationRepository applicationRepository; + + public List getAllApplications() { + return applicationRepository.findAll().stream() + .map(this::convertToDTO) + .collect(Collectors.toList()); + } + + public Optional getApplicationById(String id) { + return applicationRepository.findById(id).map(this::convertToDTO); + } + + private ApplicationDTO convertToDTO(Application entity) { + ApplicationDTO dto = new ApplicationDTO(); + dto.setId(entity.getId()); + dto.setInternalName(entity.getInternal_name()); + dto.setFeName(entity.getFE_name()); + dto.setDescription(entity.getDescription()); + return dto; + } +} diff --git a/src/main/java/com/olympus/apollo/services/RepositoryManagementService.java b/src/main/java/com/olympus/apollo/services/RepositoryManagementService.java new file mode 100644 index 0000000..8264abf --- /dev/null +++ b/src/main/java/com/olympus/apollo/services/RepositoryManagementService.java @@ -0,0 +1,169 @@ +package com.olympus.apollo.services; + +import com.olympus.apollo.repository.KsGitInfoOpensearchRepository; +import com.olympus.dto.RepositoryCreateDTO; +import com.olympus.dto.RepositoryResponseDTO; +import com.olympus.dto.RepositoryUpdateDTO; +import com.olympus.model.apollo.IngestionDetail; +import com.olympus.model.apollo.IngestionResults; +import com.olympus.model.apollo.KsGitInfoOpensearch; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +public class RepositoryManagementService { + + private static final Logger logger = LoggerFactory.getLogger(RepositoryManagementService.class); + + @Autowired + private KsGitInfoOpensearchRepository repository; + + public List getAllRepositories() { + return repository.findAll().stream() + .map(this::convertToDTO) + .collect(Collectors.toList()); + } + + public List getRepositoriesByApplicationId(String applicationId) { + return repository.findByApplicationId(applicationId).stream() + .map(this::convertToDTO) + .collect(Collectors.toList()); + } + + public List getRepositoriesByProjectName(String projectName) { + return repository.findByProjectName(projectName).stream() + .map(this::convertToDTO) + .collect(Collectors.toList()); + } + + public Optional getRepositoryById(String id) { + return repository.findById(id).map(this::convertToDTO); + } + + public RepositoryResponseDTO createRepository(RepositoryCreateDTO createDTO) { + // Check if repository already exists + Optional existing = repository.findByGitUrlAndBranch( + createDTO.getGitUrl(), createDTO.getBranch()); + + if (existing.isPresent()) { + throw new IllegalArgumentException( + "Repository already exists with URL: " + createDTO.getGitUrl() + " and branch: " + createDTO.getBranch()); + } + + KsGitInfoOpensearch entity = new KsGitInfoOpensearch(); + entity.setProjectName(createDTO.getProjectName()); + entity.setApplicationId(createDTO.getApplicationId()); + entity.setApplicationName(createDTO.getApplicationName()); + entity.setBranch(createDTO.getBranch()); + entity.setGitUrl(createDTO.getGitUrl()); + + // Set Git credentials if provided + if (createDTO.getGitCredentials() != null) { + KsGitInfoOpensearch.GitCredentials credentials = new KsGitInfoOpensearch.GitCredentials(); + credentials.setUsername(createDTO.getGitCredentials().getUsername()); + credentials.setPassword(createDTO.getGitCredentials().getPassword()); + entity.setGitCredentials(credentials); + } + + entity.setIngestionStatus("RENAME TO_BE_STARTED"); + entity.setCloneStatus("PENDING"); + entity.setFirstIngestionDate(new Date()); + entity.setStartedAt(new Date()); + entity.setIngestionLastUpdated(new Date()); + entity.setErrors(new ArrayList<>()); + entity.setIngestionDetails(new ArrayList<>()); + + // Add initial ingestion detail + IngestionDetail initialDetail = new IngestionDetail(); + initialDetail.setTimestamp(new Date()); + initialDetail.setPhase("CREATED"); + initialDetail.setMessage("Repository record created, waiting to be started"); + initialDetail.setFilesProcessed(null); + initialDetail.setFilesTotal(null); + initialDetail.setErrorDetails(null); + entity.getIngestionDetails().add(initialDetail); + + // Initialize results + IngestionResults results = new IngestionResults(); + results.setClassesFound(0); + results.setMethodsFound(0); + results.setIndexedDocuments(0); + results.setTotalFiles(0); + results.setProcessedFiles(0); + results.setFailedFiles(0); + entity.setResults(results); + + KsGitInfoOpensearch saved = repository.save(entity); + logger.info("Created new repository: {} with status TO_BE_STARTED", saved.getId()); + + return convertToDTO(saved); + } + + public RepositoryResponseDTO updateRepository(String id, RepositoryUpdateDTO updateDTO) { + KsGitInfoOpensearch entity = repository.findById(id) + .orElseThrow(() -> new IllegalArgumentException("Repository not found with id: " + id)); + if (updateDTO.getApplicationName() != null) { + entity.setApplicationName(updateDTO.getApplicationName()); + } + if (updateDTO.getGitUrl() != null) { + entity.setGitUrl(updateDTO.getGitUrl()); + } + if (updateDTO.getBranch() != null) { + entity.setBranch(updateDTO.getBranch()); + } + if (updateDTO.getProjectName() != null) { + entity.setProjectName(updateDTO.getProjectName()); + } + if (updateDTO.getIngestionStatus() != null) { + entity.setIngestionStatus(updateDTO.getIngestionStatus()); + } + if (updateDTO.getCloneStatus() != null) { + entity.setCloneStatus(updateDTO.getCloneStatus()); + } + + entity.setIngestionLastUpdated(new Date()); + + KsGitInfoOpensearch updated = repository.save(entity); + logger.info("Updated repository: {}", id); + + return convertToDTO(updated); + } + + public void deleteRepository(String id) { + if (!repository.existsById(id)) { + throw new IllegalArgumentException("Repository not found with id: " + id); + } + repository.deleteById(id); + logger.info("Deleted repository: {}", id); + } + + private RepositoryResponseDTO convertToDTO(KsGitInfoOpensearch entity) { + RepositoryResponseDTO dto = new RepositoryResponseDTO(); + dto.setId(entity.getId()); + dto.setApplicationName(entity.getApplicationName()); + dto.setGitUrl(entity.getGitUrl()); + dto.setBranch(entity.getBranch()); + dto.setIngestionStatus(entity.getIngestionStatus()); + dto.setProjectName(entity.getProjectName()); + dto.setIngestionLastUpdated(entity.getIngestionLastUpdated()); + dto.setStartedAt(entity.getStartedAt()); + dto.setIngestionDetails(entity.getIngestionDetails()); + dto.setCloneStatus(entity.getCloneStatus()); + dto.setCompletedAt(entity.getCompletedAt()); + dto.setErrors(entity.getErrors()); + dto.setClonePath(entity.getClonePath()); + dto.setCommitSha(entity.getCommitSha()); + dto.setIngestionProcessId(entity.getIngestionProcessId()); + dto.setFirstIngestionDate(entity.getFirstIngestionDate()); + dto.setResults(entity.getResults()); + return dto; + } +}