Commit 8146480bb4eb3ed0ef817f72b7f66dceb4c8ca34
Committed by
GitHub
Merge pull request #501 from dmytro-landiak/master
rest client update
Showing
4 changed files
with
58 additions
and
4 deletions
@@ -82,7 +82,7 @@ public class CustomerController extends BaseController { | @@ -82,7 +82,7 @@ public class CustomerController extends BaseController { | ||
82 | 82 | ||
83 | @PreAuthorize("hasAuthority('TENANT_ADMIN')") | 83 | @PreAuthorize("hasAuthority('TENANT_ADMIN')") |
84 | @RequestMapping(value = "/customer", method = RequestMethod.POST) | 84 | @RequestMapping(value = "/customer", method = RequestMethod.POST) |
85 | - @ResponseBody | 85 | + @ResponseBody |
86 | public Customer saveCustomer(@RequestBody Customer customer) throws ThingsboardException { | 86 | public Customer saveCustomer(@RequestBody Customer customer) throws ThingsboardException { |
87 | try { | 87 | try { |
88 | customer.setTenantId(getCurrentUser().getTenantId()); | 88 | customer.setTenantId(getCurrentUser().getTenantId()); |
@@ -107,7 +107,7 @@ public class CustomerController extends BaseController { | @@ -107,7 +107,7 @@ public class CustomerController extends BaseController { | ||
107 | } | 107 | } |
108 | 108 | ||
109 | @PreAuthorize("hasAuthority('TENANT_ADMIN')") | 109 | @PreAuthorize("hasAuthority('TENANT_ADMIN')") |
110 | - @RequestMapping(value = "/customers", params = { "limit" }, method = RequestMethod.GET) | 110 | + @RequestMapping(value = "/customers", params = {"limit"}, method = RequestMethod.GET) |
111 | @ResponseBody | 111 | @ResponseBody |
112 | public TextPageData<Customer> getCustomers(@RequestParam int limit, | 112 | public TextPageData<Customer> getCustomers(@RequestParam int limit, |
113 | @RequestParam(required = false) String textSearch, | 113 | @RequestParam(required = false) String textSearch, |
@@ -122,4 +122,16 @@ public class CustomerController extends BaseController { | @@ -122,4 +122,16 @@ public class CustomerController extends BaseController { | ||
122 | } | 122 | } |
123 | } | 123 | } |
124 | 124 | ||
125 | + @PreAuthorize("hasAuthority('TENANT_ADMIN')") | ||
126 | + @RequestMapping(value = "/tenant/customers", params = {"customerTitle"}, method = RequestMethod.GET) | ||
127 | + @ResponseBody | ||
128 | + public Customer getTenantCustomer( | ||
129 | + @RequestParam String customerTitle) throws ThingsboardException { | ||
130 | + try { | ||
131 | + TenantId tenantId = getCurrentUser().getTenantId(); | ||
132 | + return checkNotNull(customerService.findCustomerByTenantIdAndTitle(tenantId, customerTitle)); | ||
133 | + } catch (Exception e) { | ||
134 | + throw handleException(e); | ||
135 | + } | ||
136 | + } | ||
125 | } | 137 | } |
@@ -22,20 +22,24 @@ import org.thingsboard.server.common.data.id.TenantId; | @@ -22,20 +22,24 @@ import org.thingsboard.server.common.data.id.TenantId; | ||
22 | import org.thingsboard.server.common.data.page.TextPageData; | 22 | import org.thingsboard.server.common.data.page.TextPageData; |
23 | import org.thingsboard.server.common.data.page.TextPageLink; | 23 | import org.thingsboard.server.common.data.page.TextPageLink; |
24 | 24 | ||
25 | +import java.util.Optional; | ||
26 | + | ||
25 | public interface CustomerService { | 27 | public interface CustomerService { |
26 | 28 | ||
27 | Customer findCustomerById(CustomerId customerId); | 29 | Customer findCustomerById(CustomerId customerId); |
28 | 30 | ||
31 | + Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title); | ||
32 | + | ||
29 | ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId); | 33 | ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId); |
30 | 34 | ||
31 | Customer saveCustomer(Customer customer); | 35 | Customer saveCustomer(Customer customer); |
32 | - | 36 | + |
33 | void deleteCustomer(CustomerId customerId); | 37 | void deleteCustomer(CustomerId customerId); |
34 | 38 | ||
35 | Customer findOrCreatePublicCustomer(TenantId tenantId); | 39 | Customer findOrCreatePublicCustomer(TenantId tenantId); |
36 | 40 | ||
37 | TextPageData<Customer> findCustomersByTenantId(TenantId tenantId, TextPageLink pageLink); | 41 | TextPageData<Customer> findCustomersByTenantId(TenantId tenantId, TextPageLink pageLink); |
38 | - | 42 | + |
39 | void deleteCustomersByTenantId(TenantId tenantId); | 43 | void deleteCustomersByTenantId(TenantId tenantId); |
40 | 44 | ||
41 | } | 45 | } |
@@ -52,6 +52,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom | @@ -52,6 +52,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom | ||
52 | 52 | ||
53 | private static final String PUBLIC_CUSTOMER_TITLE = "Public"; | 53 | private static final String PUBLIC_CUSTOMER_TITLE = "Public"; |
54 | public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; | 54 | public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; |
55 | + public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; | ||
55 | 56 | ||
56 | @Autowired | 57 | @Autowired |
57 | private CustomerDao customerDao; | 58 | private CustomerDao customerDao; |
@@ -79,6 +80,13 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom | @@ -79,6 +80,13 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom | ||
79 | } | 80 | } |
80 | 81 | ||
81 | @Override | 82 | @Override |
83 | + public Optional<Customer> findCustomerByTenantIdAndTitle(TenantId tenantId, String title) { | ||
84 | + log.trace("Executing findCustomerByTenantIdAndTitle [{}] [{}]", tenantId, title); | ||
85 | + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | ||
86 | + return customerDao.findCustomersByTenantIdAndTitle(tenantId.getId(), title); | ||
87 | + } | ||
88 | + | ||
89 | + @Override | ||
82 | public ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId) { | 90 | public ListenableFuture<Customer> findCustomerByIdAsync(CustomerId customerId) { |
83 | log.trace("Executing findCustomerByIdAsync [{}]", customerId); | 91 | log.trace("Executing findCustomerByIdAsync [{}]", customerId); |
84 | validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); | 92 | validateId(customerId, INCORRECT_CUSTOMER_ID + customerId); |
@@ -77,6 +77,36 @@ public class RestClient implements ClientHttpRequestInterceptor { | @@ -77,6 +77,36 @@ public class RestClient implements ClientHttpRequestInterceptor { | ||
77 | } | 77 | } |
78 | } | 78 | } |
79 | 79 | ||
80 | + public Optional<Customer> findCustomer(String title) { | ||
81 | + Map<String, String> params = new HashMap<String, String>(); | ||
82 | + params.put("customerTitle", title); | ||
83 | + try { | ||
84 | + ResponseEntity<Customer> customerEntity = restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle={customerTitle}", Customer.class, params); | ||
85 | + return Optional.of(customerEntity.getBody()); | ||
86 | + } catch (HttpClientErrorException exception) { | ||
87 | + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { | ||
88 | + return Optional.empty(); | ||
89 | + } else { | ||
90 | + throw exception; | ||
91 | + } | ||
92 | + } | ||
93 | + } | ||
94 | + | ||
95 | + public Optional<Asset> findAsset(String name) { | ||
96 | + Map<String, String> params = new HashMap<String, String>(); | ||
97 | + params.put("assetName", name); | ||
98 | + try { | ||
99 | + ResponseEntity<Asset> assetEntity = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, params); | ||
100 | + return Optional.of(assetEntity.getBody()); | ||
101 | + } catch (HttpClientErrorException exception) { | ||
102 | + if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { | ||
103 | + return Optional.empty(); | ||
104 | + } else { | ||
105 | + throw exception; | ||
106 | + } | ||
107 | + } | ||
108 | + } | ||
109 | + | ||
80 | public Customer createCustomer(String title) { | 110 | public Customer createCustomer(String title) { |
81 | Customer customer = new Customer(); | 111 | Customer customer = new Customer(); |
82 | customer.setTitle(title); | 112 | customer.setTitle(title); |