-
Notifications
You must be signed in to change notification settings - Fork 1
/
CartAPIResponse.java
55 lines (46 loc) · 2.05 KB
/
CartAPIResponse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package api.model;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.JsonObject;
public class CartAPIResponse extends APIResponse {
public CartAPIResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
super(request, response);
}
/**
* Serialize a JSON response for retrieving a collection of Products
* in the shopping cart. Outputs the given Products list in the "cart"
* field of the API response. Also, add the length of the results.
*
* @param products
*/
public void serializeGetCart(Products products) { serializeGetCart(products, true); }
public JsonObject serializeGetCart(Products products, boolean terminate) {
JsonObject response = newResponse();
JsonObject cart = gson.toJsonTree(products).getAsJsonObject();
response.addProperty("length", products.getLength());
response.add("cart", cart);
if (terminate) endResponse(response);
return response;
}
/**
* Serialize a JSON response for the collection of Products added to or removed
* from the shopping cart. Outputs the given Products list in the "added" or "removed"
* field of the API response. Also, add the length of the results
* and the search parameters used to filter the results.
*
* @param products
*/
public void serializeAddToCart(Products products) { serializeCartUpdate(products, "added", true); }
public void serializeRemovedFromCart(Products products) { serializeCartUpdate(products, "removed", true); }
public JsonObject serializeCartUpdate(Products products, String label, boolean terminate) {
JsonObject response = newResponse();
JsonObject search = ((ProductFilter)session.getAttribute("filter")).toJson();
JsonObject items = gson.toJsonTree(products).getAsJsonObject();
response.addProperty("length", products.getLength());
response.add("search", search);
response.add(label, items);
if (terminate) endResponse(response);
return response;
}
}