You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TBCustomerResource.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package vn.azteam.tpf.web.rest;
  2. import com.codahale.metrics.annotation.Timed;
  3. import vn.azteam.tpf.service.TBCustomerService;
  4. import vn.azteam.tpf.service.TBRoleService;
  5. import vn.azteam.tpf.service.UserService;
  6. import vn.azteam.tpf.service.dto.TBRoleDTO;
  7. import vn.azteam.tpf.service.dto.UserDTO;
  8. import vn.azteam.tpf.service.util.UserRoleUtil;
  9. import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;
  10. import vn.azteam.tpf.web.rest.errors.ForbiddenException;
  11. import vn.azteam.tpf.web.rest.util.HeaderUtil;
  12. import vn.azteam.tpf.web.rest.util.PaginationUtil;
  13. import vn.azteam.tpf.service.dto.TBCustomerDTO;
  14. import vn.azteam.tpf.service.dto.TBCustomerCriteria;
  15. import vn.azteam.tpf.service.TBCustomerQueryService;
  16. import io.github.jhipster.web.util.ResponseUtil;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.data.domain.Page;
  20. import org.springframework.data.domain.Pageable;
  21. import org.springframework.http.HttpHeaders;
  22. import org.springframework.http.ResponseEntity;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.net.URI;
  26. import java.net.URISyntaxException;
  27. import java.nio.charset.StandardCharsets;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. /**
  31. * REST controller for managing TBCustomer.
  32. */
  33. @RestController
  34. @RequestMapping("/api")
  35. public class TBCustomerResource {
  36. private final Logger log = LoggerFactory.getLogger(TBCustomerResource.class);
  37. private static final String ENTITY_NAME = "tBCustomer";
  38. private final TBCustomerService tBCustomerService;
  39. private final TBCustomerQueryService tBCustomerQueryService;
  40. private final TBRoleService tbRoleService;
  41. private final UserService userService;
  42. private final UserRoleUtil userRoleUtil;
  43. public TBCustomerResource(TBCustomerService tBCustomerService, TBCustomerQueryService tBCustomerQueryService, TBRoleService tbRoleService, UserService userService, UserRoleUtil userRoleUtil) {
  44. this.tBCustomerService = tBCustomerService;
  45. this.tBCustomerQueryService = tBCustomerQueryService;
  46. this.tbRoleService = tbRoleService;
  47. this.userService = userService;
  48. this.userRoleUtil = userRoleUtil;
  49. }
  50. /**
  51. * POST /tb-customers : Create a new tBCustomer.
  52. *
  53. * @param tBCustomerDTO the tBCustomerDTO to create
  54. * @return the ResponseEntity with status 201 (Created) and with body the new tBCustomerDTO, or with status 400 (Bad Request) if the tBCustomer has already an ID
  55. * @throws URISyntaxException if the Location URI syntax is incorrect
  56. */
  57. @PostMapping("/tb-customers")
  58. @Timed
  59. public ResponseEntity<TBCustomerDTO> createTBCustomer(@RequestBody TBCustomerDTO tBCustomerDTO) throws URISyntaxException {
  60. log.debug("REST request to save TBCustomer : {}", tBCustomerDTO);
  61. if (tBCustomerDTO.getId() != null) {
  62. throw new BadRequestAlertException("A new tBCustomer cannot already have an ID", ENTITY_NAME, "idexists");
  63. }
  64. tBCustomerDTO.setApiKey(this.createApiKey());
  65. TBCustomerDTO result = tBCustomerQueryService.createCustomer(tBCustomerDTO);
  66. return ResponseEntity.created(new URI("/api/tb-customers/" + result.getId()))
  67. .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
  68. .body(result);
  69. }
  70. public String createApiKey(){
  71. return UUID.randomUUID().toString();
  72. }
  73. /**
  74. * PUT /tb-customers : Updates an existing tBCustomer.
  75. *
  76. * @param tBCustomerDTO the tBCustomerDTO to update
  77. * @return the ResponseEntity with status 200 (OK) and with body the updated tBCustomerDTO,
  78. * or with status 400 (Bad Request) if the tBCustomerDTO is not valid,
  79. * or with status 500 (Internal Server Error) if the tBCustomerDTO couldn't be updated
  80. * @throws URISyntaxException if the Location URI syntax is incorrect
  81. */
  82. @PutMapping("/tb-customers")
  83. @Timed
  84. public ResponseEntity<TBCustomerDTO> updateTBCustomer(@RequestBody TBCustomerDTO tBCustomerDTO) throws URISyntaxException {
  85. log.debug("REST request to update TBCustomer : {}", tBCustomerDTO);
  86. if (tBCustomerDTO.getId() == null) {
  87. throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
  88. }
  89. TBCustomerDTO result = tBCustomerQueryService.updateCustomer(tBCustomerDTO);
  90. return ResponseEntity.ok()
  91. .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, tBCustomerDTO.getId().toString()))
  92. .body(result);
  93. }
  94. @PutMapping("/tb-customers-api-key/{customerId}")
  95. @Timed
  96. public ResponseEntity<TBCustomerDTO> updateTBCustomerApiKey(@PathVariable Long customerId) {
  97. log.debug("REST request to update Api-Key TBCustomer");
  98. if (customerId == null){
  99. throw new BadRequestAlertException("Invalid customer", ENTITY_NAME, "customeridnull");
  100. }
  101. Optional<TBCustomerDTO> tbCustomerDTOOptional = tBCustomerService.findOne(customerId);
  102. if (!tbCustomerDTOOptional.isPresent()){
  103. throw new BadRequestAlertException("Customer not found", ENTITY_NAME, "customernotfound");
  104. }
  105. UserDTO currentUser = userService.getCurrentUserDTO().get();
  106. // if(currentUser.getCustomerId() != null && !currentUser.getCustomerId().equals(customerId)){
  107. // throw new ForbiddenException();
  108. // }
  109. if (currentUser.getCustomerId() != null){
  110. throw new ForbiddenException();
  111. }
  112. String apiKeyNew = this.createApiKey();
  113. TBCustomerDTO tbCustomerDTO = tbCustomerDTOOptional.get();
  114. tbCustomerDTO.setApiKey(apiKeyNew);
  115. TBCustomerDTO result = tBCustomerQueryService.updateCustomer(tbCustomerDTO);
  116. return ResponseEntity.ok()
  117. .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, tbCustomerDTO.getId().toString()))
  118. .body(result);
  119. }
  120. /**
  121. * GET /tb-customers : get all the tBCustomers.
  122. *
  123. * @param pageable the pagination information
  124. * @param criteria the criterias which the requested entities should match
  125. * @return the ResponseEntity with status 200 (OK) and the list of tBCustomers in body
  126. */
  127. @GetMapping("/tb-customers")
  128. @Timed
  129. public ResponseEntity<List<TBCustomerDTO>> getAllTBCustomers(TBCustomerCriteria criteria, Pageable pageable) {
  130. log.debug("REST request to get TBCustomers by criteria: {}", criteria);
  131. Page<TBCustomerDTO> page = tBCustomerQueryService.findByCriteria(criteria, pageable);
  132. HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-customers");
  133. return ResponseEntity.ok().headers(headers).body(page.getContent());
  134. }
  135. /**
  136. * GET /tb-customers : get all the tBCustomers.
  137. *
  138. * @param pageable the pagination information
  139. * @param criteria the criterias which the requested entities should match
  140. * @return the ResponseEntity with status 200 (OK) and the list of tBCustomers in body
  141. */
  142. @GetMapping("/tb-customers-dropdown-list/{roleId}")
  143. @Timed
  144. public ResponseEntity<List<TBCustomerDTO>> getAllTBCustomersByRoleId(@PathVariable Long roleId,TBCustomerCriteria criteria, Pageable pageable) {
  145. log.debug("REST request to get TBCustomers by criteria: {}", criteria);
  146. UserDTO currentUser = userService.getCurrentUserDTO().get();
  147. TBRoleDTO roleDTO = tbRoleService.findOne(roleId).get();
  148. Page<TBCustomerDTO> page = tBCustomerQueryService.findByCriteria(criteria, pageable);
  149. List<TBCustomerDTO> result = page.getContent();
  150. if(currentUser.getCustomerId() != null){
  151. result = page.getContent().stream()
  152. .filter(item -> item.getId().equals(currentUser.getCustomerId()))
  153. .collect(Collectors.toList());
  154. } else {
  155. if (!roleDTO.getIsAllCustomer()) {
  156. result = page.getContent().stream()
  157. .filter(item -> item.getId().equals(roleDTO.getTbCustomerId()))
  158. .collect(Collectors.toList());
  159. }
  160. }
  161. HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-customers-dropdown-list");
  162. return ResponseEntity.ok().headers(headers).body(result);
  163. }
  164. /**
  165. * GET /tb-customers/count : count all the tBCustomers.
  166. *
  167. * @param criteria the criterias which the requested entities should match
  168. * @return the ResponseEntity with status 200 (OK) and the count in body
  169. */
  170. @GetMapping("/tb-customers/count")
  171. @Timed
  172. public ResponseEntity<Long> countTBCustomers(TBCustomerCriteria criteria) {
  173. log.debug("REST request to count TBCustomers by criteria: {}", criteria);
  174. return ResponseEntity.ok().body(tBCustomerQueryService.countByCriteria(criteria));
  175. }
  176. /**
  177. * GET /tb-customers/:id : get the "id" tBCustomer.
  178. *
  179. * @param id the id of the tBCustomerDTO to retrieve
  180. * @return the ResponseEntity with status 200 (OK) and with body the tBCustomerDTO, or with status 404 (Not Found)
  181. */
  182. @GetMapping("/tb-customers/{id}")
  183. @Timed
  184. public ResponseEntity<TBCustomerDTO> getTBCustomer(@PathVariable Long id) {
  185. log.debug("REST request to get TBCustomer : {}", id);
  186. Optional<TBCustomerDTO> tBCustomerDTO = tBCustomerService.findOne(id);
  187. return ResponseUtil.wrapOrNotFound(tBCustomerDTO);
  188. }
  189. /**
  190. * GET /tb-customers-current-user/
  191. *
  192. * @return the ResponseEntity with status 200 (OK) and with body the tBCustomerDTO, or with status 404 (Not Found)
  193. */
  194. @GetMapping("/tb-customers-current-user")
  195. @Timed
  196. public ResponseEntity<TBCustomerDTO> getTBCustomerByCurrentUser() {
  197. UserDTO currentUser = userService.getCurrentUserDTO().get();
  198. if(currentUser.getCustomerId() != null) {
  199. Optional<TBCustomerDTO> tBCustomerDTO = tBCustomerService.findOne(currentUser.getCustomerId());
  200. return ResponseUtil.wrapOrNotFound(tBCustomerDTO);
  201. }
  202. return null;
  203. }
  204. /**
  205. * DELETE /tb-customers/:id : delete the "id" tBCustomer.
  206. *
  207. * @param id the id of the tBCustomerDTO to delete
  208. * @return the ResponseEntity with status 200 (OK)
  209. */
  210. @DeleteMapping("/tb-customers/{id}")
  211. @Timed
  212. public ResponseEntity<Void> deleteTBCustomer(@PathVariable Long id) {
  213. log.debug("REST request to delete TBCustomer : {}", id);
  214. tBCustomerService.delete(id);
  215. return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
  216. }
  217. /**
  218. * SEARCH /_search/tb-customers?query=:query : search for the tBCustomer corresponding
  219. * to the query.
  220. *
  221. * @param query the query of the tBCustomer search
  222. * @param pageable the pagination information
  223. * @return the result of the search
  224. */
  225. @GetMapping("/_search/tb-customers")
  226. @Timed
  227. public ResponseEntity<List<TBCustomerDTO>> searchTBCustomers(@RequestParam String query, @RequestParam String status, Pageable pageable) {
  228. log.debug("REST request to search for a page of TBCustomers for query {}", query);
  229. UserDTO currentUser = userService.getCurrentUserDTO().get();
  230. if (currentUser.getCustomerId() != null) {
  231. Page<TBCustomerDTO> page;
  232. if((query != null && !query.isEmpty()) | (status != null && !status.isEmpty())) {
  233. page = tBCustomerQueryService.searchCustomer(query, status, pageable);
  234. } else {
  235. page = tBCustomerService.search(query, pageable);
  236. }
  237. HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-customers");
  238. return ResponseEntity.ok().headers(headers).body(page.getContent());
  239. }
  240. return null;
  241. }
  242. }