Skip to content

Commit

Permalink
#14 Changing implementation to optional
Browse files Browse the repository at this point in the history
  • Loading branch information
josdem committed Oct 22, 2023
1 parent ebcc2fe commit 0c3ad40
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.validation.Valid;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -44,12 +45,12 @@ public Mono<Product> getStatus(@PathVariable String key) {
@PutMapping("/{key}/{status}")
public Mono<Product> updateStatus(@PathVariable String key, @PathVariable String status) {
log.info("Updating status with key: {} and status: {}", key, status);
if (!memory.containsKey(key)) {
Optional<Product> optional = Optional.ofNullable(memory.get(key));
if (optional.isEmpty()) {
throw new NoSuchElementException("Key not found");
}
Product product = memory.get(key);
product.setStatus(Status.valueOf(status));
return Mono.just(product);
optional.get().setStatus(Status.valueOf(status));
return Mono.just(optional.get());
}

@ExceptionHandler(NoSuchElementException.class)
Expand Down

0 comments on commit 0c3ad40

Please sign in to comment.