Skip to content

Commit 818a4b9

Browse files
committed
feat: rewire routing for publisher and index
1 parent f2d5cb2 commit 818a4b9

File tree

11 files changed

+76
-42
lines changed

11 files changed

+76
-42
lines changed

src/main/java/edu/kit/hci/soli/controller/LayoutParamsAdvice.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import edu.kit.hci.soli.domain.User;
66
import edu.kit.hci.soli.dto.LayoutParams;
77
import edu.kit.hci.soli.dto.LoginStateModel;
8-
import edu.kit.hci.soli.service.BookingsService;
9-
import edu.kit.hci.soli.service.TimeService;
10-
import edu.kit.hci.soli.service.UserService;
8+
import edu.kit.hci.soli.service.*;
119
import jakarta.servlet.http.HttpServletRequest;
1210
import lombok.extern.slf4j.Slf4j;
1311
import org.jetbrains.annotations.Nullable;
@@ -25,6 +23,7 @@ public class LayoutParamsAdvice {
2523
private final UserService userService;
2624
private final BookingsService bookingsService;
2725
private final TimeService timeService;
26+
private final RoomService roomService;
2827

2928
/**
3029
* Constructs a LoginControllerAdvice with the specified UserService.
@@ -33,10 +32,11 @@ public class LayoutParamsAdvice {
3332
* @param bookingsService the service for managing bookings
3433
* @param timeService the service for managing time
3534
*/
36-
public LayoutParamsAdvice(UserService userService, BookingsService bookingsService, TimeService timeService) {
35+
public LayoutParamsAdvice(UserService userService, BookingsService bookingsService, TimeService timeService, RoomService roomService) {
3736
this.userService = userService;
3837
this.bookingsService = bookingsService;
3938
this.timeService = timeService;
39+
this.roomService = roomService;
4040
}
4141

4242
/**
@@ -86,7 +86,7 @@ public LayoutParams getLayoutParams(@ModelAttribute("login") LoginStateModel log
8686
room -> {
8787
request.getSession().setAttribute("room", room);
8888
return getUpdate(room, login.user());
89-
});
89+
}, roomService.count() > 1);
9090
}
9191

9292
private LayoutParams.ParamsUpdate getUpdate(@Nullable Room room, User user) {

src/main/java/edu/kit/hci/soli/controller/MainController.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import edu.kit.hci.soli.config.SoliConfiguration;
44
import edu.kit.hci.soli.domain.Room;
55
import edu.kit.hci.soli.dto.KnownError;
6+
import edu.kit.hci.soli.dto.LayoutParams;
67
import edu.kit.hci.soli.service.RoomService;
78
import jakarta.servlet.http.HttpServletRequest;
89
import jakarta.servlet.http.HttpServletResponse;
@@ -12,9 +13,7 @@
1213
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
1314
import org.springframework.stereotype.Controller;
1415
import org.springframework.ui.Model;
15-
import org.springframework.web.bind.annotation.GetMapping;
16-
import org.springframework.web.bind.annotation.RequestMapping;
17-
import org.springframework.web.bind.annotation.ResponseBody;
16+
import org.springframework.web.bind.annotation.*;
1817

1918
import java.util.List;
2019
import java.util.Map;
@@ -100,6 +99,26 @@ public String securityTxt(HttpServletResponse response) {
10099
""".formatted(soliConfiguration.getHostname());
101100
}
102101

102+
/**
103+
* Handles GET requests to the root endpoint.
104+
* If there is only one room, redirect to that room.
105+
*
106+
* @param model the model to which attributes are added
107+
* @param layout the layout parameters
108+
* @return the name of the view to be rendered
109+
*/
110+
@GetMapping("/")
111+
public String index(Model model, @ModelAttribute("layout") LayoutParams layout) {
112+
List<Room> rooms = roomService.getAll();
113+
if (rooms.size() == 1) {
114+
return "redirect:/" + rooms.getFirst().getId();
115+
}
116+
layout.setRoom(null);
117+
model.addAttribute("rooms", rooms);
118+
model.addAttribute("roomsFirst", true);
119+
return "index";
120+
}
121+
103122
/**
104123
* Returns the view for the publisher information (impressum).
105124
*
@@ -110,6 +129,7 @@ public String securityTxt(HttpServletResponse response) {
110129
public String getPublisher(Model model) {
111130
List<Room> rooms = roomService.getAll();
112131
model.addAttribute("rooms", rooms);
113-
return "publisher";
132+
model.addAttribute("roomsFirst", false);
133+
return "index";
114134
}
115135
}

src/main/java/edu/kit/hci/soli/controller/RoomsController.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,6 @@ public RoomsController(RoomService roomService) {
3232
this.roomService = roomService;
3333
}
3434

35-
/**
36-
* Handles GET requests to the root endpoint.
37-
* If there is only one room, redirects to that room.
38-
*
39-
* @param model the model to which attributes are added
40-
* @param layout the layout parameters
41-
* @return the name of the view to be rendered
42-
*/
43-
@GetMapping("/")
44-
public String roomList(Model model,
45-
@ModelAttribute("layout") LayoutParams layout) {
46-
List<Room> rooms = roomService.getAll();
47-
if (rooms.size() == 1) {
48-
return "redirect:/" + rooms.get(0).getId();
49-
}
50-
layout.setRoom(null);
51-
model.addAttribute("rooms", rooms);
52-
return "rooms";
53-
}
54-
5535
/**
5636
* Shows the room list in edit mode.
5737
*
@@ -64,7 +44,7 @@ public String adminRoomList(Model model, @ModelAttribute("layout") LayoutParams
6444
List<Room> rooms = roomService.getAll();
6545
model.addAttribute("rooms", rooms);
6646
model.addAttribute("edit", true);
67-
return "rooms";
47+
return "admin/rooms/list";
6848
}
6949

7050
/**
@@ -80,7 +60,7 @@ public String newRoom(Model model, @ModelAttribute("layout") LayoutParams layout
8060
model.addAttribute("name", "");
8161
model.addAttribute("description", "");
8262
model.addAttribute("location", "");
83-
return "admin/edit_room_page";
63+
return "admin/rooms/edit";
8464
}
8565

8666
/**
@@ -104,7 +84,7 @@ public String editRoom(Model model, HttpServletResponse response,
10484
model.addAttribute("name", room.get().getName());
10585
model.addAttribute("description", room.get().getDescription());
10686
model.addAttribute("location", room.get().getLocation());
107-
return "admin/edit_room_page";
87+
return "admin/rooms/edit";
10888
}
10989

11090
/**
@@ -188,5 +168,4 @@ public String editOrCreateRoom(Model model, HttpServletResponse response, @Model
188168
roomService.save(room);
189169
return "redirect:/admin/rooms";
190170
}
191-
192171
}

src/main/java/edu/kit/hci/soli/dto/LayoutParams.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
public class LayoutParams {
1010
private final LoginStateModel login;
1111
private final RoomChangeListener onRoomChange;
12+
private final boolean hasMultipleRooms;
1213
private @Nullable Room room;
1314
private @Nullable Booking currentHighestBooking;
1415
private @Nullable Booking currentBookingOfUser;
1516

16-
public LayoutParams(@NotNull LoginStateModel login, @Nullable Room room, @NotNull RoomChangeListener onRoomChange) {
17+
public LayoutParams(@NotNull LoginStateModel login, @Nullable Room room, @NotNull RoomChangeListener onRoomChange, boolean hasMultipleRooms) {
1718
this.login = Objects.requireNonNull(login);
1819
this.room = room;
1920
this.onRoomChange = Objects.requireNonNull(onRoomChange);
21+
this.hasMultipleRooms = hasMultipleRooms;
2022
ParamsUpdate paramsUpdate = onRoomChange.onRoomChange(room);
2123
this.currentHighestBooking = paramsUpdate.currentHighestBooking();
2224
this.currentBookingOfUser = paramsUpdate.currentBookingOfUser();
@@ -45,6 +47,10 @@ public void setRoom(@Nullable Room room) {
4547
return currentBookingOfUser;
4648
}
4749

50+
public boolean isMultipleRooms() {
51+
return hasMultipleRooms;
52+
}
53+
4854
/**
4955
* Functional interface for listening to room changes on a {@link LayoutParams}
5056
* and modifying relevant fields as needed.

src/main/java/edu/kit/hci/soli/service/RoomService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public interface RoomService {
3333
*/
3434
List<Room> getAll();
3535

36+
/**
37+
* Retrieves the number of rooms.
38+
*
39+
* @return the number of rooms
40+
*/
41+
long count();
42+
3643
/**
3744
* Creates a new room.
3845
*

src/main/java/edu/kit/hci/soli/service/impl/RoomServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public List<Room> getAll() {
3636
return roomRepository.findAll();
3737
}
3838

39+
@Override
40+
public long count() {
41+
return roomRepository.count();
42+
}
43+
3944
@Override
4045
public Room save(Room room) {
4146
return roomRepository.save(room);
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
@param boolean edit = false
1313

1414
@template.layout(page = context.page("rooms"), layout = layout, context = context, content = @`
15-
@template.util.room_list(context = context, layout = layout, csrf = csrf, rooms = rooms, edit = edit)
15+
<div class="mx-auto">
16+
@template.util.room_list(context = context, layout = layout, csrf = csrf, rooms = rooms, edit = edit)
17+
</div>
1618
`)
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99
@param CsrfToken csrf
1010

1111
@param List<Room> rooms
12+
@param boolean roomsFirst
1213

1314
@template.layout(page = context.page("layout.publisher"), context = context, layout = layout, content = @`
14-
<div class="container">
15+
<div class="mx-auto" style="max-width: 100ch;">
16+
@if (roomsFirst)
17+
<h1 class="text-4xl font-bold">${context.localize("rooms")}</h1>
18+
<br>
19+
@template.util.room_list(context = context, layout = layout, csrf = csrf, rooms = rooms)
20+
<br><br>
21+
@endif
22+
1523
<h1 class="text-4xl font-bold">${context.localize("layout.publisher")}</h1>
24+
<br>
1625
<p>${context.localize("publisher.description")}</p>
1726

1827
<br>
@@ -23,9 +32,11 @@
2332
</p>
2433
<a class="link link-primary" href="https://hci.iar.kit.edu/deutsch/index.php">HCI (Human-Computer Interaction and Accessibility)</a>
2534

26-
<br><br>
27-
28-
<h1 class="text-4xl font-bold">${context.localize("rooms")}</h1>
29-
@template.util.room_list(context = context, layout = layout, csrf = csrf, rooms = rooms)
35+
@if (!roomsFirst)
36+
<br><br>
37+
<h1 class="text-4xl font-bold">${context.localize("rooms")}</h1>
38+
<br>
39+
@template.util.room_list(context = context, layout = layout, csrf = csrf, rooms = rooms)
40+
@endif
3041
</div>
3142
`)

src/main/jte/layout.jte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@
146146

147147
@if(layout.getRoom() != null)
148148
<div class="divider"></div>
149-
<li><a href="/"><i class="fa-solid fa-folder-tree"></i> ${context.localize("layout.current_room", layout.getRoom().getName())}</a></li>
149+
@if (layout.isMultipleRooms())
150+
<li><a href="/"><i class="fa-solid fa-folder-tree"></i> ${context.localize("layout.current_room", layout.getRoom().getName())}</a></li>
151+
@else
152+
<li class="pointer-events-none"><label><i class="fa-solid fa-circle-info"></i> ${context.localize("layout.current_room", layout.getRoom().getName())}</label></li>
153+
@endif
150154
@endif
151155

152156
@if(layout.getLogin().kind() == LoginStateModel.Kind.ADMIN)

0 commit comments

Comments
 (0)