mirror of
https://github.com/Example-uPagge/spring-controlleradvice.git
synced 2024-06-14 11:22:33 +03:00
Simple App
This commit is contained in:
parent
1f3e8b75b3
commit
327f7e3c6b
@ -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<Person> getByLogin(@RequestParam("login") String login) {
|
||||
return ResponseEntity.ok(personService.getByLoginOrThrown(login));
|
||||
}
|
||||
|
||||
@GetMapping("{id}")
|
||||
public ResponseEntity<Person> getById(@PathVariable("id") UUID id) {
|
||||
return ResponseEntity.ok(personService.getByIdOrThrown(id));
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package dev.struchkov.example.controlleradvice.exception;
|
||||
|
||||
public class AppException extends RuntimeException {
|
||||
|
||||
public AppException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package dev.struchkov.example.controlleradvice.exception;
|
||||
|
||||
public class NotFoundException extends AppException {
|
||||
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -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<UUID, Person> 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user