/src/Backend/OrderService/src/main/java/smpl/ordering/controllers/DealerController.java

https://github.com/Microsoft/PartsUnlimitedMRP · Java · 201 lines · 150 code · 15 blank · 36 comment · 25 complexity · 20b49eb9d21d05b508c46bd2d6a7054e MD5 · raw file

  1. package smpl.ordering.controllers;
  2. import com.microsoft.applicationinsights.TelemetryClient;
  3. import org.springframework.http.HttpHeaders;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.http.ResponseEntity;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import smpl.ordering.OrderingInitializer;
  12. import smpl.ordering.Utility;
  13. import smpl.ordering.models.DealerInfo;
  14. import smpl.ordering.repositories.DealersRepository;
  15. import smpl.ordering.repositories.RepositoryFactory;
  16. import java.util.List;
  17. @Controller
  18. @RequestMapping("/dealers")
  19. public class DealerController
  20. {
  21. /**
  22. * Gets a list of available dealers.
  23. *
  24. * @return An HttpResponse containing a list of dealers.
  25. */
  26. @RequestMapping(method = RequestMethod.GET)
  27. public ResponseEntity getDealers()
  28. {
  29. //Fix this line in Application Performance Monitoring HOL from 1000 to 1
  30. int numMongoDBCalls = 100000;
  31. try
  32. {
  33. int count = 0;
  34. List<DealerInfo> dealers = getRepository().getDealers();
  35. while(count < numMongoDBCalls - 1)
  36. {
  37. dealers = getRepository().getDealers();
  38. count++;
  39. }
  40. if (dealers == null || dealers.size() == 0)
  41. {
  42. return new ResponseEntity<List<DealerInfo>>(HttpStatus.NOT_FOUND);
  43. }
  44. else
  45. {
  46. return new ResponseEntity<>(dealers, HttpStatus.OK);
  47. }
  48. }
  49. catch (Exception exc)
  50. {
  51. // Don't cache the client -- it's relying on thread-local storage.
  52. TelemetryClient client = Utility.getTelemetryClient();
  53. if (client != null) client.trackException(exc);
  54. return new ResponseEntity<>(exc.toString(), HttpStatus.INTERNAL_SERVER_ERROR);
  55. }
  56. }
  57. /**
  58. * Gets a specific dealer by its name.
  59. *
  60. * @param name The dealer name
  61. * @return An HttpResponse containing a list of catalog items.
  62. */
  63. @RequestMapping(method = RequestMethod.GET, value = "/{name}")
  64. public ResponseEntity getDealer(@PathVariable String name)
  65. {
  66. try
  67. {
  68. DealerInfo dealer = getRepository().getDealer(name);
  69. if (dealer == null)
  70. {
  71. return new ResponseEntity<DealerInfo>(HttpStatus.NOT_FOUND);
  72. }
  73. else
  74. {
  75. return new ResponseEntity<>(dealer, HttpStatus.OK);
  76. }
  77. }
  78. catch (Exception exc)
  79. {
  80. // Don't cache the client -- it's relying on thread-local storage.
  81. TelemetryClient client = Utility.getTelemetryClient();
  82. if (client != null) client.trackException(exc);
  83. return new ResponseEntity<>(exc.toString(), HttpStatus.INTERNAL_SERVER_ERROR);
  84. }
  85. }
  86. /**
  87. * Adds a dealer contact record
  88. *
  89. * @param info Information about the dealer
  90. * @return An HTTP status code.
  91. */
  92. @RequestMapping(method = RequestMethod.POST)
  93. public ResponseEntity addDealer(@RequestBody DealerInfo info)
  94. {
  95. String errorMsg = info.validate();
  96. if (errorMsg != null)
  97. {
  98. return new ResponseEntity<>(errorMsg, HttpStatus.BAD_REQUEST);
  99. }
  100. try
  101. {
  102. DealerInfo dealer = getRepository().getDealer(info.getName());
  103. if (dealer != null)
  104. {
  105. return new ResponseEntity<>("Dealer already exists", HttpStatus.CONFLICT);
  106. }
  107. boolean result = getRepository().upsertDealer(info, null);
  108. String applicationPath = OrderingInitializer.getApplicationPath();
  109. HttpHeaders responseHeaders = new HttpHeaders();
  110. responseHeaders.set("Location", applicationPath + "/dealers/" + info.getName());
  111. return new ResponseEntity(responseHeaders, result ? HttpStatus.OK : HttpStatus.CREATED);
  112. }
  113. catch (Exception exc)
  114. {
  115. // Don't cache the client -- it's relying on thread-local storage.
  116. TelemetryClient client = Utility.getTelemetryClient();
  117. if (client != null) client.trackException(exc);
  118. return new ResponseEntity<>(exc.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
  119. }
  120. }
  121. /**
  122. * Adds a dealer contact record
  123. *
  124. * @param name The dealer name
  125. * @param info Information about the dealer
  126. * @return An HTTP status code.
  127. */
  128. @RequestMapping(method = RequestMethod.PUT, value = "/{name}")
  129. public ResponseEntity updateDealer(@PathVariable String name, @RequestBody DealerInfo info)
  130. {
  131. String errorMsg = info.validate();
  132. if (errorMsg != null)
  133. {
  134. return new ResponseEntity<>(errorMsg, HttpStatus.BAD_REQUEST);
  135. }
  136. try
  137. {
  138. DealerInfo dealer = getRepository().getDealer(name);
  139. if (dealer == null)
  140. {
  141. return new ResponseEntity<DealerInfo>(HttpStatus.NOT_FOUND);
  142. }
  143. getRepository().upsertDealer(info, null);
  144. return new ResponseEntity(HttpStatus.OK);
  145. }
  146. catch (Exception exc)
  147. {
  148. // Don't cache the client -- it's relying on thread-local storage.
  149. TelemetryClient client = Utility.getTelemetryClient();
  150. if (client != null) client.trackException(exc);
  151. return new ResponseEntity<>(exc.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
  152. }
  153. }
  154. /**
  155. * Remove an catalog item SKU from the catalog.
  156. *
  157. * @param name The dealer name.
  158. * @return An HTTP status code.
  159. */
  160. @RequestMapping(method = RequestMethod.DELETE, value = "/{name}")
  161. public ResponseEntity removeDealer(@PathVariable String name)
  162. {
  163. try
  164. {
  165. if (getRepository().removeDealer(name, null))
  166. {
  167. return new ResponseEntity<DealerInfo>(HttpStatus.NO_CONTENT);
  168. }
  169. else
  170. {
  171. return new ResponseEntity<DealerInfo>(HttpStatus.NOT_FOUND);
  172. }
  173. }
  174. catch (Exception exc)
  175. {
  176. // Don't cache the client -- it's relying on thread-local storage.
  177. TelemetryClient client = Utility.getTelemetryClient();
  178. if (client != null) client.trackException(exc);
  179. return new ResponseEntity<>(exc.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
  180. }
  181. }
  182. private static DealersRepository getRepository()
  183. {
  184. return RepositoryFactory.getDealersRepository();
  185. }
  186. }