Skip to content

Commit

Permalink
Refactor duplication endpoint (#107)
Browse files Browse the repository at this point in the history
* Refactor duplication endpoint
* code refactoring
* fix sonar
---------
Signed-off-by: TOURI ANIS <[email protected]>
Co-authored-by: Sylvain Bouzols <[email protected]>
  • Loading branch information
anistouri authored May 2, 2024
1 parent 2042a19 commit e293e90
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/main/java/org/gridsuite/filter/server/FilterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public ResponseEntity<AbstractFilter> createFilter(@RequestParam("id") UUID filt
.body(service.createFilter(filter));
}

@PostMapping(value = "/filters")
@Operation(summary = "Create a filter from an existing filter")
@PostMapping(value = "/filters", params = "duplicateFrom")
@Operation(summary = "Duplicate a filter")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The filter has been successfully created"),
@ApiResponse(responseCode = "404", description = "Source filter not found")})
public ResponseEntity<AbstractFilter> createFilter(@RequestParam("duplicateFrom") UUID sourceFilterId, @RequestParam("id") UUID filterId) {
return service.createFilter(sourceFilterId, filterId).map(filter -> ResponseEntity.ok()
public ResponseEntity<UUID> duplicateFilter(@RequestParam("duplicateFrom") UUID filterId) {
return service.duplicateFilter(filterId).map(newFilterId -> ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(filter))
.body(newFilterId))
.orElse(ResponseEntity.notFound().build());
}

Expand Down
19 changes: 8 additions & 11 deletions src/main/java/org/gridsuite/filter/server/FilterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,11 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

import static org.gridsuite.filter.server.repositories.proxies.AbstractFilterRepositoryProxy.WRONG_FILTER_TYPE;
Expand Down Expand Up @@ -164,18 +159,20 @@ public List<AbstractFilter> getFilters(List<UUID> ids) {
.collect(Collectors.toList());
}

@Transactional
@Transactional(propagation = Propagation.REQUIRED)
public <F extends AbstractFilter> AbstractFilter createFilter(F filter) {
return getRepository(filter).insert(filter);
}

@Transactional
public Optional<AbstractFilter> createFilter(UUID sourceFilterId, UUID filterId) {
public Optional<UUID> duplicateFilter(UUID sourceFilterId) {
Optional<AbstractFilter> sourceFilterOptional = getFilter(sourceFilterId);
if (sourceFilterOptional.isPresent()) {
UUID newFilterId = UUID.randomUUID();
AbstractFilter sourceFilter = sourceFilterOptional.get();
sourceFilter.setId(filterId);
return Optional.of(createFilter(sourceFilter));
sourceFilter.setId(newFilterId);
createFilter(sourceFilter);
return Optional.of(newFilterId);
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public void testDuplicateFilter() throws Exception {
lineFilter
);
insertFilter(filterId1, lineCriteriaFilter);
mvc.perform(post("/" + FilterApi.API_VERSION + "/filters?duplicateFrom=" + filterId1 + "&id=" + UUID.randomUUID())).andExpect(status().isOk());
mvc.perform(post("/" + FilterApi.API_VERSION + "/filters?duplicateFrom=" + filterId1)).andExpect(status().isOk());
checkFormFilter(filterId1, lineCriteriaFilter);
}

Expand Down

0 comments on commit e293e90

Please sign in to comment.