diff --git a/demo/src/main/java/com/example/demo/ApiController.java b/demo/src/main/java/com/example/demo/ApiController.java index ee0c6a4..d816048 100644 --- a/demo/src/main/java/com/example/demo/ApiController.java +++ b/demo/src/main/java/com/example/demo/ApiController.java @@ -1,62 +1,53 @@ package com.example.demo; import java.util.Date; +import java.util.Map; import java.util.List; +import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +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("/api") +@RequestMapping("/api/book") public class ApiController { private final Logger log = LoggerFactory.getLogger(ApiController.class); + private final Map book = new HashMap<>(); - @GetMapping - public String get(@RequestParam(name = "name", defaultValue = "World") String name) { - return String.format("Hello, %s!", name); - } - - @GetMapping("/test") - public String getTest() { - return new Date().toString(); - } - - @GetMapping("/num") - public Integer getNum() { - return 10; - } - - @GetMapping("/list") - public List getList() { - return List.of(10, 20, 30, 40, 50); - } - - @GetMapping("/map") - public TestDto getMap() { - return new TestDto("Ivanov", "Ivan"); - } - - @PostMapping("/map") - public TestDto postMap(@RequestBody TestDto testDto) { - return testDto; + @GetMapping("/{id}") + public Books getBook(@PathVariable(name = "id") int id) { + return book.get(id); } @PostMapping - public String postMethodName(@RequestBody String name) { - return get(name); + public Books postBook(@RequestBody Books entity) { + book.put(book.size(), entity); + return entity; } - @PutMapping("/{name}") - public String putMethodName(@PathVariable(name = "name") String name, @RequestBody String entity) { - log.info("The body value is {}", entity); - return get(name); + @PutMapping("/{id}") + public Books putMethodBook(@PathVariable(name = "id") int id, @RequestBody Books entity) { + book.replace(id, entity); + return entity; } -} + + @GetMapping + public List getMethodAll() { + return List.copyOf(book.values()); + } + + @DeleteMapping("/{id}") + public Books delMethod(@PathVariable(name = "id") int id) { + Books old = book.get(id); + book.remove(id); + return old; + } +} \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/Books.java b/demo/src/main/java/com/example/demo/Books.java new file mode 100644 index 0000000..cf04d86 --- /dev/null +++ b/demo/src/main/java/com/example/demo/Books.java @@ -0,0 +1,47 @@ +package com.example.demo; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Books { + private String name; + private double price; + private int typesId; + private int authorsId; + private String image; + + @JsonCreator + public Books( + @JsonProperty(value = "name") String name, + @JsonProperty(value = "price") double price, + @JsonProperty(value = "typesId") int typesId, + @JsonProperty(value = "authorsId") int authorsId, + @JsonProperty(value = "image") String image) { + this.name = name; + this.price = price; + this.typesId = typesId; + this.authorsId = authorsId; + this.image = image; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } + + public int getTypesId() { + return typesId; + } + + public int getAuthorsId() { + return authorsId; + } + + public String getImage() { + return image; + } + +} \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/TestDto.java b/demo/src/main/java/com/example/demo/TestDto.java deleted file mode 100644 index 8a52735..0000000 --- a/demo/src/main/java/com/example/demo/TestDto.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.example.demo; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class TestDto { - private String shortName; - private String fullName; - - public TestDto() { - } - - @JsonCreator - public TestDto( - @JsonProperty(value = "shortName") String shortName, - @JsonProperty(value = "fullName") String fullName) { - this.shortName = shortName; - this.fullName = fullName; - } - - public String getShortName() { - return shortName; - } - - public String getFullName() { - return fullName; - } - -}