diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..39396fb --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'demo-monolith' diff --git a/src/main/java/com/example/demo_monolith/controllers/RolesController.java b/src/main/java/com/example/demo_monolith/controllers/RolesController.java new file mode 100644 index 0000000..59dcc1a --- /dev/null +++ b/src/main/java/com/example/demo_monolith/controllers/RolesController.java @@ -0,0 +1,38 @@ +package com.example.demo_monolith.controllers; + + +import com.example.demo_monolith.model.Role; +import com.example.demo_monolith.repository.RoleRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@CrossOrigin(origins = "${app.origin}", maxAge = 3600, allowCredentials="true") +@RestController +@RequestMapping("/api") +public class RolesController { + @Autowired + private RoleRepository roleRepository; + + @GetMapping("/roles") + public ResponseEntity> getRoles(@RequestParam(required = false) String title) { + try { + List role = new ArrayList(); + role = roleRepository.findAll(); + //userRepository.findAll().forEach(users::add); + + + if (role.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + return new ResponseEntity<>(role, HttpStatus.OK); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } +} diff --git a/src/main/java/com/example/demo_monolith/controllers/SettingsController.java b/src/main/java/com/example/demo_monolith/controllers/SettingsController.java new file mode 100644 index 0000000..f84d3d7 --- /dev/null +++ b/src/main/java/com/example/demo_monolith/controllers/SettingsController.java @@ -0,0 +1,39 @@ +package com.example.demo_monolith.controllers; + + +import com.example.demo_monolith.model.Setting; +import com.example.demo_monolith.repository.SettingsRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/api") +public class SettingsController { + @Autowired + private SettingsRepository settingsRepository; + + @GetMapping("/settings") + public ResponseEntity> getSettings(@RequestParam(required = false) String title) { + try { + List settings = settingsRepository.findAll(); + //userRepository.findAll().forEach(users::add); + + + if (settings.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + return new ResponseEntity<>(settings, HttpStatus.OK); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} diff --git a/src/main/java/com/example/demo_monolith/controllers/TestController.java b/src/main/java/com/example/demo_monolith/controllers/TestController.java new file mode 100644 index 0000000..e801698 --- /dev/null +++ b/src/main/java/com/example/demo_monolith/controllers/TestController.java @@ -0,0 +1,35 @@ +package com.example.demo_monolith.controllers; + +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@CrossOrigin(origins = "*", maxAge = 3600) +@RestController +@RequestMapping("/api/test") +public class TestController { + @GetMapping("/all") + public String allAccess() { + return "Public Content."; + } + + @GetMapping("/user") + @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')") + public String userAccess() { + return "User Content."; + } + + @GetMapping("/mod") + @PreAuthorize("hasRole('MODERATOR')") + public String moderatorAccess() { + return "Moderator Board."; + } + + @GetMapping("/admin") + @PreAuthorize("hasRole('ADMIN')") + public String adminAccess() { + return "Admin Board."; + } +} diff --git a/src/main/java/com/example/demo_monolith/controllers/TutorialController.java b/src/main/java/com/example/demo_monolith/controllers/TutorialController.java new file mode 100644 index 0000000..3bac67e --- /dev/null +++ b/src/main/java/com/example/demo_monolith/controllers/TutorialController.java @@ -0,0 +1,116 @@ +package com.example.demo_monolith.controllers; + + +import com.example.demo_monolith.model.Tutorial; +import com.example.demo_monolith.repository.TutorialRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + + +@CrossOrigin(origins = "${app.origin}", maxAge = 3600, allowCredentials="true") +@RestController +@RequestMapping("/api") +public class TutorialController { + + @Autowired + TutorialRepository tutorialRepository; + + @GetMapping("/tutorials") + public ResponseEntity> getAllTutorials(@RequestParam(required = false) String title) { + try { + List tutorials = new ArrayList(); + + if (title == null) + tutorialRepository.findAll().forEach(tutorials::add); + else + tutorialRepository.findByTitleContaining(title).forEach(tutorials::add); + + if (tutorials.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + return new ResponseEntity<>(tutorials, HttpStatus.OK); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @GetMapping("/tutorials/{id}") + public ResponseEntity getTutorialById(@PathVariable("id") long id) { + Optional tutorialData = tutorialRepository.findById(id); + + if (tutorialData.isPresent()) { + return new ResponseEntity<>(tutorialData.get(), HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @PostMapping("/tutorials") + public ResponseEntity createTutorial(@RequestBody Tutorial tutorial) { + try { + Tutorial _tutorial = tutorialRepository + .save(new Tutorial(tutorial.getTitle(), tutorial.getDescription(), false)); + return new ResponseEntity<>(_tutorial, HttpStatus.CREATED); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PutMapping("/tutorials/{id}") + public ResponseEntity updateTutorial(@PathVariable("id") long id, @RequestBody Tutorial tutorial) { + Optional tutorialData = tutorialRepository.findById(id); + + if (tutorialData.isPresent()) { + Tutorial _tutorial = tutorialData.get(); + _tutorial.setTitle(tutorial.getTitle()); + _tutorial.setDescription(tutorial.getDescription()); + _tutorial.setPublished(tutorial.isPublished()); + return new ResponseEntity<>(tutorialRepository.save(_tutorial), HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @DeleteMapping("/tutorials/{id}") + public ResponseEntity deleteTutorial(@PathVariable("id") long id) { + try { + tutorialRepository.deleteById(id); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @DeleteMapping("/tutorials") + public ResponseEntity deleteAllTutorials() { + try { + tutorialRepository.deleteAll(); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + } + + @GetMapping("/tutorials/published") + public ResponseEntity> findByPublished() { + try { + List tutorials = tutorialRepository.findByPublished(true); + + if (tutorials.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + return new ResponseEntity<>(tutorials, HttpStatus.OK); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/example/demo_monolith/controllers/UsersController.java b/src/main/java/com/example/demo_monolith/controllers/UsersController.java new file mode 100644 index 0000000..6e240a8 --- /dev/null +++ b/src/main/java/com/example/demo_monolith/controllers/UsersController.java @@ -0,0 +1,38 @@ +package com.example.demo_monolith.controllers; + + +import com.example.demo_monolith.model.User; +import com.example.demo_monolith.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@CrossOrigin(origins = "${app.origin}", maxAge = 3600, allowCredentials="true") +@RestController +@RequestMapping("/api") +public class UsersController { + @Autowired + private UserRepository userRepository; + + @GetMapping("/users") + public ResponseEntity> getUsers(@RequestParam(required = false) String title) { + try { + List users = new ArrayList(); + users = userRepository.findAll(); + //userRepository.findAll().forEach(users::add); + + + if (users.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + return new ResponseEntity<>(users, HttpStatus.OK); + } catch (Exception e) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + } +}