1 package com.srv4pos.server.api.restaurant;
2
3 import com.srv4pos.server.api.infrastructure.HttpMethod;
4 import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
5 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
6 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
7 import com.srv4pos.server.api.infrastructure.http.Transport;
8 import com.srv4pos.server.api.infrastructure.request.StringResponse;
9 import com.srv4pos.server.api.seller.PublicSellerInfo;
10 import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
11
12 import java.net.URL;
13 import java.util.HashMap;
14 import java.util.List;
15
16 import static com.srv4pos.server.api.infrastructure.EmptyConnectionWriter.EMPTY_CONNECTION_WRITER;
17 import static com.srv4pos.server.api.infrastructure.http.HttpHelper.CONTENT_TYPE_APPLICATION_JSON;
18 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
19 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
20 import static java.lang.String.format;
21
22
23
24
25
26
27 public class RestaurantService {
28 private URL url;
29 private Credentials credentials;
30 private Transport transport;
31
32
33
34
35
36
37
38
39 public RestaurantService(URL url, Credentials credentials, Transport transport) {
40 this.url = url;
41 this.credentials = credentials;
42 this.transport = transport;
43 }
44
45
46
47
48
49
50
51
52
53
54 public RestaurantOverviewInfo get(String sellerId, Integer version) {
55 StringResponse response = requestString(
56 format("/restaurants-overview/%s", sellerId),
57 HttpMethod.GET,
58 url,
59 credentials.toAuthentication(),
60 HttpHelper.integerToETag(version),
61 null,
62 EMPTY_CONNECTION_WRITER,
63 CONTENT_TYPE_APPLICATION_JSON,
64 transport
65 );
66 return response == null ? null : RestaurantOverviewInfo.fromJsonToRestaurantOverviewInfo(response.getContent());
67 }
68
69
70
71
72
73
74
75
76
77
78 public void put(int version, String sellerId, RestaurantInfo restaurantInfo) {
79 requestVoid(
80 format("restaurants/%s", sellerId),
81 HttpMethod.PUT,
82 url,
83 credentials.toAuthentication(),
84 HttpHelper.integerToETag(version),
85 null,
86 new StringConnectionWriter(restaurantInfo.toJson()),
87 CONTENT_TYPE_APPLICATION_JSON,
88 transport
89 );
90 }
91
92
93
94
95
96
97 public void remove(String sellerId) {
98 requestVoid(
99 format("restaurants/%s", sellerId),
100 HttpMethod.DELETE,
101 url,
102 credentials.toAuthentication(),
103 null,
104 null,
105 EMPTY_CONNECTION_WRITER,
106 CONTENT_TYPE_APPLICATION_JSON,
107 transport);
108 }
109
110
111
112
113
114
115
116
117 public List<RestaurantListInfo> list() {
118 return list(null, null, null, null, null);
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133 public List<RestaurantListInfo> list(Integer firstResult,
134 Integer maxResults,
135 String like,
136 PublicSellerInfo.Fields orderBy,
137 Boolean orderDesc) {
138 HashMap<String, String> params = new HashMap<String, String>();
139 if (orderBy != null) {
140 params.put("orderBy", orderBy.name());
141 }
142 if (orderDesc != null) {
143 params.put("orderDesc", orderDesc.toString());
144 }
145 if (firstResult != null) {
146 params.put("firstResult", Integer.toString(firstResult));
147 }
148 if (maxResults != null) {
149 params.put("maxResults", Integer.toString(maxResults));
150 }
151 if (like != null) {
152 params.put("like", like);
153 }
154
155 StringResponse response = requestString(
156 "/restaurants",
157 HttpMethod.GET,
158 url,
159 credentials.toAuthentication(),
160 null,
161 params,
162 EMPTY_CONNECTION_WRITER,
163 CONTENT_TYPE_APPLICATION_JSON,
164 transport
165 );
166 return response == null ? null : (List<RestaurantListInfo>) RestaurantListInfo.fromJsonArrayToRestaurantListInfoes(response.getContent());
167 }
168 }