diff --git a/src/main/java/com/example/roommate/application/services/BookingApplicationService.java b/src/main/java/com/example/roommate/application/services/BookingApplicationService.java index 4bccd5f0..7ee6fb35 100644 --- a/src/main/java/com/example/roommate/application/services/BookingApplicationService.java +++ b/src/main/java/com/example/roommate/application/services/BookingApplicationService.java @@ -2,12 +2,14 @@ import com.example.roommate.annotations.ApplicationService; import com.example.roommate.application.data.RoomApplicationData; +import com.example.roommate.controller.HomeController; import com.example.roommate.domain.services.UserDomainService; import com.example.roommate.exceptions.domainService.GeneralDomainException; import com.example.roommate.interfaces.entities.IUser; import com.example.roommate.interfaces.entities.IWorkspace; import com.example.roommate.utility.IterableSupport; import com.example.roommate.values.domainValues.BookedTimeframe; +import com.example.roommate.values.domainValues.DayTimeFrame; import com.example.roommate.values.domainValues.IntermediateBookDataForm; import com.example.roommate.values.domainValues.ItemName; import com.example.roommate.domain.services.RoomDomainService; @@ -16,6 +18,7 @@ import com.example.roommate.interfaces.entities.IRoom; import com.example.roommate.values.forms.KeyMasterForm; import com.example.roommate.values.models.RoomBookingModel; +import com.example.roommate.values.models.RoomHomeModel; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; @@ -26,6 +29,9 @@ import java.time.format.DateTimeFormatter; import java.time.LocalDate; import java.time.DayOfWeek; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; @ApplicationService @SuppressFBWarnings(value="EI2", justification="RoomDomainService is properly injected") @@ -41,8 +47,18 @@ public BookingApplicationService(RoomDomainService roomDomainService, UserDomain } + public static List getItemsOfWorkspace(IWorkspace workspace) { + return workspace.getItems().stream().map(ItemName::type).collect(Collectors.toList()); + } + + public static List convertToItemNameList(List gegenstaende) { + return gegenstaende.stream() + .map(ItemName::new) + .toList(); + } + @PostConstruct - public void initialize(){ + public void initialize() { roomDomainService.addDummyDummy(); } public void addBookEntry(IntermediateBookDataForm form, String userHandle) throws NotFoundException, GeneralDomainException { @@ -52,14 +68,13 @@ public void addBookEntry(IntermediateBookDataForm form, String userHandle) throw List bookedTimeframes = IterableSupport.toList(form.bookingDays().toBookedTimeframes(userHandle)); - try{ + try { for (BookedTimeframe bookedTimeframe : bookedTimeframes) { - roomDomainService.addBooking(bookedTimeframe,workspaceId,roomId); + roomDomainService.addBooking(bookedTimeframe, workspaceId, roomId); } - if(bookedTimeframes.isEmpty()) + if (bookedTimeframes.isEmpty()) throw new GeneralDomainException(); - } - catch (NotFoundRepositoryException e){ + } catch (NotFoundRepositoryException e) { throw new NotFoundException(); } @@ -75,7 +90,7 @@ public Collection getRooms() { public void addRoom(IRoom room) throws NotFoundException { roomDomainService.addRoom(new RoomApplicationData(room.getRoomID(), room.getRoomNumber())); - if(!IterableSupport.toList(room.getWorkspaces()).isEmpty()) + if (!IterableSupport.toList(room.getWorkspaces()).isEmpty()) for (IWorkspace workspace : room.getWorkspaces()) { try { roomDomainService.addWorkspace(room, workspace); @@ -85,14 +100,14 @@ public void addRoom(IRoom room) throws NotFoundException { } } - public IRoom findRoomByID(UUID roomID) throws NotFoundException{ + public IRoom findRoomByID(UUID roomID) throws NotFoundException { try { return roomDomainService.findRoomByID(roomID); } catch (NotFoundRepositoryException e) { throw new NotFoundException(); } } - + public List getItemsOfRoom(UUID roomId) throws NotFoundException { List items = new ArrayList<>(); IRoom room = findRoomByID(roomId); @@ -118,7 +133,7 @@ public List findAvailableWorkspacesWithItems(List it Collection rooms = roomDomainService.getRooms(); List availableRooms = rooms.stream() .filter(room -> IterableSupport.toList(room.getWorkspaces()).stream() - .anyMatch(workspace->RoomDomainService.isWorkspaceAvailable(workspace, bookedTimeframe)) + .anyMatch(workspace -> RoomDomainService.isWorkspaceAvailable(workspace, bookedTimeframe)) ) .toList(); List availableWorkspaces = availableRooms.stream() @@ -128,7 +143,7 @@ public List findAvailableWorkspacesWithItems(List it ) ).toList(); return availableWorkspaces.stream() - .filter(rbm-> new HashSet<>(IterableSupport.toList(rbm.itemNameList())).containsAll(items)) + .filter(rbm -> new HashSet<>(IterableSupport.toList(rbm.itemNameList())).containsAll(items)) .toList(); } @@ -172,6 +187,52 @@ public void removeItem(String itemName) { roomDomainService.removeItem(itemName); } + public List getAllWorkspaces(Collection roomList) { + return roomList.stream() + .flatMap(room -> StreamSupport.stream(room.getWorkspaces().spliterator(), false)) + .collect(Collectors.toList()); + } + + public IWorkspace getWorkspace(IRoom room, UUID workspaceId) throws NotFoundException { + Optional optionalWorkspace = IterableSupport.toList(room.getWorkspaces()).stream() + .filter(x -> x.getId().equals(workspaceId)) + .findFirst(); + if (optionalWorkspace.isEmpty()) + throw new NotFoundException(); + + IWorkspace workspace = optionalWorkspace.get(); + return workspace; + } + + public List getUnusedItems(List UsedItemsOfWorkspace) { + return allItems() + .stream() + .map(ItemName::type) + .filter(type -> !UsedItemsOfWorkspace.contains(type)) + .toList(); + } + + public List getRoomHomeModels() { + return getRooms().stream() + .flatMap(BookingApplicationService::toRoomHomeModel) + .toList(); + } + private static Stream toRoomHomeModel(IRoom room) { + List list = IterableSupport.toList(room.getWorkspaces()).stream() + .filter(workspace -> !IterableSupport.toList(workspace.getBookedTimeframes()).isEmpty()) + .map(workspace -> new RoomHomeModel(room.getRoomID(), + workspace.getId(), + room.getRoomNumber(), + workspace.getWorkspaceNumber(), + DayTimeFrame.from(IterableSupport.toList(workspace.getBookedTimeframes())).convertToString(), + workspace.getItems() + )) + .toList(); + return list.stream(); + } + + + public void addWorkspace(String workspaceString, UUID roomID) throws NotFoundRepositoryException { int workspaceNumber; try { diff --git a/src/main/java/com/example/roommate/controller/AdminController.java b/src/main/java/com/example/roommate/controller/AdminController.java index 616376cd..161d762c 100644 --- a/src/main/java/com/example/roommate/controller/AdminController.java +++ b/src/main/java/com/example/roommate/controller/AdminController.java @@ -35,6 +35,9 @@ public AdminController(BookingApplicationService bookingApplicationService) { public String adminPage(Model model) { Collection itemList = bookingApplicationService.allItems(); Collection roomList = bookingApplicationService.getRooms(); + /* List workspaceList = roomList.stream() + .flatMap(room -> StreamSupport.stream(room.getWorkspaces().spliterator(), false)) + .collect(Collectors.toList());*/ model.addAttribute("itemList", itemList); model.addAttribute("roomList", roomList); diff --git a/src/main/java/com/example/roommate/controller/HomeController.java b/src/main/java/com/example/roommate/controller/HomeController.java index c4f0bcc0..21d87685 100644 --- a/src/main/java/com/example/roommate/controller/HomeController.java +++ b/src/main/java/com/example/roommate/controller/HomeController.java @@ -49,15 +49,15 @@ public String index(Model model, OAuth2AuthenticationToken auth) { } */ - - List roomModels = bookingApplicationService.getRooms().stream() + List roomModels = bookingApplicationService.getRoomHomeModels(); + /* List roomModels = bookingApplicationService.getRooms().stream() .flatMap(HomeController::toRoomHomeModel) - .toList(); + .toList();*/ model.addAttribute("homeModels", roomModels); return "home"; } - private static Stream toRoomHomeModel(IRoom room){ + /* private static Stream toRoomHomeModel(IRoom room){ List list = IterableSupport.toList(room.getWorkspaces()).stream() .filter(workspace -> !IterableSupport.toList(workspace.getBookedTimeframes()).isEmpty()) .map(workspace -> new RoomHomeModel(room.getRoomID(), @@ -69,7 +69,8 @@ private static Stream toRoomHomeModel(IRoom room){ )) .toList(); return list.stream(); - } + }*/ + @PostMapping("/registration") public String registerKey(String keyId, OAuth2AuthenticationToken auth, Model model) { diff --git a/src/main/java/com/example/roommate/controller/RoomController.java b/src/main/java/com/example/roommate/controller/RoomController.java index b3aa1900..1da2cd19 100644 --- a/src/main/java/com/example/roommate/controller/RoomController.java +++ b/src/main/java/com/example/roommate/controller/RoomController.java @@ -5,7 +5,6 @@ import com.example.roommate.application.services.AdminApplicationService; import com.example.roommate.exceptions.ArgumentValidationException; import com.example.roommate.interfaces.entities.IWorkspace; -import com.example.roommate.utility.IterableSupport; import com.example.roommate.values.domainValues.*; import com.example.roommate.exceptions.applicationService.NotFoundException; import com.example.roommate.interfaces.entities.IRoom; @@ -13,6 +12,7 @@ import com.example.roommate.values.forms.BookDataForm; import com.example.roommate.application.services.BookingApplicationService; import com.example.roommate.values.forms.RoomDataForm; +import com.example.roommate.values.forms.SearchTimeForm; import com.example.roommate.values.models.RoomBookingModel; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import jakarta.validation.Valid; @@ -28,7 +28,6 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes; import java.util.*; -import java.util.stream.Collectors; @Controller @SuppressFBWarnings(value="EI2", justification="BookingApplicationService & AdminApplicationService are properly injected") @@ -48,27 +47,24 @@ public RoomController(BookingApplicationService bookingApplicationService, Admin // http://localhost:8080/rooms?datum=1221-12-21&uhrzeit=12%3A21&gegenstaende=Table&gegenstaende=Desk @GetMapping("/rooms") - public String changeBookings(@RequestParam(required = false) List gegenstaende, - @RequestParam(required = false) String datum, - @RequestParam(required = false) String startUhrzeit, @RequestParam(required = false) String endUhrzeit, - Model model, + public String changeBookings(@RequestParam(required = false) List gegenstaende, /*@RequestParam(required = false)*/ SearchTimeForm timeForm, Model model, OAuth2AuthenticationToken auth) { - if (datum == null) datum = "2024-01-01"; - if (startUhrzeit == null) startUhrzeit = "08:00"; - if (endUhrzeit == null) endUhrzeit = "16:00"; + /* if (timeForm.datum == null) timeForm.datum = "2024-01-01"; + if (timeForm.startUhrzeit == null) timeForm.startUhrzeit = "08:00"; + if (timeForm.endUhrzeit == null) timeForm.endUhrzeit = "16:00";*/ if (gegenstaende == null) gegenstaende = new ArrayList<>(); - List selectedItemsList = gegenstaende.stream() + List selectedItemsList = BookingApplicationService.convertToItemNameList(gegenstaende); + /* List selectedItemsList = gegenstaende.stream() .map(ItemName::new) - .toList(); - + .toList();*/ OAuth2User user = auth.getPrincipal(); String userHandle = user.getAttribute("login"); - List availableWorkspacesWithItems = bookingApplicationService.findAvailableWorkspacesWithItems(selectedItemsList, datum, startUhrzeit, endUhrzeit, userHandle); - model.addAttribute("date", datum); - model.addAttribute("startTime", startUhrzeit); - model.addAttribute("endTime", endUhrzeit); + List availableWorkspacesWithItems = bookingApplicationService.findAvailableWorkspacesWithItems(selectedItemsList, timeForm.datum(), timeForm.startUhrzeit(), timeForm.endUhrzeit(), userHandle); + model.addAttribute("date", timeForm.datum()); + model.addAttribute("startTime", timeForm.startUhrzeit()); + model.addAttribute("endTime", timeForm.endUhrzeit()); model.addAttribute("items", bookingApplicationService.allItems()); model.addAttribute("gegenstaende", gegenstaende); model.addAttribute("roomBookingModels", availableWorkspacesWithItems); @@ -92,18 +88,21 @@ public String addRoom(RoomDataForm roomDataForm){ public ModelAndView roomDetails(Model model, @PathVariable UUID roomId, @PathVariable UUID workspaceId) { try { IRoom room = bookingApplicationService.findRoomByID(roomId); - Optional optionalWorkspace = IterableSupport.toList(room.getWorkspaces()).stream() + IWorkspace workspace = bookingApplicationService.getWorkspace(room, workspaceId); + /* Optional optionalWorkspace = IterableSupport.toList(room.getWorkspaces()).stream() .filter(x -> x.getId().equals(workspaceId)) - .findFirst(); - if(optionalWorkspace.isEmpty()) - throw new NotFoundException(); - IWorkspace workspace = optionalWorkspace.get(); - List itemsOfWorkspace = workspace.getItems().stream().map(ItemName::type).collect(Collectors.toList()); - List filteredItems = bookingApplicationService.allItems() + .findFirst();*/ + /* if(optionalWorkspace.isEmpty()) + throw new NotFoundException();*/ + // IWorkspace workspace = optionalWorkspace.get(); + List itemsOfWorkspace = BookingApplicationService.getItemsOfWorkspace(workspace); + // List itemsOfWorkspace = workspace.getItems().stream().map(ItemName::type).collect(Collectors.toList()); + List filteredItems = bookingApplicationService.getUnusedItems(itemsOfWorkspace); + /* List filteredItems = bookingApplicationService.allItems() .stream() .map(ItemName::type) .filter(type -> !itemsOfWorkspace.contains(type)) - .toList(); + .toList();*/ DayTimeFrame dayTimeFrame = DayTimeFrame.from(workspace.getBookedTimeframes()); model.addAttribute("frame",dayTimeFrame); @@ -124,6 +123,7 @@ public ModelAndView roomDetails(Model model, @PathVariable UUID roomId, @PathVar @VerifiedOnly @PostMapping("/rooms") + public ModelAndView addBooking(@Valid BookDataForm form , BindingResult bindingResult , RedirectAttributes redirectAttributes diff --git a/src/main/java/com/example/roommate/values/forms/SearchTimeForm.java b/src/main/java/com/example/roommate/values/forms/SearchTimeForm.java new file mode 100644 index 00000000..e4bf0fe3 --- /dev/null +++ b/src/main/java/com/example/roommate/values/forms/SearchTimeForm.java @@ -0,0 +1,39 @@ +package com.example.roommate.values.forms; + +public record SearchTimeForm (String datum,String startUhrzeit, String endUhrzeit){ + + public SearchTimeForm(String datum, String startUhrzeit, String endUhrzeit) { + if (datum == null) datum = "2024-01-01"; + if (startUhrzeit == null) startUhrzeit = "08:00"; + if (endUhrzeit == null) endUhrzeit = "16:00"; + + this.datum = datum; + this.startUhrzeit = startUhrzeit; + this.endUhrzeit = endUhrzeit; + } + /* public SearchTimeForm(){}*/ +// public void setDatum(String datum){ +// this.datum = datum; +// } +// +// public void setStartUhrzeit(String startUhrzeit){ +// this.startUhrzeit = startUhrzeit; +// } +// +// public void setEndUhrzeit(String endUhrzeit){ +// this.endUhrzeit = endUhrzeit; +// } + + +// public String getDatum() { +// return datum; +// } +// +// public String getStartUhrzeit() { +// return startUhrzeit; +// } +// +// public String getEndUhrzeit() { +// return endUhrzeit; +// } +} diff --git a/src/test/java/com/example/roommate/tests/controller/booking/GetRoomIDTest.java b/src/test/java/com/example/roommate/tests/controller/booking/GetRoomIDTest.java index 2702c9b6..13843a79 100644 --- a/src/test/java/com/example/roommate/tests/controller/booking/GetRoomIDTest.java +++ b/src/test/java/com/example/roommate/tests/controller/booking/GetRoomIDTest.java @@ -49,6 +49,7 @@ public class GetRoomIDTest { void test_1() throws Exception { Room office = Officer.Room(); Workspace officeWorkspace = Officer.Workspace(); + when(bookingApplicationService.getWorkspace(office, officeWorkspace.getId())).thenReturn(officeWorkspace); when(bookingApplicationService.findRoomByID(office.getRoomID())).thenReturn(office); MvcResult result = mvc.perform(get("/room/{roomId}/workspace/{workspaceId}", office.getRoomID().toString(), officeWorkspace.getId().toString())) diff --git a/src/test/java/com/example/roommate/tests/controller/room/GetRoomTest.java b/src/test/java/com/example/roommate/tests/controller/room/GetRoomTest.java index 5dfaf20a..e12598e6 100644 --- a/src/test/java/com/example/roommate/tests/controller/room/GetRoomTest.java +++ b/src/test/java/com/example/roommate/tests/controller/room/GetRoomTest.java @@ -48,6 +48,7 @@ public class GetRoomTest { public void test_1() throws Exception { Room office = Officer.Room(); Workspace officeWorkspace = Officer.Workspace(); + when(bookingApplicationService.getWorkspace(office, officeWorkspace.getId())).thenReturn(officeWorkspace); when(bookingApplicationService.findRoomByID(office.getRoomID())).thenReturn(office); MvcResult result = mvc.perform(get("/room/{roomId}/workspace/{workspaceId}",office.getRoomID(), officeWorkspace.getId()))