diff --git a/src/main/java/dev/struchkov/example/controlleradvice/controller/PersonController.java b/src/main/java/dev/struchkov/example/controlleradvice/controller/PersonController.java new file mode 100644 index 0000000..7789155 --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/controller/PersonController.java @@ -0,0 +1,34 @@ +package dev.struchkov.example.controlleradvice.controller; + +import dev.struchkov.example.controlleradvice.domain.Person; +import dev.struchkov.example.controlleradvice.service.PersonService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.UUID; + +@Slf4j +@RestController +@RequestMapping("api/person") +@RequiredArgsConstructor +public class PersonController { + + private final PersonService personService; + + @GetMapping + public ResponseEntity getByLogin(@RequestParam("login") String login) { + return ResponseEntity.ok(personService.getByLoginOrThrown(login)); + } + + @GetMapping("{id}") + public ResponseEntity getById(@PathVariable("id") UUID id) { + return ResponseEntity.ok(personService.getByIdOrThrown(id)); + } + +} diff --git a/src/main/java/dev/struchkov/example/controlleradvice/domain/ErrorMessage.java b/src/main/java/dev/struchkov/example/controlleradvice/domain/ErrorMessage.java new file mode 100644 index 0000000..06c6b66 --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/domain/ErrorMessage.java @@ -0,0 +1,14 @@ +package dev.struchkov.example.controlleradvice.domain; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class ErrorMessage { + + private String message; + +} diff --git a/src/main/java/dev/struchkov/example/controlleradvice/domain/Person.java b/src/main/java/dev/struchkov/example/controlleradvice/domain/Person.java new file mode 100644 index 0000000..f0c2cc2 --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/domain/Person.java @@ -0,0 +1,21 @@ +package dev.struchkov.example.controlleradvice.domain; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.UUID; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class Person { + + private UUID id; + private String login; + private String firstName; + private String password; + +} diff --git a/src/main/java/dev/struchkov/example/controlleradvice/exception/AppException.java b/src/main/java/dev/struchkov/example/controlleradvice/exception/AppException.java new file mode 100644 index 0000000..1dff067 --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/exception/AppException.java @@ -0,0 +1,9 @@ +package dev.struchkov.example.controlleradvice.exception; + +public class AppException extends RuntimeException { + + public AppException(String message) { + super(message); + } + +} diff --git a/src/main/java/dev/struchkov/example/controlleradvice/exception/NotFoundException.java b/src/main/java/dev/struchkov/example/controlleradvice/exception/NotFoundException.java new file mode 100644 index 0000000..caef6f5 --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/exception/NotFoundException.java @@ -0,0 +1,9 @@ +package dev.struchkov.example.controlleradvice.exception; + +public class NotFoundException extends AppException { + + public NotFoundException(String message) { + super(message); + } + +} diff --git a/src/main/java/dev/struchkov/example/controlleradvice/service/PersonService.java b/src/main/java/dev/struchkov/example/controlleradvice/service/PersonService.java new file mode 100644 index 0000000..575d458 --- /dev/null +++ b/src/main/java/dev/struchkov/example/controlleradvice/service/PersonService.java @@ -0,0 +1,36 @@ +package dev.struchkov.example.controlleradvice.service; + +import dev.struchkov.example.controlleradvice.domain.Person; +import dev.struchkov.example.controlleradvice.exception.NotFoundException; +import lombok.NonNull; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +@Service +public class PersonService { + + private final Map persons = new HashMap<>(); + + public PersonService() { + final UUID komarId = UUID.randomUUID(); + persons.put(komarId, new Person(komarId, "komar", "Алексей", "ertyuiop")); + } + + public Person getByLoginOrThrown(@NonNull String login) { + return persons.values().stream() + .filter(person -> person.getLogin().equals(login)) + .findFirst() + .orElseThrow(); + } + + public Person getByIdOrThrown(@NonNull UUID id) { + if (!persons.containsKey(id)) { + throw new NotFoundException("Пользователь не найден"); + } + return persons.get(id); + } + +}