|
| 1 | +package com.baeldung.spring.jpa.guide; |
| 2 | + |
| 3 | +import com.baeldung.spring.jpa.guide.model.Publishers; |
| 4 | +import com.baeldung.spring.jpa.guide.service.PublisherService; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.http.ResponseEntity; |
| 7 | +import org.springframework.web.bind.annotation.*; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +@RestController |
| 12 | +@RequestMapping("/publishers") |
| 13 | +public class PublisherController { |
| 14 | + |
| 15 | + private final PublisherService publisherService; |
| 16 | + |
| 17 | + @Autowired |
| 18 | + public PublisherController(PublisherService publisherService) { |
| 19 | + this.publisherService = publisherService; |
| 20 | + } |
| 21 | + |
| 22 | + @PostMapping("/save") |
| 23 | + public ResponseEntity<Publishers> save(@RequestBody Publishers publishers) { |
| 24 | + Publishers savedPublisher = publisherService.save(publishers); |
| 25 | + return ResponseEntity.ok(savedPublisher); |
| 26 | + } |
| 27 | + |
| 28 | + @GetMapping("/all") |
| 29 | + public ResponseEntity<List<Publishers>> findAll() { |
| 30 | + List<Publishers> publishersList = publisherService.findAll(); |
| 31 | + return ResponseEntity.ok(publishersList); |
| 32 | + } |
| 33 | + |
| 34 | + @GetMapping("/{id}") |
| 35 | + public ResponseEntity<Publishers> findById(@PathVariable int id) { |
| 36 | + Publishers publisher = publisherService.findById(id); |
| 37 | + return ResponseEntity.ok(publisher); |
| 38 | + } |
| 39 | + |
| 40 | + @PutMapping("/update") |
| 41 | + public ResponseEntity<Publishers> update(@RequestBody Publishers publishers) { |
| 42 | + Publishers updatedPublisher = publisherService.update(publishers); |
| 43 | + return ResponseEntity.ok(updatedPublisher); |
| 44 | + } |
| 45 | + |
| 46 | + @DeleteMapping("/{id}") |
| 47 | + public ResponseEntity<Void> delete(@PathVariable int id) { |
| 48 | + publisherService.delete(id); |
| 49 | + return ResponseEntity.noContent() |
| 50 | + .build(); |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("/location/{location}") |
| 54 | + public ResponseEntity<List<Publishers>> findAllByLocation(@PathVariable String location) { |
| 55 | + List<Publishers> publishersList = publisherService.findAllByLocation(location); |
| 56 | + return ResponseEntity.ok(publishersList); |
| 57 | + } |
| 58 | + |
| 59 | + @GetMapping("/min-journals") |
| 60 | + public ResponseEntity<List<Publishers>> findPublishersWithMinJournalsInLocation(@RequestParam int minJournals, @RequestParam String location) { |
| 61 | + List<Publishers> publishersList = publisherService.findPublishersWithMinJournalsInLocation(minJournals, location); |
| 62 | + return ResponseEntity.ok(publishersList); |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments