diff --git a/README.md b/README.md index 4651f8e..43fc4c0 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,6 @@ Customer documents and related API responses: + // Use a computed field to calculate the customer's cumulative purchase total. + // The field sums purchase `total` values from the customer's linked Order documents. + compute totalPurchaseAmt: Number = (customer => customer.orders.fold(0, (sum, order) => { - + let order: Any = order + sum + order.total + })) ... diff --git a/schema/collections.fsl b/schema/collections.fsl index 2a7a69a..a0de39c 100644 --- a/schema/collections.fsl +++ b/schema/collections.fsl @@ -72,9 +72,8 @@ collection Order { compute items: Set = (order => OrderItem.byOrder(order)) compute total: Number = (order => order.items.fold(0, (sum, orderItem) => { - let orderItem: Any = orderItem if (orderItem.product != null) { - sum + orderItem.product.price * orderItem.quantity + sum + orderItem.product!.price * orderItem.quantity } else { sum } @@ -113,4 +112,4 @@ collection OrderItem { index byOrderAndProduct { terms [.order, .product] } -} \ No newline at end of file +} diff --git a/schema/functions.fsl b/schema/functions.fsl index 3649c09..d34cc59 100644 --- a/schema/functions.fsl +++ b/schema/functions.fsl @@ -70,7 +70,7 @@ function getOrCreateCart(id) { function checkout(orderId, status, payment) { // Find the order by id, using the ! operator to assert that the order exists. - let order: Any = Order.byId(orderId)! + let order = Order.byId(orderId)! // Check that we are setting the order to the processing status. If not, we should // not be calling this function. @@ -98,7 +98,7 @@ function checkout(orderId, status, payment) { // Check that the order items are still in stock. order!.items.forEach((item) => { - let product: Any = item.product + let product = item.product if (product.stock < item.quantity) { abort("One of the selected products does not have the requested quantity in stock.") } @@ -106,7 +106,7 @@ function checkout(orderId, status, payment) { // Decrement the stock of each product in the order. order!.items.forEach((item) => { - let product: Any = item.product + let product = item.product product.update({ stock: product.stock - item.quantity }) }) @@ -129,4 +129,4 @@ function validateOrderStatusTransition(oldStatus, newStatus) { // The order can only transition from shipped to delivered. abort("Invalid status transition.") } -} \ No newline at end of file +} diff --git a/seed/orders.fql b/seed/orders.fql index 0b1fd0f..8c4cc83 100644 --- a/seed/orders.fql +++ b/seed/orders.fql @@ -2,16 +2,16 @@ "query": " let customer = Customer.byEmail('fake@fauna.com').first()!\n let orders = ['cart', 'processing', 'shipped', 'delivered'].map(status => {\n - let order: Any = Order.byCustomer(customer).firstWhere(o => o.status == status)\n + let order = Order.byCustomer(customer).firstWhere(o => o.status == status)\n if (order == null) {\n - let newOrder: Any = Order.create({\n + let newOrder = Order.create({\n customer: customer,\n status: status,\n createdAt: Time.now(),\n payment: {}\n })\n - let product: Any = Product.byName('Drone').first()!\n - let orderItem: Any = OrderItem.create({ order: newOrder, product: product, quantity: 1 })\n + let product = Product.byName('Drone').first()!\n + let orderItem = OrderItem.create({ order: newOrder, product: product, quantity: 1 })\n orderItem\n newOrder\n } else {\n diff --git a/seed/products.fql b/seed/products.fql index d177df3..c383b62 100644 --- a/seed/products.fql +++ b/seed/products.fql @@ -63,7 +63,7 @@ 'category': 'movies' } ].map(p => {\n - let existing: Any = Product.byName(p.name).first()\n + let existing = Product.byName(p.name).first()\n if (existing != null) {\n existing!.update({ stock: p.stock })\n } else {\n diff --git a/src/main/java/fauna/sample/controllers/customers/CustomersController.java b/src/main/java/fauna/sample/controllers/customers/CustomersController.java index c244c04..ab3faed 100644 --- a/src/main/java/fauna/sample/controllers/customers/CustomersController.java +++ b/src/main/java/fauna/sample/controllers/customers/CustomersController.java @@ -50,7 +50,7 @@ Future get(@PathVariable String id) { // // Use projection to only return the fields you need. Query query = fql(""" - let customer: Any = Customer.byId(${id})! + let customer = Customer.byId(${id})! ${response} """, args); @@ -71,7 +71,7 @@ Future create(@RequestBody Customer customer) { // Create a new Customer document with the provided fields. // Use projection to only return the fields you need. Query query = fql(""" - let customer: Any = Customer.create(${customer}) + let customer = Customer.create(${customer}) ${response} """, args); @@ -99,7 +99,7 @@ Future update(@PathVariable String id, @RequestBody Customer customer) // // Use projection to only return the fields you need. Query query = fql(""" - let customer: Any = Customer.byId(${id})!.update(${customer}) + let customer = Customer.byId(${id})!.update(${customer}) ${response} """, args); diff --git a/src/main/java/fauna/sample/controllers/orders/OrdersController.java b/src/main/java/fauna/sample/controllers/orders/OrdersController.java index 90f940e..7d5652a 100644 --- a/src/main/java/fauna/sample/controllers/orders/OrdersController.java +++ b/src/main/java/fauna/sample/controllers/orders/OrdersController.java @@ -93,7 +93,7 @@ public OrdersController(FaunaClient client) { Future get(@PathVariable String id) { var args = Map.of("id", id, "response", response); var q = fql(""" - let order: Any = Order.byId(${id})! + let order = Order.byId(${id})! ${response} """, args); @@ -118,7 +118,7 @@ Future update(@PathVariable String id, @RequestBody OrderUpdate req) { // as the order status. query = fql(""" let req = ${req} - let order: Any = checkout(${id}, req.status, req.payment) + let order = checkout(${id}, req.status, req.payment) ${response} """, args); } else { @@ -127,7 +127,7 @@ Future update(@PathVariable String id, @RequestBody OrderUpdate req) { // error. We then use the validateOrderStatusTransition UDF to ensure that the order status transition // is valid. If the transition is not valid, the UDF will throw an abort error. query = fql(""" - let order: Any = Order.byId(${id})! + let order = Order.byId(${id})! let req = ${req} // Validate the order status transition if a status is provided. @@ -162,7 +162,7 @@ Future> getByCustomer(@PathVariable("id") String customerId, @Reques if (afterToken != null) { /** * The `afterToken` parameter contains a Fauna `after` cursor. - * `after` cursors may contain special characters, such as `.` or `+`). + * `after` cursors may contain special characters, such as `.` or `+`). * Make sure to URL encode the `afterToken` value to preserve these * characters in URLs. */ @@ -177,10 +177,8 @@ Future> getByCustomer(@PathVariable("id") String customerId, @Reques // the results to return only the fields we care about. var args = Map.of("customerId", customerId,"pageSize", pageSize,"response", response); query = fql(""" - let customer: Any = Customer.byId(${customerId})! + let customer = Customer.byId(${customerId})! Order.byCustomer(customer).pageSize(${pageSize}).map((order) => { - let order: Any = order - // Return the order. ${response} }) @@ -201,7 +199,7 @@ Future createCart(@PathVariable("id") String customerId) { // definition can be found 'server/schema/functions.fsl'. Map args = Map.of("customerId", customerId, "response", response); Query query = fql(""" - let order: Any = getOrCreateCart(${customerId}) + let order = getOrCreateCart(${customerId}) // Return the cart. ${response} @@ -223,7 +221,7 @@ Future addToCart(@PathVariable("id") String customerId, @RequestBody AddT Map args = Map.of("customerId", customerId, "req", req, "response", response); Query query = fql(""" let req = ${req} - let order: Any = createOrUpdateCartItem(${customerId}, req.productName, req.quantity) + let order = createOrUpdateCartItem(${customerId}, req.productName, req.quantity) // Return the cart as an OrderResponse object. ${response} @@ -244,7 +242,7 @@ Future getCart(@PathVariable("id") String customerId) { // If the document does not exist, Fauna will throw a document_not_found error. Map args = Map.of("customerId", customerId, "response", response); Query query = fql(""" - let order: Any = Customer.byId(${customerId})!.cart + let order = Customer.byId(${customerId})!.cart // Return the cart as an OrderResponse object. ${response} diff --git a/src/main/java/fauna/sample/controllers/products/ProductsController.java b/src/main/java/fauna/sample/controllers/products/ProductsController.java index 23ae2f4..ccffb14 100644 --- a/src/main/java/fauna/sample/controllers/products/ProductsController.java +++ b/src/main/java/fauna/sample/controllers/products/ProductsController.java @@ -42,7 +42,7 @@ Future> paginate( if (afterToken != null) { /** * The `afterToken` parameter contains a Fauna `after` cursor. - * `after` cursors may contain special characters, such as `.` or `+`). + * `after` cursors may contain special characters, such as `.` or `+`). * Make sure to URL encode the `afterToken` value to preserve these * characters in URLs. */ @@ -73,14 +73,16 @@ Future> paginate( ${subQuery} .map(product => { let product: Any = product - let category: Any = product.category { id: product.id, name: product.name, price: product.price, description: product.description, stock: product.stock, - category: { id: category.id, name: category.name, description: category.description }, + category: { + id: product.category.id, + name: product.category.name, + description: product.category.description }, } }) """, Map.of("subQuery", subQuery)); @@ -113,7 +115,7 @@ Future create(@RequestBody ProductRequest req) { // Create the product with the given values. let args = { name: input.name, price: input.price, stock: input.stock, description: input.description, category: category } - let product: Any = Product.create(args) + let product = Product.create(args) // Use projection to only return the fields you need. product { @@ -147,7 +149,7 @@ Future update(@PathVariable String id, @RequestBody ProductRequest req) // Get the product by id, using the ! operator to assert that the product exists. // If it does not exist Fauna will throw a document_not_found error. - let product: Any = Product.byId(${id})! + let product = Product.byId(${id})! // Get the category by name. We can use .first() here because we know that the category // name is unique. @@ -217,7 +219,6 @@ Future> search(@RequestParam(required = false) Integer minPrice, query = fql(""" Product.sortedByPriceLowToHigh({ from: ${minPrice}, to: ${maxPrice}}) .pageSize(${pageSize}).map(product => { - let product: Any = product product { id, name,