SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @RestController
  2. @RequestMapping("/")
  3. @RequiredArgsConstructor
  4. public class FloristsController {
  5.  
  6.     private final FlowerRepository flowerRepository;
  7.     private final PriceListRepository priceListRepository;
  8.  
  9.  
  10.     //--------------------------------KWIATY--------------------------------------------------------------------
  11.  
  12.     @GetMapping("/flower")
  13.     public ResponseEntity<List<Flower>> getAllFlowers(){
  14.         return ResponseEntity.ok(flowerRepository.findAll());
  15.     }
  16.  
  17.     @PostMapping("/")
  18.     public ResponseEntity<Flower> newFlower(@RequestBody @Valid Flower flower){
  19.         return new ResponseEntity<>(flowerRepository.saveAndFlush(flower), HttpStatus.CREATED);
  20.     }
  21.  
  22.     @GetMapping("/{id}")
  23.     public ResponseEntity<Flower> getFlower(@PathVariable("id") @Valid Long id){
  24.         return ResponseEntity.ok(flowerRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Unknown ID: " + id)));
  25.     }
  26.  
  27.     @DeleteMapping("/{id}")
  28.     public ResponseEntity<Flower> buyFlower(@PathVariable("id") @Valid Long id){
  29.         if(!flowerRepository.existsById(id)){
  30.             return ResponseEntity.notFound().build();
  31.         }
  32.         flowerRepository.deleteById(id);
  33.         return ResponseEntity.noContent().build();
  34.     }
  35.  
  36.     @PutMapping("/{id}")
  37.     public Flower editFlowerById(@RequestBody Flower newFlower, @PathVariable("id") @Valid Long id){
  38.         return flowerRepository.findById(id)
  39.                 .map(flower -> {
  40.                     flower.setName(newFlower.getName());
  41.                     flower.setColour(newFlower.getColour());
  42.                     return flowerRepository.save(flower);
  43.                 }).orElseGet(() -> {
  44.                     newFlower.setId(id);
  45.                     return flowerRepository.save(newFlower);
  46.                 });
  47.     }
  48.  
  49.  
  50.  
  51.     //--------------------------------CENNIK--------------------------------------------------------------------
  52.  
  53.     @GetMapping("/pricelist")
  54.     public ResponseEntity<List<PriceList>> getAllPriceLists(){
  55.         return ResponseEntity.ok(priceListRepository.findAll());
  56.     }
  57.  
  58.     @GetMapping("/pricelist/{id}")
  59.     public ResponseEntity <PriceList> getPriceListById(@PathVariable("id") @Valid Long id){
  60.         return ResponseEntity.ok(priceListRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Unknown Price List ID: " + id)));
  61.     }
  62.  
  63.     @PostMapping("/pricelist")
  64.     public ResponseEntity<PriceList> newPriceList(@RequestBody @Valid PriceList priceList){
  65.         return new ResponseEntity<>(priceListRepository.saveAndFlush(priceList), HttpStatus.CREATED);
  66.     }
  67.  
  68.     @DeleteMapping("/pricelist/{id}")
  69.     public ResponseEntity<PriceList> deletePriceListById(@PathVariable("id") @Valid long id){
  70.         if(!priceListRepository.existsById(id)){
  71.             return ResponseEntity.notFound().build();
  72.         }
  73.         priceListRepository.deleteById(id);
  74.         return ResponseEntity.noContent().build();
  75.     }
  76. }
  77.  
  78. //--------------------------------REPOSITORY--------------------------------------------------------------------
  79.  
  80. public interface FlowerRepository extends JpaRepository <Flower, Long>{
  81. }
  82.  
  83. public interface PriceListRepository extends JpaRepository <PriceList, Long> {
  84. }
  85.  
  86. //--------------------------------MODEL--------------------------------------------------------------------
  87.  
  88. @Entity
  89. @Data
  90. @AllArgsConstructor
  91. public class Flower {
  92.  
  93.     @Id
  94.     @GeneratedValue
  95.     private long id;
  96.     @NotEmpty
  97.     private String name;
  98.     @NotEmpty
  99.     private String colour;
  100.  
  101. }
  102.  
  103. @Entity
  104. @Data
  105. public class PriceList {
  106.  
  107.     @Id
  108.     @GeneratedValue
  109.     private long id;
  110.     @NotEmpty
  111.     private String name;
  112.     @NotEmpty
  113.     private LocalDateTime dateTimeFrom;
  114.     @NotEmpty
  115.     private LocalDateTime dateTimeTo;
  116.     @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
  117.     @JoinColumn(name = "pricelistpositions_id")
  118.     private List<PriceListPosition> priceListPositions;
  119. }
  120.  
  121.  
  122. @Data
  123. @Entity
  124. public class PriceListPosition {
  125.  
  126.     @Id
  127.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  128.     private long id;
  129.     @Transient
  130.     private Flower flower;
  131.     private double price;
  132.     @ManyToOne
  133.     @JoinColumn(name = "pricelist_id")
  134.     private PriceList priceList;
  135.  
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top