From 3269c0b399f3327e205ac1099d1add1872c0def5 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Fri, 17 Jun 2022 20:57:21 +0300 Subject: [PATCH] PersonService --- .../controlleradvice/service/PersonService.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/dev/struchkov/example/controlleradvice/service/PersonService.java b/src/main/java/dev/struchkov/example/controlleradvice/service/PersonService.java index f669311..2aceb6e 100644 --- a/src/main/java/dev/struchkov/example/controlleradvice/service/PersonService.java +++ b/src/main/java/dev/struchkov/example/controlleradvice/service/PersonService.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.UUID; @Service @@ -23,14 +24,11 @@ public class PersonService { return people.values().stream() .filter(person -> person.getLogin().equals(login)) .findFirst() - .orElseThrow(); + .orElseThrow(() -> new NotFoundException("Пользователь не найден")); } - public Person getByIdOrThrown(@NonNull UUID id) { - if (!people.containsKey(id)) { - throw new NotFoundException("Пользователь не найден"); - } - return people.get(id); + public Optional getById(@NonNull UUID id) { + return Optional.ofNullable(people.get(id)); } }