From 0286800b3e607e57a3f2c4da1ee496674f6715a7 Mon Sep 17 00:00:00 2001 From: Jigar Joshi Date: Fri, 10 Jun 2022 12:27:11 -0700 Subject: [PATCH] fix: Updated starter app to be compatible with 1.0.0-alpha17 client version --- pom.xml | 4 +- .../tigrisdata/starter/collections/Order.java | 104 +++++++++--------- .../starter/collections/Product.java | 36 +++--- .../tigrisdata/starter/collections/User.java | 24 ++-- .../starter/controller/OrderController.java | 6 +- .../starter/controller/ProductController.java | 11 +- .../starter/controller/UserController.java | 7 +- .../starter/spring/TigrisInitializer.java | 3 +- 8 files changed, 95 insertions(+), 100 deletions(-) diff --git a/pom.xml b/pom.xml index 8118871..90c6835 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.tigrisdata tigris-example-java - 1.0.0-alpha.16 + 1.0.0-alpha.17 @@ -26,7 +26,7 @@ - 1.0.0-alpha.16 + 1.0.0-alpha.17 1.8 1.8 UTF-8 diff --git a/src/main/java/com/tigrisdata/starter/collections/Order.java b/src/main/java/com/tigrisdata/starter/collections/Order.java index 62b5a2c..3ef8c8c 100644 --- a/src/main/java/com/tigrisdata/starter/collections/Order.java +++ b/src/main/java/com/tigrisdata/starter/collections/Order.java @@ -24,7 +24,7 @@ public class Order implements TigrisCollectionType { @TigrisField(description = "A unique identifier for the order") @TigrisPrimaryKey(order = 1, autoGenerate = true) - private Integer id; + private int id; @TigrisField(description = "The identifier of the user that placed the order") private int userId; @@ -43,6 +43,54 @@ public Order(int userId, double orderTotal, List productItems) { this.productItems = productItems; } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public double getOrderTotal() { + return orderTotal; + } + + public void setOrderTotal(double orderTotal) { + this.orderTotal = orderTotal; + } + + public List getProductItems() { + return productItems; + } + + public void setProductItems(List productItems) { + this.productItems = productItems; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Order order = (Order) o; + return userId == order.userId + && Double.compare(order.orderTotal, orderTotal) == 0 + && Objects.equals(id, order.id) + && Objects.equals(productItems, order.productItems); + } + + @Override + public int hashCode() { + return Objects.hash(id, userId, orderTotal, productItems); + } + public static class ProductItem { @TigrisField(description = "The product identifier") private int productId; @@ -61,14 +109,14 @@ public int getProductId() { return productId; } - public int getQuantity() { - return quantity; - } - public void setProductId(int productId) { this.productId = productId; } + public int getQuantity() { + return quantity; + } + public void setQuantity(int quantity) { this.quantity = quantity; } @@ -91,50 +139,4 @@ public int hashCode() { return result; } } - - public Integer getId() { - return id; - } - - public int getUserId() { - return userId; - } - - public double getOrderTotal() { - return orderTotal; - } - - public List getProductItems() { - return productItems; - } - - public void setId(Integer id) { - this.id = id; - } - - public void setUserId(int userId) { - this.userId = userId; - } - - public void setOrderTotal(double orderTotal) { - this.orderTotal = orderTotal; - } - - public void setProductItems(List productItems) { - this.productItems = productItems; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Order order = (Order) o; - return userId == order.userId && Double.compare(order.orderTotal, orderTotal) == 0 && Objects.equals(id, - order.id) && Objects.equals(productItems, order.productItems); - } - - @Override - public int hashCode() { - return Objects.hash(id, userId, orderTotal, productItems); - } } diff --git a/src/main/java/com/tigrisdata/starter/collections/Product.java b/src/main/java/com/tigrisdata/starter/collections/Product.java index 0d01627..f5466af 100644 --- a/src/main/java/com/tigrisdata/starter/collections/Product.java +++ b/src/main/java/com/tigrisdata/starter/collections/Product.java @@ -25,7 +25,7 @@ public class Product implements TigrisCollectionType { @TigrisField(description = "A unique identifier for the product") @TigrisPrimaryKey(order = 1, autoGenerate = true) - private Integer id; + private int id; @TigrisField(description = "Name of the product") private String name; @@ -38,41 +38,41 @@ public class Product implements TigrisCollectionType { public Product() {} - public Product(Integer id, String name, int quantity, double price) { + public Product(int id, String name, int quantity, double price) { this.id = id; this.name = name; this.quantity = quantity; this.price = price; } - public Integer getId() { + public int getId() { return id; } - public String getName() { - return name; - } - - public int getQuantity() { - return quantity; - } - - public double getPrice() { - return price; + public void setId(int id) { + this.id = id; } - public void setId(Integer id) { - this.id = id; + public String getName() { + return name; } public void setName(String name) { this.name = name; } + public int getQuantity() { + return quantity; + } + public void setQuantity(int quantity) { this.quantity = quantity; } + public double getPrice() { + return price; + } + public void setPrice(double price) { this.price = price; } @@ -82,8 +82,10 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Product product = (Product) o; - return quantity == product.quantity && Double.compare(product.price, price) == 0 && Objects.equals(id, - product.id) && Objects.equals(name, product.name); + return quantity == product.quantity + && Double.compare(product.price, price) == 0 + && Objects.equals(id, product.id) + && Objects.equals(name, product.name); } @Override diff --git a/src/main/java/com/tigrisdata/starter/collections/User.java b/src/main/java/com/tigrisdata/starter/collections/User.java index 6466690..7dfe73a 100644 --- a/src/main/java/com/tigrisdata/starter/collections/User.java +++ b/src/main/java/com/tigrisdata/starter/collections/User.java @@ -22,7 +22,7 @@ public class User implements TigrisCollectionType { @TigrisField(description = "A unique identifier for the user") @TigrisPrimaryKey(order = 1, autoGenerate = true) - private Integer id; + private int id; @TigrisField(description = "Name of the user") private String name; @@ -32,26 +32,26 @@ public class User implements TigrisCollectionType { public User() {} - public Integer getId() { + public int getId() { return id; } - public String getName() { - return name; - } - - public double getBalance() { - return balance; + public void setId(int id) { + this.id = id; } - public void setId(Integer id) { - this.id = id; + public String getName() { + return name; } public void setName(String name) { this.name = name; } + public double getBalance() { + return balance; + } + public void setBalance(double balance) { this.balance = balance; } @@ -61,7 +61,9 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; - return Double.compare(user.balance, balance) == 0 && Objects.equals(id, user.id) && Objects.equals(name, user.name); + return Double.compare(user.balance, balance) == 0 + && Objects.equals(id, user.id) + && Objects.equals(name, user.name); } @Override diff --git a/src/main/java/com/tigrisdata/starter/controller/OrderController.java b/src/main/java/com/tigrisdata/starter/controller/OrderController.java index 45711e4..268d7d6 100644 --- a/src/main/java/com/tigrisdata/starter/controller/OrderController.java +++ b/src/main/java/com/tigrisdata/starter/controller/OrderController.java @@ -47,10 +47,6 @@ public OrderController(TigrisDatabase tigrisStarterDatabase) { public ResponseEntity read(@PathVariable("id") int id) throws TigrisException { Optional order = tigrisStarterDatabase.getCollection(Order.class).readOne(Filters.eq("id", id)); - if (order.isPresent()) { - return ResponseEntity.ok(order.get()); - } - return ResponseEntity.notFound().build(); + return order.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } - } diff --git a/src/main/java/com/tigrisdata/starter/controller/ProductController.java b/src/main/java/com/tigrisdata/starter/controller/ProductController.java index c2e14d6..befb2a6 100644 --- a/src/main/java/com/tigrisdata/starter/controller/ProductController.java +++ b/src/main/java/com/tigrisdata/starter/controller/ProductController.java @@ -14,7 +14,6 @@ package com.tigrisdata.starter.controller; import com.tigrisdata.db.client.Filters; -import com.tigrisdata.db.client.InsertResponse; import com.tigrisdata.db.client.TigrisCollection; import com.tigrisdata.db.client.TigrisDatabase; import com.tigrisdata.db.client.error.TigrisException; @@ -43,17 +42,15 @@ public ProductController(TigrisDatabase tigrisDatabase) { @PostMapping("/create") public ResponseEntity create(@RequestBody Product product) throws TigrisException { - InsertResponse insertResponse = productTigrisCollection.insert(product); - return ResponseEntity.status(HttpStatus.CREATED).body("product created with id = "+insertResponse.getGeneratedKeys()[0].get("id")); + productTigrisCollection.insert(product); + return ResponseEntity.status(HttpStatus.CREATED) + .body("product created with id = " + product.getId()); } @GetMapping("/{id}") public ResponseEntity read(@PathVariable("id") int id) throws TigrisException { Optional product = productTigrisCollection.readOne(Filters.eq("id", id)); - if (product.isPresent()) { - return ResponseEntity.ok(product.get()); - } - return ResponseEntity.notFound().build(); + return product.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @DeleteMapping("/{id}") diff --git a/src/main/java/com/tigrisdata/starter/controller/UserController.java b/src/main/java/com/tigrisdata/starter/controller/UserController.java index 4209e5c..23ede19 100644 --- a/src/main/java/com/tigrisdata/starter/controller/UserController.java +++ b/src/main/java/com/tigrisdata/starter/controller/UserController.java @@ -44,16 +44,13 @@ public UserController(TigrisDatabase tigrisDatabase) { @PostMapping("/create") public ResponseEntity create(@RequestBody User user) throws TigrisException { InsertResponse insertResponse = userTigrisCollection.insert(user); - return ResponseEntity.status(HttpStatus.CREATED).body("User created with id = "+insertResponse.getGeneratedKeys()[0].get("id")); + return ResponseEntity.status(HttpStatus.CREATED).body("User created with id = " + user.getId()); } @GetMapping("/{id}") public ResponseEntity read(@PathVariable("id") int id) throws TigrisException { Optional user = userTigrisCollection.readOne(Filters.eq("id", id)); - if (user.isPresent()) { - return ResponseEntity.ok(user.get()); - } - return ResponseEntity.notFound().build(); + return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } @DeleteMapping("/{id}") diff --git a/src/main/java/com/tigrisdata/starter/spring/TigrisInitializer.java b/src/main/java/com/tigrisdata/starter/spring/TigrisInitializer.java index 2f8650a..fc787ef 100644 --- a/src/main/java/com/tigrisdata/starter/spring/TigrisInitializer.java +++ b/src/main/java/com/tigrisdata/starter/spring/TigrisInitializer.java @@ -24,11 +24,10 @@ public class TigrisInitializer implements CommandLineRunner { + private static final Logger log = LoggerFactory.getLogger(TigrisInitializer.class); private final TigrisClient tigrisClient; private final String dbName; - private static final Logger log = LoggerFactory.getLogger(TigrisInitializer.class); - public TigrisInitializer(TigrisClient tigrisClient, String dbName) { this.tigrisClient = tigrisClient; this.dbName = dbName;