|
10 | 10 | @RESTful |
11 | 11 | public class ProductsController extends AppController { |
12 | 12 |
|
| 13 | + private ObjectMapper mapper = new ObjectMapper(); |
| 14 | + |
13 | 15 | public void index() { |
14 | | - try { |
15 | | - view("products", Product.findAll()); |
16 | | - render().contentType("application/json"); |
17 | | - } catch (Exception e) { |
18 | | - view("message", "There was an error.", "code", 200); |
19 | | - render("message"); |
20 | | - } |
| 16 | + try { |
| 17 | + view("products", Product.findAll()); |
| 18 | + render().contentType("application/json"); |
| 19 | + } catch (Exception e) { |
| 20 | + view("message", "There was an error.", "code", 200); |
| 21 | + render("message"); |
| 22 | + } |
21 | 23 | } |
22 | 24 |
|
23 | 25 | public void create() { |
24 | | - try { |
25 | | - Map payload = new ObjectMapper().readValue(getRequestString(), Map.class); |
26 | | - Product p = new Product(); |
27 | | - p.fromMap(payload); |
28 | | - p.saveIt(); |
29 | | - view("message", "Successfully saved product id " + p.get("id"), "code", 200); |
30 | | - render("message"); |
31 | | - } catch (Exception e) { |
32 | | - view("message", "There was an error.", "code", 200); |
33 | | - render("message"); |
34 | | - } |
| 26 | + try { |
| 27 | + Map payload = mapper.readValue(getRequestString(), Map.class); |
| 28 | + Product p = new Product(); |
| 29 | + p.fromMap(payload); |
| 30 | + p.saveIt(); |
| 31 | + view("message", "Successfully saved product id " + p.get("id"), "code", 200); |
| 32 | + render("message"); |
| 33 | + } catch (Exception e) { |
| 34 | + view("message", "There was an error.", "code", 200); |
| 35 | + render("message"); |
| 36 | + } |
35 | 37 | } |
36 | 38 |
|
37 | 39 | public void update() { |
38 | | - try { |
39 | | - Map payload = new ObjectMapper().readValue(getRequestString(), Map.class); |
40 | | - String id = getId(); |
41 | | - Product p = Product.findById(id); |
42 | | - if (p == null) { |
43 | | - view("message", "Product id " + id + " not found.", "code", 200); |
44 | | - render("message"); |
45 | | - return; |
| 40 | + try { |
| 41 | + Map payload = mapper.readValue(getRequestString(), Map.class); |
| 42 | + String id = getId(); |
| 43 | + Product p = Product.findById(id); |
| 44 | + if (p == null) { |
| 45 | + view("message", "Product id " + id + " not found.", "code", 200); |
| 46 | + render("message"); |
| 47 | + return; |
| 48 | + } |
| 49 | + p.fromMap(payload); |
| 50 | + p.saveIt(); |
| 51 | + view("message", "Successfully updated product id " + id, "code", 200); |
| 52 | + render("message"); |
| 53 | + } catch (Exception e) { |
| 54 | + view("message", "There was an error.", "code", 200); |
| 55 | + render("message"); |
46 | 56 | } |
47 | | - p.fromMap(payload); |
48 | | - p.saveIt(); |
49 | | - view("message", "Successfully updated product id " + id, "code", 200); |
50 | | - render("message"); |
51 | | - } catch (Exception e) { |
52 | | - view("message", "There was an error.", "code", 200); |
53 | | - render("message"); |
54 | | - } |
55 | 57 | } |
56 | 58 |
|
57 | 59 | public void show() { |
58 | | - try { |
59 | | - String id = getId(); |
60 | | - Product p = Product.findById(id); |
61 | | - if (p == null) { |
62 | | - view("message", "Product id " + id + " not found.", "code", 200); |
63 | | - render("message"); |
64 | | - return; |
| 60 | + try { |
| 61 | + String id = getId(); |
| 62 | + Product p = Product.findById(id); |
| 63 | + if (p == null) { |
| 64 | + view("message", "Product id " + id + " not found.", "code", 200); |
| 65 | + render("message"); |
| 66 | + return; |
| 67 | + } |
| 68 | + view("product", p); |
| 69 | + render("_product"); |
| 70 | + } catch (Exception e) { |
| 71 | + view("message", "There was an error.", "code", 200); |
| 72 | + render("message"); |
65 | 73 | } |
66 | | - view("product", p); |
67 | | - render("_product"); |
68 | | - } catch (Exception e) { |
69 | | - view("message", "There was an error.", "code", 200); |
70 | | - render("message"); |
71 | | - } |
72 | 74 | } |
73 | 75 |
|
74 | 76 | public void destroy() { |
75 | | - try { |
76 | | - String id = getId(); |
77 | | - Product p = Product.findById(id); |
78 | | - if (p == null) { |
79 | | - view("message", "Product id " + id + " not found.", "code", 200); |
80 | | - render("message"); |
81 | | - return; |
| 77 | + try { |
| 78 | + String id = getId(); |
| 79 | + Product p = Product.findById(id); |
| 80 | + if (p == null) { |
| 81 | + view("message", "Product id " + id + " not found.", "code", 200); |
| 82 | + render("message"); |
| 83 | + return; |
| 84 | + } |
| 85 | + p.delete(); |
| 86 | + view("message", "Successfully deleted product id " + id, "code", 200); |
| 87 | + render("message"); |
| 88 | + } catch (Exception e) { |
| 89 | + view("message", "There was an error.", "code", 200); |
| 90 | + render("message"); |
82 | 91 | } |
83 | | - p.delete(); |
84 | | - view("message", "Successfully deleted product id " + id, "code", 200); |
85 | | - render("message"); |
86 | | - } catch (Exception e) { |
87 | | - view("message", "There was an error.", "code", 200); |
88 | | - render("message"); |
89 | | - } |
90 | 92 | } |
91 | 93 |
|
92 | 94 | @Override |
93 | 95 | protected String getContentType() { |
94 | | - return "application/json"; |
| 96 | + return "application/json"; |
95 | 97 | } |
96 | 98 |
|
97 | 99 | @Override |
98 | 100 | protected String getLayout() { |
99 | | - return null; |
| 101 | + return null; |
100 | 102 | } |
101 | 103 | } |
0 commit comments