Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
fix: Update starter app to be compatible with 1.0.0-alpha.7
Browse files Browse the repository at this point in the history
  • Loading branch information
JigarJoshi committed Apr 26, 2022
1 parent 7fd20b9 commit 6961092
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 155 deletions.
15 changes: 1 addition & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,11 @@
</dependencies>
<properties>
<!-- we are still pre-release -->
<tigrisdb.client.java.version>1.0.0-alpha.6</tigrisdb.client.java.version>
<tigrisdb.client.java.version>1.0.0-alpha.7</tigrisdb.client.java.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<!-- For immutable models to work with jackson -->
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>
77 changes: 48 additions & 29 deletions src/main/java/com/tigrisdata/starter/collections/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@
*/
package com.tigrisdata.starter.collections;

import com.tigrisdata.db.annotation.TigrisDBCollectionField;
import com.tigrisdata.db.annotation.TigrisDBCollectionPrimaryKey;
import com.tigrisdata.db.annotation.TigrisField;
import com.tigrisdata.db.annotation.TigrisPrimaryKey;
import com.tigrisdata.db.type.TigrisCollectionType;

import java.util.List;
import java.util.Objects;

public class Order implements TigrisCollectionType {

@TigrisDBCollectionField(description = "A unique identifier for the order")
@TigrisDBCollectionPrimaryKey(1)
private final int id;
@TigrisField(description = "A unique identifier for the order")
@TigrisPrimaryKey(1)
private int id;

@TigrisDBCollectionField(description = "The identifier of the user that placed the order")
private final int userId;
@TigrisField(description = "The identifier of the user that placed the order")
private int userId;

@TigrisDBCollectionField(description = "The total cost of the order")
private final double orderTotal;
@TigrisField(description = "The total cost of the order")
private double orderTotal;

@TigrisDBCollectionField(description = "The list of products that are part of this order")
private final List<ProductItem> productItems;
@TigrisField(description = "The list of products that are part of this order")
private List<ProductItem> productItems;

public Order() {}

public Order(int id, int userId, double orderTotal, List<ProductItem> productItems) {
this.id = id;
Expand All @@ -43,11 +45,13 @@ public Order(int id, int userId, double orderTotal, List<ProductItem> productIte
}

public static class ProductItem {
@TigrisDBCollectionField(description = "The product identifier")
private final int productId;
@TigrisField(description = "The product identifier")
private int productId;

@TigrisField(description = "The quantity of this product in this order")
private int quantity;

@TigrisDBCollectionField(description = "The quantity of this product in this order")
private final int quantity;
public ProductItem() {}

public ProductItem(int productId, int quantity) {
this.productId = productId;
Expand All @@ -62,6 +66,14 @@ public int getQuantity() {
return quantity;
}

public void setProductId(int productId) {
this.productId = productId;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -97,28 +109,35 @@ public List<ProductItem> getProductItems() {
return productItems;
}

public void setId(int id) {
this.id = id;
}

public void setUserId(int userId) {
this.userId = userId;
}

public void setOrderTotal(double orderTotal) {
this.orderTotal = orderTotal;
}

public void setProductItems(List<ProductItem> 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;

if (id != order.id) return false;
if (userId != order.userId) return false;
if (Double.compare(order.orderTotal, orderTotal) != 0) return false;
return Objects.equals(productItems, order.productItems);
return id == order.id
&& userId == order.userId
&& Double.compare(order.orderTotal, orderTotal) == 0
&& Objects.equals(productItems, order.productItems);
}

@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + userId;
temp = Double.doubleToLongBits(orderTotal);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (productItems != null ? productItems.hashCode() : 0);
return result;
return Objects.hash(id, userId, orderTotal, productItems);
}
}
61 changes: 36 additions & 25 deletions src/main/java/com/tigrisdata/starter/collections/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@
*/
package com.tigrisdata.starter.collections;

import com.tigrisdata.db.annotation.TigrisDBCollectionField;
import com.tigrisdata.db.annotation.TigrisDBCollectionPrimaryKey;
import com.tigrisdata.db.annotation.TigrisCollection;
import com.tigrisdata.db.annotation.TigrisField;
import com.tigrisdata.db.annotation.TigrisPrimaryKey;
import com.tigrisdata.db.type.TigrisCollectionType;

import java.util.Objects;

@TigrisCollection("product_collection")
public class Product implements TigrisCollectionType {

@TigrisDBCollectionField(description = "A unique identifier for the product")
@TigrisDBCollectionPrimaryKey(1)
private final int id;
@TigrisField(description = "A unique identifier for the product")
@TigrisPrimaryKey(1)
private int id;

@TigrisDBCollectionField(description = "Name of the product")
private final String name;
@TigrisField(description = "Name of the product")
private String name;

@TigrisDBCollectionField(description = "Number of products available in the store")
private final int quantity;
@TigrisField(description = "Number of products available in the store")
private int quantity;

@TigrisDBCollectionField(description = "Price of the product")
private final double price;
@TigrisField(description = "Price of the product")
private double price;

public Product() {}

public Product(int id, String name, int quantity, double price) {
this.id = id;
Expand All @@ -57,28 +61,35 @@ public double getPrice() {
return price;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public void setPrice(double price) {
this.price = price;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Product product = (Product) o;

if (id != product.id) return false;
if (quantity != product.quantity) return false;
if (Double.compare(product.price, price) != 0) return false;
return Objects.equals(name, product.name);
return id == product.id
&& quantity == product.quantity
&& Double.compare(product.price, price) == 0
&& Objects.equals(name, product.name);
}

@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + quantity;
temp = Double.doubleToLongBits(price);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
return Objects.hash(id, name, quantity, price);
}
}
51 changes: 26 additions & 25 deletions src/main/java/com/tigrisdata/starter/collections/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@
*/
package com.tigrisdata.starter.collections;

import com.tigrisdata.db.annotation.TigrisDBCollectionField;
import com.tigrisdata.db.annotation.TigrisDBCollectionPrimaryKey;
import com.tigrisdata.db.annotation.TigrisField;
import com.tigrisdata.db.annotation.TigrisPrimaryKey;
import com.tigrisdata.db.type.TigrisCollectionType;

import java.util.Objects;

public class User implements TigrisCollectionType {
@TigrisDBCollectionField(description = "A unique identifier for the user")
@TigrisDBCollectionPrimaryKey(1)
private final int id;
@TigrisField(description = "A unique identifier for the user")
@TigrisPrimaryKey(1)
private int id;

@TigrisDBCollectionField(description = "Name of the user")
private final String name;
@TigrisField(description = "Name of the user")
private String name;

@TigrisDBCollectionField(description = "User account balance")
private final double balance;
@TigrisField(description = "User account balance")
private double balance;

public User(int id, String name, double balance) {
this.id = id;
this.name = name;
this.balance = balance;
public User() {
}

public int getId() {
Expand All @@ -48,26 +45,30 @@ public double getBalance() {
return balance;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setBalance(double balance) {
this.balance = balance;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

User user = (User) o;

if (id != user.id) return false;
if (Double.compare(user.balance, balance) != 0) return false;
return Objects.equals(name, user.name);
return id == user.id
&& Double.compare(user.balance, balance) == 0
&& Objects.equals(name, user.name);
}

@Override
public int hashCode() {
int result;
long temp;
result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
temp = Double.doubleToLongBits(balance);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
return Objects.hash(id, name, balance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import com.tigrisdata.db.client.Filters;
import com.tigrisdata.db.client.TigrisDatabase;
import com.tigrisdata.db.client.error.TigrisDBException;
import com.tigrisdata.db.client.error.TigrisException;
import com.tigrisdata.starter.collections.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -36,7 +36,7 @@ public OrderController(TigrisDatabase tigrisStarterDatabase) {
}

@GetMapping("/{id}")
public Order read(@PathVariable("id") int id) throws TigrisDBException {
public Order read(@PathVariable("id") int id) throws TigrisException {
return tigrisStarterDatabase.getCollection(Order.class).readOne(Filters.eq("id", id)).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.tigrisdata.db.client.Filters;
import com.tigrisdata.db.client.TigrisCollection;
import com.tigrisdata.db.client.TigrisDatabase;
import com.tigrisdata.db.client.error.TigrisDBException;
import com.tigrisdata.db.client.error.TigrisException;
import com.tigrisdata.starter.collections.Product;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -39,18 +39,18 @@ public ProductController(TigrisDatabase tigrisDatabase) {
}

@PostMapping("/create")
public ResponseEntity<String> create(@RequestBody Product product) throws TigrisDBException {
public ResponseEntity<String> create(@RequestBody Product product) throws TigrisException {
productTigrisCollection.insert(product);
return ResponseEntity.status(HttpStatus.CREATED).body("product created");
}

@GetMapping("/{id}")
public Product read(@PathVariable("id") int id) throws TigrisDBException {
public Product read(@PathVariable("id") int id) throws TigrisException {
return productTigrisCollection.readOne(Filters.eq("id", id)).get();
}

@DeleteMapping("/{id}")
public ResponseEntity<String> delete(@PathVariable("id") int id) throws TigrisDBException {
public ResponseEntity<String> delete(@PathVariable("id") int id) throws TigrisException {
productTigrisCollection.delete(Filters.eq("id", id));
return ResponseEntity.status(HttpStatus.OK).body("product deleted");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package com.tigrisdata.starter.controller;

import com.tigrisdata.db.client.error.TigrisDBException;
import com.tigrisdata.db.client.error.TigrisException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
Expand All @@ -24,9 +24,9 @@
public class TigrisDBExceptionAdvice {

@ResponseBody
@ExceptionHandler(TigrisDBException.class)
@ExceptionHandler(TigrisException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
String exceptionHandler(TigrisDBException ex) {
String exceptionHandler(TigrisException ex) {
String errorMessage = ex.getMessage();
if (ex.getCause() != null && ex.getCause().getMessage() != null) {
errorMessage += ", " + ex.getCause().getMessage();
Expand Down
Loading

0 comments on commit 6961092

Please sign in to comment.