Commit c5d2649f authored by Kutzner's avatar Kutzner 🤸
Browse files

add post with request body to api

parent b0716979
...@@ -13,8 +13,12 @@ import org.springframework.web.bind.annotation.GetMapping; ...@@ -13,8 +13,12 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
// WRONG IMPORT!!!
//import io.swagger.v3.oas.annotations.parameters.RequestBody;
@RestController @RestController
public class ShoppingAPI { public class ShoppingAPI {
...@@ -93,7 +97,6 @@ public class ShoppingAPI { ...@@ -93,7 +97,6 @@ public class ShoppingAPI {
//return "Increased Amount (from " + (tempItem.amount - amount) + " to " + newItem.amount + ") of " + newItem.item + " successfully in shoppingItemRepository"; //return "Increased Amount (from " + (tempItem.amount - amount) + " to " + newItem.amount + ") of " + newItem.item + " successfully in shoppingItemRepository";
return newItem; return newItem;
} }
} }
...@@ -133,7 +136,7 @@ public class ShoppingAPI { ...@@ -133,7 +136,7 @@ public class ShoppingAPI {
logger.info("new id = {}", tempItem.id); logger.info("new id = {}", tempItem.id);
logger.info("new item = {}", tempItem.item); logger.info("new item = {}", tempItem.item);
logger.info("amount = {}", tempItem.amount); logger.info("new amount = {}", tempItem.amount);
return "Increased Amount (from " + (tempItem.amount - 1) + " to " + newItem.amount + ") of " + newItem.item + " successfully in shoppingItemRepository"; return "Increased Amount (from " + (tempItem.amount - 1) + " to " + newItem.amount + ") of " + newItem.item + " successfully in shoppingItemRepository";
} }
...@@ -146,12 +149,51 @@ public class ShoppingAPI { ...@@ -146,12 +149,51 @@ public class ShoppingAPI {
logger.info("----- ADD Item -----"); logger.info("----- ADD Item -----");
logger.info("new id = {}", newItem.id); logger.info("new id = {}", newItem.id);
logger.info("new item = {}", newItem.item); logger.info("new item = {}", newItem.item);
logger.info("amount = {}", newItem.amount); logger.info("new amount = {}", newItem.amount);
return "Added " + newItem.item + " successfully to shoppingItemRepository"; return "Added " + newItem.item + " successfully to shoppingItemRepository";
} }
@PostMapping(consumes = "application/json", produces = "application/json", path = "/shoppingItem/add")
public ShoppingItem addShoppingItemRequestBody(@RequestBody ShoppingItem newItem) {
Iterable<ShoppingItem> iterableItems = shoppingItemRepository.findAll();
Iterator<ShoppingItem> itemTerator = iterableItems.iterator();
while(itemTerator.hasNext()){
ShoppingItem tempItem = itemTerator.next();
if (tempItem.item.equals(newItem.item)) {
logger.info("----- ADD Item via UI with Amount-----");
int newAmount = tempItem.amount + newItem.getAmount();
newItem.setId(tempItem.id);
newItem.setAmount(newAmount);
shoppingItemRepository.save(newItem);
logger.info("new id = {}", newItem.id);
logger.info("new item = {}", newItem.item);
logger.info("amount = {}", newAmount);
return newItem;
}
}
shoppingItemRepository.save(newItem);
logger.info("----- ADD Item via UI -----");
logger.info("new id = {}", newItem.id);
logger.info("new item = {}", newItem.item);
logger.info("new amount = {}", newItem.amount);
return newItem;
}
@PutMapping("/shoppingItem/{old_item}/{new_item}") @PutMapping("/shoppingItem/{old_item}/{new_item}")
public String updaShoppingItem(@PathVariable String old_item, @PathVariable String new_item) { public String updaShoppingItem(@PathVariable String old_item, @PathVariable String new_item) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment