diff --git a/nekontakte/src/main/java/com/example/nekontakte/NekontakteApplication.java b/nekontakte/src/main/java/com/example/nekontakte/NekontakteApplication.java index eda2708..0624bc5 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/NekontakteApplication.java +++ b/nekontakte/src/main/java/com/example/nekontakte/NekontakteApplication.java @@ -2,10 +2,11 @@ package com.example.nekontakte; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.RestController; @SpringBootApplication +@RestController public class NekontakteApplication { - public static void main(String[] args) { SpringApplication.run(NekontakteApplication.class, args); } diff --git a/nekontakte/src/main/java/com/example/nekontakte/UserApiEndpoint.java b/nekontakte/src/main/java/com/example/nekontakte/UserApiEndpoint.java new file mode 100644 index 0000000..a266eb0 --- /dev/null +++ b/nekontakte/src/main/java/com/example/nekontakte/UserApiEndpoint.java @@ -0,0 +1,64 @@ +package com.example.nekontakte; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import java.util.HashMap; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.PathVariable; + +@RestController +@RequestMapping("/user") +public class UserApiEndpoint { + private Integer lastUserId = 0; + private HashMap users = new HashMap<>(); + + @GetMapping() + public List getUsers() { + return users.values().stream().toList(); + } + + @GetMapping("/{id}") + public UserDTO getUser(@PathVariable Integer id) { + if (users.containsKey(id)) { + return users.get(id); + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + + @PostMapping() + public UserDTO postUser(@RequestBody UserDTO entity) { + Integer currentUserId = lastUserId + 1; + entity.setId(currentUserId); + users.put(currentUserId, entity); + lastUserId++; + return entity; + } + + @PutMapping("/{id}") + public UserDTO putUser(@PathVariable Integer id, @RequestBody UserDTO entity) { + if (users.containsKey(id)) { + entity.setId(id); + users.put(id, entity); + return entity; + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + + @DeleteMapping("/{id}") + public UserDTO deleteUser(@PathVariable Integer id) { + if (users.containsKey(id)) { + return users.remove(id); + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + +} diff --git a/nekontakte/src/main/java/com/example/nekontakte/WebConfig.java b/nekontakte/src/main/java/com/example/nekontakte/WebConfig.java index c53a7c1..1371923 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/WebConfig.java +++ b/nekontakte/src/main/java/com/example/nekontakte/WebConfig.java @@ -2,7 +2,10 @@ package com.example.nekontakte; import org.springframework.context.annotation.Configuration; import org.springframework.lang.NonNull; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.method.HandlerTypePredicate; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @@ -13,4 +16,9 @@ public class WebConfig implements WebMvcConfigurer { .allowedMethods("GET", "POST", "PUT", "DELETE"); } + + @Override + public void configurePathMatch(@SuppressWarnings("null") PathMatchConfigurer configurer) { + configurer.addPathPrefix("api", HandlerTypePredicate.forAnnotation(RestController.class)); + } }