|
| 1 | +package com.bezkoder.spring.jpa.postgresql.controller; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.web.bind.annotation.CrossOrigin; |
| 11 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 12 | +import org.springframework.web.bind.annotation.GetMapping; |
| 13 | +import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.PutMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestBody; |
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | +import org.springframework.web.bind.annotation.RequestParam; |
| 19 | +import org.springframework.web.bind.annotation.RestController; |
| 20 | + |
| 21 | +import com.bezkoder.spring.jpa.postgresql.model.Tutorial; |
| 22 | +import com.bezkoder.spring.jpa.postgresql.repository.TutorialRepository; |
| 23 | + |
| 24 | +@CrossOrigin(origins = "http://localhost:8081") |
| 25 | +@RestController |
| 26 | +@RequestMapping("/api") |
| 27 | +public class TutorialController { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + TutorialRepository tutorialRepository; |
| 31 | + |
| 32 | + @GetMapping("/tutorials") |
| 33 | + public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String title) { |
| 34 | + try { |
| 35 | + List<Tutorial> tutorials = new ArrayList<Tutorial>(); |
| 36 | + |
| 37 | + if (title == null) |
| 38 | + tutorialRepository.findAll().forEach(tutorials::add); |
| 39 | + else |
| 40 | + tutorialRepository.findByTitleContaining(title).forEach(tutorials::add); |
| 41 | + |
| 42 | + if (tutorials.isEmpty()) { |
| 43 | + return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 44 | + } |
| 45 | + |
| 46 | + return new ResponseEntity<>(tutorials, HttpStatus.OK); |
| 47 | + } catch (Exception e) { |
| 48 | + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @GetMapping("/tutorials/{id}") |
| 53 | + public ResponseEntity<Tutorial> getTutorialById(@PathVariable("id") long id) { |
| 54 | + Optional<Tutorial> tutorialData = tutorialRepository.findById(id); |
| 55 | + |
| 56 | + if (tutorialData.isPresent()) { |
| 57 | + return new ResponseEntity<>(tutorialData.get(), HttpStatus.OK); |
| 58 | + } else { |
| 59 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @PostMapping("/tutorials") |
| 64 | + public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) { |
| 65 | + try { |
| 66 | + Tutorial _tutorial = tutorialRepository |
| 67 | + .save(new Tutorial(tutorial.getTitle(), tutorial.getDescription(), false)); |
| 68 | + return new ResponseEntity<>(_tutorial, HttpStatus.CREATED); |
| 69 | + } catch (Exception e) { |
| 70 | + return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @PutMapping("/tutorials/{id}") |
| 75 | + public ResponseEntity<Tutorial> updateTutorial(@PathVariable("id") long id, @RequestBody Tutorial tutorial) { |
| 76 | + Optional<Tutorial> tutorialData = tutorialRepository.findById(id); |
| 77 | + |
| 78 | + if (tutorialData.isPresent()) { |
| 79 | + Tutorial _tutorial = tutorialData.get(); |
| 80 | + _tutorial.setTitle(tutorial.getTitle()); |
| 81 | + _tutorial.setDescription(tutorial.getDescription()); |
| 82 | + _tutorial.setPublished(tutorial.isPublished()); |
| 83 | + return new ResponseEntity<>(tutorialRepository.save(_tutorial), HttpStatus.OK); |
| 84 | + } else { |
| 85 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @DeleteMapping("/tutorials/{id}") |
| 90 | + public ResponseEntity<HttpStatus> deleteTutorial(@PathVariable("id") long id) { |
| 91 | + try { |
| 92 | + tutorialRepository.deleteById(id); |
| 93 | + return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 94 | + } catch (Exception e) { |
| 95 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @DeleteMapping("/tutorials") |
| 100 | + public ResponseEntity<HttpStatus> deleteAllTutorials() { |
| 101 | + try { |
| 102 | + tutorialRepository.deleteAll(); |
| 103 | + return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 104 | + } catch (Exception e) { |
| 105 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @GetMapping("/tutorials/published") |
| 111 | + public ResponseEntity<List<Tutorial>> findByPublished() { |
| 112 | + try { |
| 113 | + List<Tutorial> tutorials = tutorialRepository.findByPublished(true); |
| 114 | + |
| 115 | + if (tutorials.isEmpty()) { |
| 116 | + return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 117 | + } |
| 118 | + return new ResponseEntity<>(tutorials, HttpStatus.OK); |
| 119 | + } catch (Exception e) { |
| 120 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments