Advertisement
Guest User

BuildingController

a guest
Feb 27th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package nl.tudelft.oopp.demo.controllers;
  2.  
  3. import nl.tudelft.oopp.demo.entities.Building;
  4. import nl.tudelft.oopp.demo.entities.Room;
  5. import nl.tudelft.oopp.demo.repositories.BuildingRepository;
  6. import nl.tudelft.oopp.demo.repositories.RoomRepository;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.*;
  10.  
  11. import java.util.*;
  12.  
  13.  
  14. @Controller
  15. @RequestMapping("/building")
  16. public class BuildingController {
  17. /**
  18. * GET Endpoint for all buildings
  19. */
  20.  
  21. @Autowired
  22. private BuildingRepository buildingRepository;
  23.  
  24. @Autowired
  25. private RoomRepository roomRepository;
  26.  
  27.  
  28. /** Endpoint for all buildings
  29. * Comparator needs to be implemented
  30. * @return list of all buildings
  31. */
  32. @RequestMapping(method = RequestMethod.GET)
  33. @GetMapping("building")
  34. @ResponseBody
  35. public List<Building> getBuildings() {
  36. List<Building> buildingList = buildingRepository.findAll();
  37. Comparator<Building> cmp = (Building o1, Building o2) -> o1.getBuildingName().compareTo(o2.getBuildingName());
  38. buildingList.sort(cmp);
  39. return buildingList;
  40. }
  41.  
  42. @RequestMapping(method = RequestMethod.POST)
  43. @PostMapping("building")
  44. @ResponseBody
  45. public void insertBuilding(@RequestBody Map<String, String> body) {
  46. String name = body.get("name");
  47. String address = body.get("address");
  48. String numberOfBikes = body.get("numberOfBikes");
  49. buildingRepository.insertBuilding(name, address, Integer.parseInt(numberOfBikes));
  50. }
  51.  
  52. /** Endpoint for specific building
  53. * @return a building object
  54. * @throws IllegalAccessException if building is not present
  55. */
  56. @RequestMapping(value="/{buildingId}", method = RequestMethod.GET)
  57. @ResponseBody
  58. public Building getBuilding(@PathVariable final long buildingId) throws IllegalAccessException {
  59. Optional<Building> b = buildingRepository.findById(buildingId);
  60. if (b.isEmpty()) {
  61. throw new IllegalAccessException("Building is not found");
  62. }
  63. else return b.get();
  64. }
  65.  
  66. /**
  67. * Return all rooms from a specific building
  68. * @param buildingId the building you want the rooms in
  69. * @return a list of rooms
  70. * @throws IllegalAccessException if the building is not present
  71. */
  72. @RequestMapping(value="/{buildingId}/rooms", method = RequestMethod.GET)
  73. @ResponseBody
  74. public List<Room> getRoomsFromBuilding(@PathVariable final long buildingId) throws IllegalAccessException {
  75. Optional<Building> building = buildingRepository.findById(buildingId);
  76. if (building.isEmpty()) {
  77. throw new IllegalAccessException("Building is not found");
  78. }
  79. List<Room> result = new ArrayList<>();
  80. for (Room room: roomRepository.findAll()) {
  81. if (buildingId == room.getBuildingId()) {
  82. result.add(room);
  83. }
  84. }
  85. return result;
  86. }
  87.  
  88. /**
  89. * Return a specific room from a specific building
  90. * @param buildingId the building you want the room in
  91. * @param roomId the room you want
  92. * @return a room
  93. * @throws IllegalAccessException if the building or room is not present
  94. */
  95. @RequestMapping(value="/{buildingId}/rooms/{roomId} ", method = RequestMethod.GET)
  96. @ResponseBody
  97. public Room getRoomFromBuilding(@PathVariable final long buildingId, @PathVariable final long roomId) throws IllegalAccessException {
  98. Optional<Building> building = buildingRepository.findById(buildingId);
  99. if (building.isEmpty()) {
  100. throw new IllegalAccessException("Building is not found");
  101. }
  102. List<Room> result = new ArrayList<>();
  103. for (Room room: roomRepository.findAll()) {
  104. if (buildingId == room.getBuildingId() && room.getId() == roomId) {
  105. return room;
  106. }
  107. }
  108. throw new IllegalAccessException("Room is not found");
  109. }
  110.  
  111. /**
  112. * Return all rooms
  113. * @return a list of rooms
  114. */
  115. @RequestMapping(value="/rooms", method = RequestMethod.GET)
  116. @ResponseBody
  117. public List<Room> getRooms() {
  118. List<Room> rooms = roomRepository.findAll();
  119. Comparator<Room> cmp = (Room o1, Room o2) -> o1.getName().compareTo(o2.getName());
  120. rooms.sort(cmp);
  121. return rooms;
  122. }
  123.  
  124. /**
  125. * Return a specific room
  126. * @param roomId the ID of the room you want
  127. * @return a room
  128. * @throws IllegalAccessException if the room is not present
  129. */
  130. @RequestMapping(value="/rooms/{roomId}", method = RequestMethod.GET)
  131. @ResponseBody
  132. public Room getRoom(@PathVariable final long roomId) throws IllegalAccessException {
  133. Optional<Room> room = roomRepository.findById(roomId);
  134. if (room.isEmpty()) {
  135. throw new IllegalAccessException("Building is not found");
  136. }
  137. return room.get();
  138. }
  139.  
  140.  
  141. /** Test to show how Endpoint works.
  142. * @return String "hello"
  143. */
  144. @RequestMapping(value="/test", method = RequestMethod.GET)
  145. @ResponseBody
  146. public String getTest() {
  147. return "hello";
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement