Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.revisorsystem.controller;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import net.revisorsystem.RestClasses.*;
- import net.revisorsystem.Views.Views;
- import net.revisorsystem.model.Owner;
- import net.revisorsystem.model.Product;
- import net.revisorsystem.model.Seller;
- import net.revisorsystem.service.OwnerService;
- import net.revisorsystem.service.ProductService;
- import net.revisorsystem.service.SellerService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import java.io.IOException;
- import java.time.LocalDate;
- import java.util.List;
- @Controller
- @EnableWebMvc
- @RequestMapping(value = "/rest")
- public class RestController {
- @Autowired(required = true)
- @Qualifier(value = "ownerService")
- private OwnerService ownerService;
- @Autowired
- @Qualifier(value = "sellerService")
- private SellerService sellerService;
- @Autowired
- @Qualifier(value = "productService")
- private ProductService productService;
- @ResponseBody
- @RequestMapping(value = "owner/all", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public List<Owner> search() {
- List<Owner> owners = ownerService.listOwners();
- return owners;
- }
- @ResponseBody
- @RequestMapping(value = "owner/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public Owner getOwner(@PathVariable("id") int id) {
- System.out.println("Fetching User with id " + id);
- Owner owner = ownerService.getOwnerById(id);
- System.out.println(new ResponseEntity<Owner>(owner, HttpStatus.OK));
- return owner;
- }
- /**
- * 1. Get auth
- */
- @ResponseBody
- @RequestMapping(value = "auth", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public String auth(@RequestParam String phoneNum, @RequestParam String password) throws IOException {
- ObjectMapper mapper = new ObjectMapper();
- OwnerRest ownerRest = ownerService.authOwner(phoneNum, password);
- SellerRest sellerRest = sellerService.authSeller(phoneNum, password);
- if (ownerRest.getName() != null) {
- String owner = mapper.writerWithView(Views.Auth.class).writeValueAsString(ownerRest);
- return owner;
- }
- if (sellerRest.getName() != null) {
- String seller = mapper.writerWithView(Views.Auth.class).writeValueAsString(sellerRest);
- return seller;
- }
- return null;
- }
- /** 2. Get basic plan info GET */
- @ResponseBody
- @RequestMapping(value = "getBasicPlanInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public GetBasicPlanInfo getBasicPlanInfo(@RequestParam String phoneNum) {
- return ownerService.getBasicPlanInfo(phoneNum);
- }
- /** 3. Get chains GET */
- @ResponseBody
- @RequestMapping(value = "getChains", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public String getChains(@RequestParam String phoneNum) throws JsonProcessingException {
- ObjectMapper mapper = new ObjectMapper();
- String chains = mapper.writerWithView(Views.Owner.class).writeValueAsString(ownerService.getChains(phoneNum));
- return chains;
- }
- /** 4. Add chain POST */
- @ResponseBody
- @RequestMapping(value = "addChain", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public String addChain(@RequestParam String phoneNum, @RequestParam String nameChain) {
- return ownerService.addChain(phoneNum, nameChain);
- }
- /** 5. Get departments GET */
- @ResponseBody
- @RequestMapping(value = "getShops", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public String getShops(@RequestParam String phoneNum) throws JsonProcessingException {
- ObjectMapper mapper = new ObjectMapper();
- String result = mapper.writerWithView(Views.Seller.class).writeValueAsString(ownerService.getShops(phoneNum));
- return result;
- }
- /** 6. Add department POST */
- @ResponseBody
- @RequestMapping(value = "addShop", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public String addShop(@RequestParam String phoneNum, @RequestParam int chainId, @RequestParam String name, @RequestParam String adress) {
- return ownerService.addShop(phoneNum, chainId, name, adress);
- }
- /**
- * 7. Get sellers GET
- */
- @ResponseBody
- @RequestMapping(value = "getSellers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public List<GetSellersRest> getSellers(@RequestParam String phoneNum) {
- return this.ownerService.getSellers(phoneNum);
- }
- /**
- * 8. Add seller POST
- */
- @ResponseBody
- @RequestMapping(value = "addSeller", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public Status addSeller(@RequestParam String name, @RequestParam String surname, @RequestParam String phone,
- @RequestParam String password, @RequestParam int shopId, @RequestParam int gender) {
- Seller seller = new Seller();
- seller.setName(name);
- seller.setSurname(surname);
- seller.setPhone(phone);
- seller.setPass(password);
- seller.setShop(shopId);
- seller.setOwner(ownerService.getOwnerByShopId(shopId));
- seller.setGender(gender);
- sellerService.addSeller(seller);
- Status status = new Status(true);
- return status;
- }
- /**
- * 9. Get stock GET
- */
- @ResponseBody
- @RequestMapping(value = "getStock", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public List<ProductRest> getStock(@RequestParam String phoneNum) {
- if (ownerService.check(phoneNum )) {
- return this.ownerService.getProducts(phoneNum);
- }
- if (sellerService.check(phoneNum)) {
- return this.sellerService.getProducts(phoneNum);
- }
- return null;
- }
- @ResponseBody
- @RequestMapping(value = "getStock1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public List<ProductRest> getStock(@RequestParam String phoneNum,@RequestParam int chainId) {
- return ownerService.getProducts(phoneNum,chainId);
- }
- @ResponseBody
- @RequestMapping(value = "getStock2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public List<ProductRest> getStock(@RequestParam String phoneNum,@RequestParam int chainId,@RequestParam int shopId) {
- return ownerService.getProducts(phoneNum,chainId,shopId);
- }
- /** 10. add Product */
- @ResponseBody
- @RequestMapping(value = "addProduct", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public Status addProduct(@RequestParam String phoneNum, @RequestParam int shopId, @RequestParam String barcode,
- @RequestParam String name, @RequestParam int groupId, @RequestParam float primeCost) {
- Product product = new Product();
- product.setBarCode(barcode);
- product.setShop(shopId);
- product.setBarCode(barcode);
- product.setName(name);
- product.setGroup(groupId);
- product.setPrice(primeCost);
- product.setStatus(0);
- LocalDate localDate= LocalDate.now();
- System.out.println(localDate);
- product.setDate(localDate.toString());
- productService.addProduct(product);
- Status status = new Status(true);
- return status;
- }
- /** 11. Return Product */
- @ResponseBody
- @RequestMapping(value = "returnProduct", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
- public Status returnProduct(@RequestParam String phoneNum, @RequestParam int shopId, @RequestParam String barcode,
- @RequestParam String name, @RequestParam int groupId, @RequestParam float primeCost) {
- Product product = new Product();
- product.setBarCode(barcode);
- product.setShop(shopId);
- product.setBarCode(barcode);
- product.setName(name);
- product.setGroup(groupId);
- product.setPrice(primeCost);
- product.setStatus(0);
- LocalDate localDate= LocalDate.now();
- System.out.println(localDate);
- product.setDate(localDate.toString());
- productService.addProduct(product);
- ownerService.returnProduct(shopId);
- Status status = new Status(true);
- return status;
- }
- @RequestMapping(value = "/owner/add", method = RequestMethod.GET)
- public @ResponseBody
- String addNewOwner(@RequestParam String name
- // @RequestParam String surname,
- // @RequestParam String accountNumber,
- // @RequestParam String phone,
- // @RequestParam String email,
- // @RequestParam String pass,
- // @RequestParam float balance,
- // @RequestParam int plan
- ) {
- Owner owner = new Owner();
- owner.setName(name);
- // owner.setSurname(surname);
- // owner.setAccountNumber(accountNumber);
- // owner.setPhone(phone);
- // owner.setEmail(email);
- // owner.setPass(pass);
- // owner.setBalance(balance);
- // owner.setPlan(plan);
- this.ownerService.addOwner(owner);
- return "Saved";
- }
- // @RequestMapping(value= "/owner/all",method = RequestMethod.POST)
- // @ResponseBody public List<Owner> getAllOwners(){
- //
- // return this.ownerService.listOwners();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement