-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up REST API to conform to test harness conventions
- Loading branch information
1 parent
1c5725d
commit 8aacfca
Showing
12 changed files
with
460 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ | |
# | ||
# Author: Nathan VanBenschoten ([email protected]) | ||
|
||
ifneq ($(ADDR),) | ||
ADDRFLAG = -PappArgs="['-addr', '$(ADDR)']" | ||
endif | ||
|
||
.PHONY: start | ||
start: | ||
@gradle run | ||
@gradle run $(ADDRFLAG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 34 additions & 35 deletions
69
java/hibernate/src/main/java/com/cockroachlabs/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,47 @@ | ||
package com.cockroachlabs; | ||
|
||
import com.cockroachlabs.model.Customer; | ||
import com.cockroachlabs.model.Order; | ||
import com.cockroachlabs.model.Product; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.cfg.Configuration; | ||
import org.hibernate.service.ServiceRegistry; | ||
|
||
import java.math.BigDecimal; | ||
import com.beust.jcommander.JCommander; | ||
import com.beust.jcommander.Parameter; | ||
import com.cockroachlabs.services.CustomerService; | ||
import com.cockroachlabs.services.OrderService; | ||
import com.cockroachlabs.services.PingService; | ||
import com.cockroachlabs.services.ProductService; | ||
import com.cockroachlabs.util.SessionUtil; | ||
import org.glassfish.jersey.netty.httpserver.NettyHttpContainerProvider; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
|
||
import javax.ws.rs.core.UriBuilder; | ||
import java.net.URI; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
try (SessionFactory sf = buildSessionFactory()) { | ||
Session session = sf.getCurrentSession(); | ||
|
||
Customer c = new Customer(); | ||
c.setName("joe"); | ||
|
||
Order o = new Order(); | ||
o.setCustomer(c); | ||
o.setSubtotal(new BigDecimal(100)); | ||
@Parameter(names = "-addr", description = "the address of the database") | ||
private String dbAddr; | ||
|
||
session.beginTransaction(); | ||
session.save(c); | ||
session.save(o); | ||
session.getTransaction().commit(); | ||
} | ||
public static void main(String[] args) { | ||
Application app = new Application(); | ||
new JCommander(app, args); | ||
app.run(); | ||
} | ||
|
||
private static SessionFactory buildSessionFactory() { | ||
Configuration configuration = new Configuration(); | ||
configuration.configure("hibernate.cfg.xml"); | ||
configuration.addAnnotatedClass(Customer.class); | ||
configuration.addAnnotatedClass(Order.class); | ||
configuration.addAnnotatedClass(Product.class); | ||
private void run() { | ||
initHibernate(); | ||
initHTTPServer(); | ||
} | ||
|
||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() | ||
.applySettings(configuration.getProperties()) | ||
.build(); | ||
private void initHibernate() { | ||
SessionUtil.init(dbAddr); | ||
} | ||
|
||
return configuration.buildSessionFactory(serviceRegistry); | ||
private void initHTTPServer() { | ||
URI baseUri = UriBuilder.fromUri("http://localhost/").port(6543).build(); | ||
ResourceConfig resourceConfig = new ResourceConfig( | ||
PingService.class, | ||
CustomerService.class, | ||
ProductService.class, | ||
OrderService.class | ||
); | ||
NettyHttpContainerProvider.createServer(baseUri, resourceConfig, true); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
java/hibernate/src/main/java/com/cockroachlabs/services/CustomerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.cockroachlabs.services; | ||
|
||
import com.cockroachlabs.model.Customer; | ||
import com.cockroachlabs.util.SessionUtil; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.hibernate.Session; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.query.Query; | ||
|
||
import javax.ws.rs.*; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Path("/customer") | ||
public class CustomerService { | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@GET | ||
@Produces("application/json") | ||
public String getCustomers() { | ||
try (Session session = SessionUtil.getSession()) { | ||
Query query = session.createQuery("from Customer"); | ||
List customers = query.list(); | ||
return mapper.writeValueAsString(customers); | ||
} catch (JsonProcessingException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@POST | ||
@Produces("application/json") | ||
public String createCustomer(String body) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Customer newCustomer = mapper.readValue(body, Customer.class); | ||
session.save(newCustomer); | ||
|
||
return mapper.writeValueAsString(newCustomer); | ||
} catch (IOException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/{customerID}") | ||
@Produces("application/json") | ||
public String getCustomer(@PathParam("customerID") long customerID) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Customer customer = session.get(Customer.class, customerID); | ||
if (customer == null) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return mapper.writeValueAsString(customer); | ||
} catch (JsonProcessingException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@PUT | ||
@Path("/{customerID}") | ||
@Produces("application/json") | ||
public String updateCustomer(@PathParam("customerID") long customerID, String body) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Customer updateInfo = mapper.readValue(body, Customer.class); | ||
updateInfo.setId(customerID); | ||
|
||
Customer updatedCustomer = (Customer) session.merge(updateInfo); | ||
return mapper.writeValueAsString(updatedCustomer); | ||
} catch (IOException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@DELETE | ||
@Path("/{customerID}") | ||
@Produces("text/plain") | ||
public String deleteCustomer(@PathParam("customerID") long customerID) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Transaction tx = session.beginTransaction(); | ||
|
||
Query deleteReferencing = session.createQuery("delete from Order where customer_id = :id"); | ||
deleteReferencing.setParameter("id", customerID); | ||
deleteReferencing.executeUpdate(); | ||
|
||
Query deleteCustomer = session.createQuery("delete from Customer where id = :id"); | ||
deleteCustomer.setParameter("id", customerID); | ||
|
||
int rowCount = deleteCustomer.executeUpdate(); | ||
if (rowCount == 0) { | ||
tx.rollback(); | ||
throw new NotFoundException(); | ||
} | ||
tx.commit(); | ||
return "ok"; | ||
} | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
java/hibernate/src/main/java/com/cockroachlabs/services/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.cockroachlabs.services; | ||
|
||
import com.cockroachlabs.model.Order; | ||
import com.cockroachlabs.util.SessionUtil; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.hibernate.Session; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.query.Query; | ||
|
||
import javax.ws.rs.*; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Path("/order") | ||
public class OrderService { | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@GET | ||
@Produces("application/json") | ||
public String getOrders() { | ||
try (Session session = SessionUtil.getSession()) { | ||
Query query = session.createQuery("from Order"); | ||
List orders = query.list(); | ||
return mapper.writeValueAsString(orders); | ||
} catch (JsonProcessingException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@POST | ||
@Produces("application/json") | ||
public String createOrder(String body) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Order newOrder = mapper.readValue(body, Order.class); | ||
session.save(newOrder); | ||
|
||
return mapper.writeValueAsString(newOrder); | ||
} catch (IOException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/{orderID}") | ||
@Produces("application/json") | ||
public String getOrder(@PathParam("orderID") long orderID) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Order order = session.get(Order.class, orderID); | ||
if (order == null) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return mapper.writeValueAsString(order); | ||
} catch (JsonProcessingException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@PUT | ||
@Path("/{orderID}") | ||
@Produces("application/json") | ||
public String updateOrder(@PathParam("orderID") long orderID, String body) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Order updateInfo = mapper.readValue(body, Order.class); | ||
updateInfo.setId(orderID); | ||
|
||
Order updatedOrder = (Order) session.merge(updateInfo); | ||
return mapper.writeValueAsString(updatedOrder); | ||
} catch (IOException e) { | ||
return e.toString(); | ||
} | ||
} | ||
|
||
@DELETE | ||
@Path("/{orderID}") | ||
@Produces("text/plain") | ||
public String deleteOrder(@PathParam("orderID") long orderID) { | ||
try (Session session = SessionUtil.getSession()) { | ||
Transaction tx = session.beginTransaction(); | ||
|
||
Query deleteReferencing = session.createQuery("delete from Order where order_id = :id"); | ||
deleteReferencing.setParameter("id", orderID); | ||
deleteReferencing.executeUpdate(); | ||
|
||
Query deleteOrder = session.createQuery("delete from Order where id = :id"); | ||
deleteOrder.setParameter("id", orderID); | ||
|
||
int rowCount = deleteOrder.executeUpdate(); | ||
if (rowCount == 0) { | ||
tx.rollback(); | ||
throw new NotFoundException(); | ||
} | ||
tx.commit(); | ||
return "ok"; | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
java/hibernate/src/main/java/com/cockroachlabs/services/PingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.cockroachlabs.services; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
|
||
@Path("/ping") | ||
public class PingService { | ||
|
||
@GET | ||
@Produces("text/plain") | ||
public String ping() { | ||
return "pong"; | ||
} | ||
|
||
} |
Oops, something went wrong.