Commit f44527c9 by Patryk Czarnik

Wersja MultiFormatowa

parent 974a6bf0
......@@ -11,9 +11,9 @@ import java.math.BigDecimal;
import java.util.List;
@Path("/products")
@Produces("application/json")
public class RProducts {
@GET
@Produces({"application/json", "application/xml", "text/plain;charset=UTF-8"})
public List<Product> allProducts() throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
......@@ -21,8 +21,24 @@ public class RProducts {
}
}
// Może też być tak, że kilka metod działa pod tym samym adresem, ale służą one do tworzenia odpowiedzi w różnych formatach.
// Przykład: tworzenie HTML w oddzielnej metodzie
@GET
@Produces("text/html;charset=UTF-8")
public String readAllHTML() throws DBException {
List<Product> products = allProducts();
StringBuilder txt = new StringBuilder("<!DOCTYPE html>\n<html><body>\n");
txt.append("<h1>Lista produktów</h1>\n");
for(Product product : products) {
txt.append(product.toHtml()).append('\n');
}
txt.append("</body></html>");
return txt.toString();
}
@GET
@Path("/{id}")
@Produces({"application/json", "application/xml", "text/plain;charset=UTF-8"})
// przykładowo /api/products/3
public Product oneProduct(@PathParam("id") int productId) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
......@@ -31,8 +47,17 @@ public class RProducts {
}
}
@GET
@Produces("text/html;charset=UTF-8")
@Path("/{id}")
public String readOneHTML(@PathParam("id") int productId) throws DBException, RecordNotFound {
Product product = oneProduct(productId);
return "<!DOCTYPE html>\n<html><body>" + product.toHtml() + "</body></html>";
}
@POST
@Consumes("application/json")
@Consumes({"application/json", "application/xml"})
public void saveProduct(Product product) throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
......@@ -45,13 +70,14 @@ public class RProducts {
@GET
@Path("/{id}/price")
// przykładowo /api/products/3/price
@Produces({"application/json", "text/plain"})
public BigDecimal getPrice(@PathParam("id") int productId) throws DBException, RecordNotFound {
return oneProduct(productId).getPrice();
}
@PUT
@Path("/{id}/price")
@Consumes("application/json")
@Consumes({"application/json", "text/plain"})
public void setPrice(@PathParam("id") int productId, BigDecimal newPrice) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
......
......@@ -5,7 +5,9 @@ import sklep.db.DBConnection;
import sklep.db.DBException;
import sklep.db.ProductDAO;
import sklep.db.RecordNotFound;
import sklep.model.Price;
import sklep.model.Product;
import sklep.model.ProductList;
import java.math.BigDecimal;
import java.util.List;
......@@ -15,10 +17,10 @@ import java.util.List;
@Consumes("application/xml")
public class RProductsXml {
@GET
public List<Product> allProducts() throws DBException {
public ProductList allProducts() throws DBException {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
return productDAO.readAll();
return new ProductList(productDAO.readAll());
}
}
......@@ -43,17 +45,17 @@ public class RProductsXml {
@GET
@Path("/{id}/price")
public BigDecimal getPrice(@PathParam("id") int productId) throws DBException, RecordNotFound {
return oneProduct(productId).getPrice();
public Price getPrice(@PathParam("id") int productId) throws DBException, RecordNotFound {
return new Price(oneProduct(productId).getPrice());
}
@PUT
@Path("/{id}/price")
public void setPrice(@PathParam("id") int productId, BigDecimal newPrice) throws DBException, RecordNotFound {
public void setPrice(@PathParam("id") int productId, Price newPrice) throws DBException, RecordNotFound {
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
Product product = productDAO.findById(productId);
product.setPrice(newPrice);
product.setPrice(newPrice.getValue());
productDAO.save(product);
db.commit();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment