Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Andras Madi-Szabo
Added: May 19, 2020 1:15 PM
Modified: May 19, 2020 1:17 PM
Views: 132
Tags: vizsga
  1. import com.greenfoxacademy.orinetationnormalexam.models.APIResult;
  2. import com.greenfoxacademy.orinetationnormalexam.models.Item;
  3. import com.greenfoxacademy.orinetationnormalexam.services.ItemService;
  4. import java.util.List;
  5. import javassist.NotFoundException;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.PathVariable;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.RestController;
  14.  
  15. @RestController
  16. public class APIController {
  17.  
  18.   private ItemService itemService;
  19.  
  20.   @Autowired
  21.   public APIController(ItemService itemService) {
  22.     this.itemService = itemService;
  23.   }
  24.  
  25.   @GetMapping("/shoppingplanner/query")
  26.   public ResponseEntity<?> selectItems(@RequestParam String price, @RequestParam String type) {
  27.     Float priceNumber = Float.parseFloat(price);
  28.     APIResult apiResult = new APIResult();
  29.     List<Item> selectedItems = itemService.selectItems(priceNumber, type);
  30.  
  31.       if (selectedItems.size() > 0) {
  32.         apiResult.setResult("ok");
  33.         apiResult.setClothes(selectedItems);
  34.         HttpStatus status;
  35.         status = HttpStatus.OK;
  36.         return ResponseEntity.status(status).body(apiResult);
  37.       }  else {
  38.         apiResult.setResult("no clothes available");
  39.         apiResult.setClothes(selectedItems);
  40.         HttpStatus status;
  41.         status = HttpStatus.OK;
  42.         return ResponseEntity.status(status).body(apiResult);
  43.       }
  44.     }
  45.   }
  46.  
  47. //    HttpStatus status;
  48. //    try {
  49. //      linkService.deleteLinkBySecret(secret.getSecretCode(), id);
  50. //      status= HttpStatus.NO_CONTENT;
  51. //    } catch (IllegalArgumentException e) {
  52. //      status=(HttpStatus.FORBIDDEN);
  53. //    } catch (NotFoundException e) {
  54. //      status=(HttpStatus.NOT_FOUND);
  55. //    }
  56. //    return ResponseEntity.status(status).build();
  57.  
  58.  
  59.   @Override
  60.   public List<Item> selectItems(Float price, String type) {
  61.     List<Item> selected = null;
  62.     if (type.equalsIgnoreCase("lower")){
  63.       selected = itemRepository.findAllByUnitPriceIsLessThan(price);
  64.     } else if (type.equalsIgnoreCase("higher")){
  65.       selected =  itemRepository.findAllByUnitPriceIsGreaterThan(price);
  66.     } else if (type.equalsIgnoreCase("equal")){
  67.       selected =  itemRepository.findAllByUnitPriceIs(price);
  68.     }
  69.     return selected;
  70.   }