Commits (3)
......@@ -8,11 +8,11 @@
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.hft-stuttgart</groupId>
<artifactId>middleware</artifactId>
<groupId>de.kebidge.middleware</groupId>
<artifactId>shopping-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>middleware</name>
<description>Demo project for Spring Boot</description>
<name>ShoppingApp</name>
<description>Shopping List Application</description>
<properties>
<java.version>11</java.version>
</properties>
......@@ -31,13 +31,11 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
-->
<dependency>
<groupId>org.postgresql</groupId>
......
package de.hftstuttgart.middleware;
package de.kebidge.middleware;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class MiddlewareApplication {
......@@ -12,25 +9,4 @@ public class MiddlewareApplication {
public static void main(String[] args) {
SpringApplication.run(MiddlewareApplication.class, args);
}
/*
@RestController
class Hello {
@GetMapping("/")
public String sayHello() {
return "Hello Kevin";
}
@GetMapping("/hello")
public String sayHelloAgain() {
return "Hello Kevin, from another REST Endpoint";
}
@GetMapping("/hello/{parameter}")
public String sayHelloWithParameter(@PathVariable String parameter) {
return "Hello Kevin, from another REST Endpoint " + parameter;
}
}
*/
}
\ No newline at end of file
package de.hftstuttgart.middleware;
package de.kebidge.middleware;
import java.util.ArrayList;
import java.util.Iterator;
......@@ -24,11 +24,6 @@ public class ShoppingAPI {
Logger logger = LoggerFactory.getLogger(ShoppingAPI.class);
/*public ShoppingAPI(ShoppingItemRepository shoppingItemRepository) {
this.shoppingItemRepository = shoppingItemRepository;
}*/
@GetMapping("/shoppingItem")
public ArrayList<Optional<ShoppingItem>> getShoppingItems() {
......@@ -40,9 +35,6 @@ public class ShoppingAPI {
Iterator<ShoppingItem> itemTerator = iterableItems.iterator();
// TODO: That should be work
//Iterator<Optional<ShoppingItem>> optionalItemTerator = optionalItemList.iterator();
logger.info("----- GET Item -----");
while(itemTerator.hasNext()){
......@@ -71,6 +63,54 @@ public class ShoppingAPI {
return item;
}
@PostMapping(path = "/shoppingItem/{item}/{amount}", produces = "application/json")
public ShoppingItem addShoppingItemJSON(@PathVariable String item, @PathVariable int amount){
ShoppingItem newItem = new ShoppingItem(item);
Iterable<ShoppingItem> iterableItems = shoppingItemRepository.findAll();
Iterator<ShoppingItem> itemTerator = iterableItems.iterator();
while(itemTerator.hasNext()){
ShoppingItem tempItem = itemTerator.next();
if (tempItem.item.equals(item)) {
logger.info("----- ADD Item with Amount-----");
int newAmount = tempItem.amount + amount;
newItem.setId(tempItem.id);
newItem.setAmount(newAmount);
shoppingItemRepository.save(newItem);
logger.info("new id = {}", tempItem.id);
logger.info("new item = {}", tempItem.item);
logger.info("amount = {}", newAmount);
//return "Increased Amount (from " + (tempItem.amount - amount) + " to " + newItem.amount + ") of " + newItem.item + " successfully in shoppingItemRepository";
return newItem;
}
}
newItem.setAmount(amount);
shoppingItemRepository.save(newItem);
logger.info("----- ADD Item with Amount-----");
logger.info("new id = {}", newItem.id);
logger.info("new item = {}", newItem.item);
logger.info("amount = {}", amount);
//return "Added " + newItem.item + " with an Amount of " + amount + " successfully to shoppingItemRepository";
return newItem;
}
@PostMapping("/shoppingItem/{item}")
public String addShoppingItem(@PathVariable String item){
......@@ -128,12 +168,15 @@ public class ShoppingAPI {
logger.info("----- UPDATE Item -----");
logger.info("old id = {}", tempItem.id);
logger.info("old item = {}", tempItem.item);
logger.info("old amount = {}", tempItem.amount);
myNewItem.setId(tempItem.id);
myNewItem.setItem(new_item);
myNewItem.setAmount(tempItem.amount);
logger.info("new id ={}", myNewItem.id);
logger.info("new item ={}", myNewItem.item);
logger.info("new amount ={}", myNewItem.amount);
shoppingItemRepository.save(myNewItem);
......@@ -187,11 +230,17 @@ public class ShoppingAPI {
if(itemAmount > 1) {
/****************************************************
* ID bring nix da automatisch generiert !!!!!!!
* is aber nich schlimm
* amount ist ja richtig
*/
ShoppingItem newItem = new ShoppingItem(item);
newItem.setAmount(itemAmount - 1);
newItem.setId(itemId);
shoppingItemRepository.delete(tempItem);
//shoppingItemRepository.delete(tempItem);
logger.info("----- DELETE Item (Amount) -----");
logger.info("id = {}", newItem.id);
......@@ -208,7 +257,7 @@ public class ShoppingAPI {
logger.info("----- DELETE Item-----");
logger.info("delete id = {}", tempItem.id);
logger.info("delete item = {}", tempItem.item);
logger.info("delete amount = {}", tempItem.item);
logger.info("delete amount = {}", tempItem.amount);
shoppingItemRepository.delete(tempItem);
return "Deleted " + tempItem.item + " sucessfully";
......
package de.hftstuttgart.middleware;
package de.kebidge.middleware;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
......@@ -21,6 +21,16 @@ public class ShoppingItem {
item = itemName;
}
public ShoppingItem(String itemName, int itemAmount){
amount = itemAmount;
}
public ShoppingItem(long itemId, String itemName, int itemAmount){
id = itemId;
}
/* ********** *********** ********** */
......
package de.hftstuttgart.middleware;
package de.kebidge.middleware;
import org.springframework.data.repository.CrudRepository;
......
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
\ No newline at end of file
spring.datasource.url=jdbc:postgresql://localhost:5432/kebidgedata
spring.datasource.username=kebidge
spring.datasource.password=password
spring.jpa.show-sql=false
\ No newline at end of file
spring.datasource.url=jdbc:postgresql://localhost:5432/kevindb
spring.datasource.username=kevin
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
server.port = 8181
logging.level.de.hftstuttgart=INFO
\ No newline at end of file
logging.level.de.kebidge=INFO
spring.profiles.active=prod
\ No newline at end of file
package de.hftstuttgart.middleware;
package de.kebidge.middleware;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
......