Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.36 KB | None | 0 0
  1. package net.revisorsystem.controller;
  2.  
  3.  
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import net.revisorsystem.RestClasses.*;
  7. import net.revisorsystem.Views.Views;
  8. import net.revisorsystem.model.Owner;
  9. import net.revisorsystem.model.Product;
  10. import net.revisorsystem.model.Seller;
  11. import net.revisorsystem.service.OwnerService;
  12. import net.revisorsystem.service.ProductService;
  13. import net.revisorsystem.service.SellerService;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Qualifier;
  16. import org.springframework.http.HttpStatus;
  17. import org.springframework.http.MediaType;
  18. import org.springframework.http.ResponseEntity;
  19. import org.springframework.stereotype.Controller;
  20. import org.springframework.web.bind.annotation.*;
  21. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  22.  
  23. import java.io.IOException;
  24. import java.time.LocalDate;
  25. import java.util.List;
  26.  
  27. @Controller
  28. @EnableWebMvc
  29. @RequestMapping(value = "/rest")
  30. public class RestController {
  31.  
  32.  
  33. @Autowired(required = true)
  34. @Qualifier(value = "ownerService")
  35. private OwnerService ownerService;
  36.  
  37. @Autowired
  38. @Qualifier(value = "sellerService")
  39. private SellerService sellerService;
  40. @Autowired
  41. @Qualifier(value = "productService")
  42. private ProductService productService;
  43.  
  44. @ResponseBody
  45. @RequestMapping(value = "owner/all", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  46. public List<Owner> search() {
  47. List<Owner> owners = ownerService.listOwners();
  48. return owners;
  49. }
  50.  
  51.  
  52. @ResponseBody
  53. @RequestMapping(value = "owner/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  54. public Owner getOwner(@PathVariable("id") int id) {
  55. System.out.println("Fetching User with id " + id);
  56. Owner owner = ownerService.getOwnerById(id);
  57.  
  58. System.out.println(new ResponseEntity<Owner>(owner, HttpStatus.OK));
  59. return owner;
  60.  
  61. }
  62.  
  63. /**
  64. * 1. Get auth
  65. */
  66. @ResponseBody
  67. @RequestMapping(value = "auth", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  68. public String auth(@RequestParam String phoneNum, @RequestParam String password) throws IOException {
  69. ObjectMapper mapper = new ObjectMapper();
  70. OwnerRest ownerRest = ownerService.authOwner(phoneNum, password);
  71. SellerRest sellerRest = sellerService.authSeller(phoneNum, password);
  72. if (ownerRest.getName() != null) {
  73.  
  74.  
  75. String owner = mapper.writerWithView(Views.Auth.class).writeValueAsString(ownerRest);
  76.  
  77. return owner;
  78. }
  79. if (sellerRest.getName() != null) {
  80.  
  81.  
  82. String seller = mapper.writerWithView(Views.Auth.class).writeValueAsString(sellerRest);
  83.  
  84. return seller;
  85.  
  86. }
  87.  
  88. return null;
  89. }
  90.  
  91. /** 2. Get basic plan info GET */
  92. @ResponseBody
  93. @RequestMapping(value = "getBasicPlanInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  94. public GetBasicPlanInfo getBasicPlanInfo(@RequestParam String phoneNum) {
  95.  
  96. return ownerService.getBasicPlanInfo(phoneNum);
  97. }
  98.  
  99. /** 3. Get chains GET */
  100. @ResponseBody
  101. @RequestMapping(value = "getChains", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  102. public String getChains(@RequestParam String phoneNum) throws JsonProcessingException {
  103. ObjectMapper mapper = new ObjectMapper();
  104. String chains = mapper.writerWithView(Views.Owner.class).writeValueAsString(ownerService.getChains(phoneNum));
  105.  
  106. return chains;
  107. }
  108.  
  109. /** 4. Add chain POST */
  110. @ResponseBody
  111. @RequestMapping(value = "addChain", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  112. public String addChain(@RequestParam String phoneNum, @RequestParam String nameChain) {
  113.  
  114. return ownerService.addChain(phoneNum, nameChain);
  115. }
  116.  
  117. /** 5. Get departments GET */
  118. @ResponseBody
  119. @RequestMapping(value = "getShops", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  120. public String getShops(@RequestParam String phoneNum) throws JsonProcessingException {
  121. ObjectMapper mapper = new ObjectMapper();
  122. String result = mapper.writerWithView(Views.Seller.class).writeValueAsString(ownerService.getShops(phoneNum));
  123. return result;
  124. }
  125.  
  126. /** 6. Add department POST */
  127. @ResponseBody
  128. @RequestMapping(value = "addShop", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  129. public String addShop(@RequestParam String phoneNum, @RequestParam int chainId, @RequestParam String name, @RequestParam String adress) {
  130. return ownerService.addShop(phoneNum, chainId, name, adress);
  131. }
  132.  
  133. /**
  134. * 7. Get sellers GET
  135. */
  136. @ResponseBody
  137. @RequestMapping(value = "getSellers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  138. public List<GetSellersRest> getSellers(@RequestParam String phoneNum) {
  139.  
  140. return this.ownerService.getSellers(phoneNum);
  141.  
  142. }
  143.  
  144. /**
  145. * 8. Add seller POST
  146. */
  147. @ResponseBody
  148. @RequestMapping(value = "addSeller", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  149. public Status addSeller(@RequestParam String name, @RequestParam String surname, @RequestParam String phone,
  150. @RequestParam String password, @RequestParam int shopId, @RequestParam int gender) {
  151.  
  152. Seller seller = new Seller();
  153. seller.setName(name);
  154. seller.setSurname(surname);
  155. seller.setPhone(phone);
  156. seller.setPass(password);
  157. seller.setShop(shopId);
  158. seller.setOwner(ownerService.getOwnerByShopId(shopId));
  159. seller.setGender(gender);
  160. sellerService.addSeller(seller);
  161. Status status = new Status(true);
  162. return status;
  163.  
  164. }
  165.  
  166. /**
  167. * 9. Get stock GET
  168. */
  169. @ResponseBody
  170. @RequestMapping(value = "getStock", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  171. public List<ProductRest> getStock(@RequestParam String phoneNum) {
  172.  
  173. if (ownerService.check(phoneNum )) {
  174. return this.ownerService.getProducts(phoneNum);
  175. }
  176. if (sellerService.check(phoneNum)) {
  177. return this.sellerService.getProducts(phoneNum);
  178. }
  179. return null;
  180. }
  181.  
  182. @ResponseBody
  183. @RequestMapping(value = "getStock1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  184. public List<ProductRest> getStock(@RequestParam String phoneNum,@RequestParam int chainId) {
  185.  
  186. return ownerService.getProducts(phoneNum,chainId);
  187. }
  188.  
  189. @ResponseBody
  190. @RequestMapping(value = "getStock2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  191. public List<ProductRest> getStock(@RequestParam String phoneNum,@RequestParam int chainId,@RequestParam int shopId) {
  192.  
  193. return ownerService.getProducts(phoneNum,chainId,shopId);
  194. }
  195.  
  196.  
  197. /** 10. add Product */
  198. @ResponseBody
  199. @RequestMapping(value = "addProduct", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  200. public Status addProduct(@RequestParam String phoneNum, @RequestParam int shopId, @RequestParam String barcode,
  201. @RequestParam String name, @RequestParam int groupId, @RequestParam float primeCost) {
  202.  
  203. Product product = new Product();
  204. product.setBarCode(barcode);
  205. product.setShop(shopId);
  206. product.setBarCode(barcode);
  207. product.setName(name);
  208. product.setGroup(groupId);
  209. product.setPrice(primeCost);
  210. product.setStatus(0);
  211. LocalDate localDate= LocalDate.now();
  212. System.out.println(localDate);
  213. product.setDate(localDate.toString());
  214.  
  215. productService.addProduct(product);
  216. Status status = new Status(true);
  217. return status;
  218.  
  219. }
  220.  
  221. /** 11. Return Product */
  222. @ResponseBody
  223. @RequestMapping(value = "returnProduct", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  224. public Status returnProduct(@RequestParam String phoneNum, @RequestParam int shopId, @RequestParam String barcode,
  225. @RequestParam String name, @RequestParam int groupId, @RequestParam float primeCost) {
  226.  
  227. Product product = new Product();
  228. product.setBarCode(barcode);
  229. product.setShop(shopId);
  230. product.setBarCode(barcode);
  231. product.setName(name);
  232. product.setGroup(groupId);
  233. product.setPrice(primeCost);
  234. product.setStatus(0);
  235. LocalDate localDate= LocalDate.now();
  236. System.out.println(localDate);
  237. product.setDate(localDate.toString());
  238. productService.addProduct(product);
  239. ownerService.returnProduct(shopId);
  240. Status status = new Status(true);
  241. return status;
  242.  
  243. }
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278. @RequestMapping(value = "/owner/add", method = RequestMethod.GET)
  279. public @ResponseBody
  280. String addNewOwner(@RequestParam String name
  281. // @RequestParam String surname,
  282. // @RequestParam String accountNumber,
  283. // @RequestParam String phone,
  284. // @RequestParam String email,
  285. // @RequestParam String pass,
  286. // @RequestParam float balance,
  287. // @RequestParam int plan
  288. ) {
  289.  
  290. Owner owner = new Owner();
  291. owner.setName(name);
  292. // owner.setSurname(surname);
  293. // owner.setAccountNumber(accountNumber);
  294. // owner.setPhone(phone);
  295. // owner.setEmail(email);
  296. // owner.setPass(pass);
  297. // owner.setBalance(balance);
  298. // owner.setPlan(plan);
  299. this.ownerService.addOwner(owner);
  300. return "Saved";
  301. }
  302. // @RequestMapping(value= "/owner/all",method = RequestMethod.POST)
  303. // @ResponseBody public List<Owner> getAllOwners(){
  304. //
  305. // return this.ownerService.listOwners();
  306. // }
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement