Skip to content

Commit

Permalink
Merge pull request #2 from adutra/more-enhancements
Browse files Browse the repository at this point in the history
More enhancements :-)
  • Loading branch information
alexleventer authored Aug 20, 2020
2 parents 91ac410 + f3aba49 commit 535e624
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 49 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This application uses Spring Data Cassandra and DataStax Astra to build a REST API for a backend service that interacts with products and orders.

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/DataStax-Examples/spring-data-starter)
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/from-referrer)

## Running in Gitpod

Expand All @@ -24,12 +24,12 @@ This application uses Spring Data Cassandra and DataStax Astra to build a REST A

6. Once you have copied your credentials, click to Open in Gitpod:

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/DataStax-Examples/spring-data-starter)
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/from-referrer)

7. Once your gitpod workspace has loaded paste your service account credentials and enter in the bottom of the screen
![Screen Shot 2020-08-20 at 9 28 42 AM](https://user-images.githubusercontent.com/69874632/90801910-5e816c00-e2cb-11ea-874b-fb64ee7a26ae.png)

8. After entering, open this link in your browser: http://localhost:8081/swagger-ui/

9. You're ready to use this application!
![Swagger](https://github.com/DataStax-Examples/spring-data-starter/blob/master/doc/pics/spring-data-swagger-ui.png?raw=true)
![Swagger](doc/pics/spring-data-swagger-ui.png?raw=true)
Binary file modified doc/pics/spring-data-swagger-ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.examples;

import java.nio.file.Path;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand All @@ -9,5 +10,5 @@
@Setter
public class DataStaxAstraProperties {

private String secureConnectBundle;
private Path secureConnectBundle;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.datastax.examples;

import java.nio.file.Paths;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.cassandra.CqlSessionBuilderCustomizer;
Expand All @@ -17,7 +16,7 @@ public static void main(String[] args) {

@Bean
public CqlSessionBuilderCustomizer sessionBuilderCustomizer(DataStaxAstraProperties astraProperties) {
return builder -> builder.withCloudSecureConnectBundle(Paths.get(astraProperties.getSecureConnectBundle()));
return builder -> builder.withCloudSecureConnectBundle(astraProperties.getSecureConnectBundle());
}
}

60 changes: 36 additions & 24 deletions src/main/java/com/datastax/examples/order/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,66 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.UUID;

@BasePathAwareController
@RestController
public class OrderController {

@Autowired
private OrderRepository orderRepository;

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + "/swagger-ui/");
public ModelAndView root() {
return new ModelAndView("redirect:/swagger-ui/");
}

@GetMapping("orders/{id}")
public ResponseEntity<List<Order>> getOrder(@PathVariable("oid") UUID oid) {
return ResponseEntity.ok(orderRepository.findByKeyOrderId(oid));
}
// CREATE

@GetMapping("orders/{oid}/{pid}")
public ResponseEntity<Order> getProductInOrder(@PathVariable("oid") UUID oid, @PathVariable("id") UUID pid) {
return ResponseEntity.ok(orderRepository.findByKeyOrderIdAndKeyProductId(oid, pid));
@PostMapping("orders")
public Order createOrder(@RequestBody Order order) {
return orderRepository.save(order);
}

@DeleteMapping("orders/{id}")
public ResponseEntity deleteOrder(@PathVariable("oid") UUID oid) {
orderRepository.deleteByKeyOrderId(oid);
return ResponseEntity.noContent().build();
// UPDATE

@PutMapping("orders/{oid}/{pid}")
public Order updateOrder(@PathVariable("oid") UUID oid, @PathVariable("pid") UUID pid, @RequestBody Order order) {
order.getKey().setOrderId(oid);
order.getKey().setProductId(pid);
return orderRepository.save(order);
}

// DELETE

@DeleteMapping("orders/{oid}/{pid}")
public ResponseEntity deleteProductFromOrder(@PathVariable("oid") UUID oid, @PathVariable("id") UUID pid) {
public void deleteOrder(@PathVariable("oid") UUID oid, @PathVariable("pid") UUID pid) {
orderRepository.deleteByKeyOrderIdAndKeyProductId(oid, pid);
return ResponseEntity.noContent().build();
}

@PutMapping("orders/{id}")
public ResponseEntity<Order> updateOrder(@PathVariable("id") UUID oid, @RequestBody Order order) {
order.getKey().setOrderId(oid);
return ResponseEntity.ok(orderRepository.save(order));
@DeleteMapping("orders/{oid}")
public void deleteOrders(@PathVariable("oid") UUID oid) {
orderRepository.deleteByKeyOrderId(oid);
}

@PatchMapping("orders/{id}")
public ResponseEntity patchOrder(@PathVariable("id") UUID oid, @RequestBody Order order) {
return ResponseEntity.notFound().build();
// FIND

@GetMapping("orders/{oid}/{pid}")
public ProductNameAndPrice findOrder(@PathVariable("oid") UUID oid, @PathVariable("pid") UUID pid) {
return orderRepository.findByKeyOrderIdAndKeyProductId(oid, pid);
}

@GetMapping("orders/{oid}")
public List<ProductNameAndPrice> findOrders(@PathVariable("oid") UUID oid) {
return orderRepository.findByKeyOrderId(oid);
}

@GetMapping("orders")
public List<ProductNameAndPrice> findAll() {
return orderRepository.findAllProjectedBy();
}

}
27 changes: 11 additions & 16 deletions src/main/java/com/datastax/examples/order/OrderRepository.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package com.datastax.examples.order;

import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@RepositoryRestResource(collectionResourceRel = "orders", path = "orders")
@Repository
@RestResource(exported = false)
public interface OrderRepository extends CassandraRepository<Order, OrderPrimaryKey> {

@RestResource(exported = false)
void deleteByKeyOrderId(UUID orderId);
// DELETE

void deleteByKeyOrderId(UUID orderId);

@RestResource(exported = false)
void deleteByKeyOrderIdAndKeyProductId(UUID orderId, UUID productId);
void deleteByKeyOrderIdAndKeyProductId(UUID orderId, UUID productId);

@RestResource(path="name-and-price-only")
@Query("SELECT product_name, product_price FROM orders WHERE order_id = :orderId")
List<Order> findProductNamesAndPricesFromOrder(@Param("orderId") UUID orderId);
// FIND (all projected)

@RestResource(exported = false)
List<Order> findByKeyOrderId(UUID orderId);
ProductNameAndPrice findByKeyOrderIdAndKeyProductId(UUID orderId, UUID productId);

@RestResource(exported = false)
Order findByKeyOrderIdAndKeyProductId(UUID orderId, UUID productId);
List<ProductNameAndPrice> findByKeyOrderId(UUID orderId);

List<ProductNameAndPrice> findAllProjectedBy();
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.data.rest.core.config.Projection;

@Projection(name = "product-name-and-price", types = { Order.class })
public interface ProductNameAndPriceOnly {
public interface ProductNameAndPrice {
String getProductName();
Float getProductPrice();
}
2 changes: 0 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ server.port: 8081

management.server.port: 8082

spring.data.rest.basePath: /

spring.data.cassandra:
keyspace-name: ${ASTRA_DB_KEYSPACE}
username: ${ASTRA_DB_USERNAME}
Expand Down

0 comments on commit 535e624

Please sign in to comment.