diff --git a/src/main/java/com/adpro/tasc/controller/BookAppointmentController.java b/src/main/java/com/adpro/tasc/controller/BookAppointmentController.java index b07d3c5e5ee60e4051b633acf65d10231909acb2..56089f2bb888b6a8b208953bf77223467054da58 100644 --- a/src/main/java/com/adpro/tasc/controller/BookAppointmentController.java +++ b/src/main/java/com/adpro/tasc/controller/BookAppointmentController.java @@ -8,6 +8,7 @@ import com.adpro.tasc.user.db.dao.UserDAO; import com.adpro.tasc.user.db.model.AcademicUser; import com.adpro.tasc.user.db.model.User; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -21,6 +22,7 @@ import java.time.YearMonth; import java.util.*; @Controller +@PreAuthorize("hasRole('STUDENT')") public class BookAppointmentController { @Autowired AppointmentDAO appointmentDAO; @@ -98,8 +100,8 @@ public class BookAppointmentController { public String bookAppointment( Model model, Principal principal, - @RequestParam("startTime") int startTime, - @RequestParam("finishTime") int finishTime, + @RequestParam("startTime") long startTime, + @RequestParam("finishTime") long finishTime, @RequestParam("day") String day, @RequestParam("taUserName") String taUserName, @RequestParam("courseName") String courseName, @@ -180,57 +182,63 @@ public class BookAppointmentController { @RequestParam("slotStartTime") long slotStartTime, @RequestParam("slotFinishTime") long slotFinishTime, @RequestParam("bookDateWeekOffset") int bookDateWeekOffset, - @RequestParam("startHourForm") int startHour, - @RequestParam("endHourForm") int endHour, - @RequestParam("startMinuteForm") int startMinute, - @RequestParam("endMinuteForm") int endMinute, + @RequestParam("startTimeForm") String startTime, + @RequestParam("endTimeForm") String endTime, @RequestParam("targetBookDate") long targetBookMillis, @RequestParam("taUserName") String taUserName, @RequestParam("courseName") String courseName ) { - boolean valid =true; - - long targetBookEpochStart = java.time.Duration.ofMinutes(startMinute).toMillis() + - java.time.Duration.ofHours(startHour).toMillis(); - long targetBookEpochEnd = java.time.Duration.ofMinutes(endMinute).toMillis() + - java.time.Duration.ofHours(endHour).toMillis(); - - if ( - !( - targetBookEpochStart < targetBookEpochEnd && - targetBookEpochStart >= slotStartTime && - targetBookEpochStart < slotFinishTime && - targetBookEpochEnd <= slotFinishTime && - targetBookEpochEnd > slotStartTime - ) || - !( - startHour >= 0 && startHour < 24 && - startMinute >= 0 && startMinute < 60 && - endHour >= 0 && endHour < 24 && - endMinute >= 0 && endMinute < 60 - ) - - ) { - valid = false; - } - if (!valid) { - System.out.println("invalid"); - String referer = request.getHeader("Referer").replaceAll("false","true"); - System.out.println(referer); - return "redirect:"+referer; - } else { + try { + + String[] splittedStartTime = startTime.split("\\."); + String[] splittedEndTime = endTime.split("\\."); + + if (splittedStartTime.length != 2 || splittedEndTime.length != 2) { + throw new Exception(""); + } + + int startHour = Integer.parseInt(splittedStartTime[0]); + int startMinute = Integer.parseInt(splittedStartTime[1]); + int endHour = Integer.parseInt(splittedEndTime[0]); + int endMinute = Integer.parseInt(splittedEndTime[1]); + + long targetBookEpochStart = java.time.Duration.ofMinutes(startMinute).toMillis() + + java.time.Duration.ofHours(startHour).toMillis(); + long targetBookEpochEnd = java.time.Duration.ofMinutes(endMinute).toMillis() + + java.time.Duration.ofHours(endHour).toMillis(); + + if ( + !( + targetBookEpochStart < targetBookEpochEnd && + targetBookEpochStart >= slotStartTime && + targetBookEpochStart < slotFinishTime && + targetBookEpochEnd <= slotFinishTime && + targetBookEpochEnd > slotStartTime + ) || + !( + startHour >= 0 && startHour < 24 && + startMinute >= 0 && startMinute < 60 && + endHour >= 0 && endHour < 24 && + endMinute >= 0 && endMinute < 60 + ) + + ) { + throw new Exception(""); + } + + System.out.println("valid"); Calendar calendarBookDate = Calendar.getInstance(); calendarBookDate.setTimeInMillis(targetBookMillis); int targetBookDate = calendarBookDate.get(Calendar.DATE); - int targetBookMonth = calendarBookDate.get(Calendar.MONTH)+1; + int targetBookMonth = calendarBookDate.get(Calendar.MONTH); int targetBookYear = calendarBookDate.get(Calendar.YEAR); long duration = targetBookEpochEnd - targetBookEpochStart; - calendarBookDate.set(targetBookYear,targetBookMonth,targetBookDate,startHour,startMinute); - calendarBookDate.add(Calendar.DATE,bookDateWeekOffset*7); + calendarBookDate.set(targetBookYear, targetBookMonth, targetBookDate, startHour, startMinute); + calendarBookDate.add(Calendar.DATE, bookDateWeekOffset * 7); long targetBookEpochDate = calendarBookDate.getTimeInMillis(); Appointment appointment = new Appointment(); @@ -249,6 +257,12 @@ public class BookAppointmentController { appointmentDAO.createAppointment(appointmentRequest); return "redirect:/see-appointment-student"; + + } catch (Exception e) { + System.out.println("invalid"); + String referer = request.getHeader("Referer").replaceAll("false", "true"); + System.out.println(referer); + return "redirect:" + referer; } } } diff --git a/src/main/java/com/adpro/tasc/controller/CreateScheduleController.java b/src/main/java/com/adpro/tasc/controller/CreateScheduleController.java index 3359d3057e9035ab8320782e5500fedab5b52612..a20e871e759c9b2ec93f7202cf4481a98d713645 100644 --- a/src/main/java/com/adpro/tasc/controller/CreateScheduleController.java +++ b/src/main/java/com/adpro/tasc/controller/CreateScheduleController.java @@ -73,32 +73,60 @@ public class CreateScheduleController { @PostMapping(value="/add-slot/post") public String createSlot( @RequestParam("day") int day, - @RequestParam("startHour") int startHour, - @RequestParam("startMinute") int startMinute, - @RequestParam("endHour") int endHour, - @RequestParam("endMinute") int endMinute, + @RequestParam("startTime") String startTime, + @RequestParam("endTime") String endTime, Principal principal ) { - System.out.println(day+" "+startHour+ " " + startMinute); + try { + String[] splittedStartTime = startTime.split("\\."); + String[] splittedEndTime = endTime.split("\\."); + + if (splittedStartTime.length != 2 || splittedEndTime.length != 2) { + throw new Exception(""); + } + + int startHour = Integer.parseInt(splittedStartTime[0]); + int startMinute = Integer.parseInt(splittedStartTime[1]); + int endHour = Integer.parseInt(splittedEndTime[0]); + int endMinute = Integer.parseInt(splittedEndTime[1]); + + long epochStart = java.time.Duration.ofMinutes(startMinute).toMillis() + + java.time.Duration.ofHours(startHour).toMillis(); + + long epochEnd = java.time.Duration.ofMinutes(endMinute).toMillis() + + java.time.Duration.ofHours(endHour).toMillis(); + + if ( + !( + startHour >= 0 && startHour < 24 && + startMinute >= 0 && startMinute < 60 && + endHour >= 0 && endHour < 24 && + endMinute >= 0 && endMinute < 60 && + epochStart < epochEnd + ) + ) { + throw new Exception(""); + } - long epochStart = java.time.Duration.ofMinutes(startMinute).toMillis() + - java.time.Duration.ofHours(startHour).toMillis(); + Slot slot1 = new Slot(); + slot1.setDay(Slot.Day.valueOf(daysArr[day])); + slot1.setStartTime(epochStart); + slot1.setFinishTime(epochEnd); - long epochEnd = java.time.Duration.ofMinutes(endMinute).toMillis() + - java.time.Duration.ofHours(endHour).toMillis(); + User currentUser = userDAO.getUser(principal.getName()); + Schedule schedule = scheduleDAO.getUserSchedule((AcademicUser) currentUser); + slot1.setSchedule(schedule.getId()); - Slot slot1 = new Slot(); - slot1.setDay(Slot.Day.valueOf(daysArr[day])); - slot1.setStartTime(epochStart); - slot1.setFinishTime(epochEnd); +// slots.add(slot1); - User currentUser = userDAO.getUser(principal.getName()); - Schedule schedule = scheduleDAO.getUserSchedule((AcademicUser) currentUser); - slot1.setSchedule(schedule.getId()); + slotDAO.addSlot(slot1); + + return "redirect:/create-schedule"; + } catch (Exception e) { + return "redirect:/add-slot"; + } - slotDAO.addSlot(slot1); - return "redirect:/create-schedule"; } } diff --git a/src/main/java/com/adpro/tasc/controller/UserListCoursesController.java b/src/main/java/com/adpro/tasc/controller/UserListCoursesController.java index c0a8787c782aa651318f248cc707cb1aab807f5f..f0d78bd2abe12ab3c21690a5f7f2cff7668a59be 100644 --- a/src/main/java/com/adpro/tasc/controller/UserListCoursesController.java +++ b/src/main/java/com/adpro/tasc/controller/UserListCoursesController.java @@ -30,6 +30,7 @@ public class UserListCoursesController { List<Course> courseList = courseDAO.getAllCourse(); List<Course> userCourseList = courseDAO.getUserCourseList((AcademicUser) currentUser); + model.addAttribute("currentUser", currentUser); model.addAttribute("courseList", courseList); model.addAttribute("userCourseList",userCourseList); diff --git a/src/main/resources/templates/book_appointment/book_appointment.html b/src/main/resources/templates/book_appointment/book_appointment.html index 8db345400d7d264230fe9e2414552a33cdcd436b..8148391d702346b1b08820fa74ea70b7425ba986 100644 --- a/src/main/resources/templates/book_appointment/book_appointment.html +++ b/src/main/resources/templates/book_appointment/book_appointment.html @@ -18,7 +18,9 @@ <nav th:replace="${currentUser.getRole().toString() == 'ROLE_STUDENT'} ? ~{fragments/navbar_student :: navbar}"></nav> <div style="padding:2em;" > <div> - <form th:action="${currentUser.getRole().toString() == 'ROLE_STUDENT'} ? @{/book-appointment/pick-ta}"> + <form th:action="@{/book-appointment/see-ta-schedule}"> + <input id = "courseNameBackTrack" name = "courseName" type = "hidden" th:value = "${courseName}"/> + <input id = "taUserNameBackTrack" name = "taUserName" type = "hidden" th:value = "${taUserName}"/> <button type="submit" class="btn btn-danger">Back</button> </form> </div> @@ -26,10 +28,10 @@ <h2>Book an appointment</h2> </div> <div> - <div style="border-width: 1px; border-radius: 10px; border-color:grey; border-style: solid;padding:3em"> - <h5> + <div style="border-width: 1px; border-radius: 10px; border-color:grey; border-style: solid;padding:2em; margin: 1em 20vw"> + <h4> For slot: - </h5> + </h4> <div> <h6>TA:</h6> <p th:text="${taUserName}"></p> @@ -43,48 +45,61 @@ <p th:text="${day}"></p> </div> <div> - <h6>Start time:</h6> - <p th:text="${(startTime / (1000 * 60 * 60))%24}+'.'+${(startTime / 60000) % 60}"></p> + <h6>Time:</h6> + <p th:text="${#dates.format(startTime-25200000, 'HH:mm')}+' - '+${#dates.format(finishTime-25200000,'HH:mm')}"></p> </div> - <div> - <h6>Finish time:</h6> - <p th:text="${(finishTime / (1000 * 60 * 60))%24}+'.'+${(finishTime / 60000) % 60}"></p> +<!-- <div style="flex-direction: row; display: flex">--> +<!-- <div style="margin-right: 0.5em">--> +<!-- <h6>Start time:</h6>--> +<!-- <p th:text="${(startTime / (1000 * 60 * 60))%24}+'.'+${(startTime / 60000) % 60}"></p>--> +<!-- </div>--> +<!-- <div>--> +<!-- <h6>Finish time:</h6>--> +<!-- <p th:text="${(finishTime / (1000 * 60 * 60))%24}+'.'+${(finishTime / 60000) % 60}"></p>--> +<!-- </div>--> </div> </div> - <div> + <div style="padding: 0.5em 3em; text-align: center"> <h5>Enter values within above time slot range:</h5> <form th:action="@{/book-appointment/validate-book}" id="bookAppointment" method="post"> <div th:if="${error == 'true'}" class="alert alert-danger" role="alert"> Invalid start and end time! </div> - - <div> - <label for="bookDates">Date:</label> - <select id="bookDates" name="bookDateWeekOffset"> - <option th:each="date,iStat: ${targetBookDates}" th:text="${date}" th:value="${iStat.index}"></option> - </select> - </div> - - <h6>Start time:</h6> - <div> - <label for="startHour">Hours:</label> - <input id = "startHour" name = "startHourForm" type = "number" min="0" max="23" value="0"/> - </div> - - <div> - <label for="startMinute">Minute:</label> - <input id = "startMinute" name = "startMinuteForm" type = "number" min="0" max="59" value="0"/> +<!-- <div>--> +<!-- <label for="bookDates">Date:</label>--> +<!-- <select id="bookDates" name="bookDateWeekOffset">--> +<!-- <option th:each="date,iStat: ${targetBookDates}" th:text="${date}" th:value="${iStat.index}"></option>--> +<!-- </select>--> +<!-- </div>--> + <div class="form-group" style="flex-direction: row; display: flex;justify-content: center; align-items: center"> + <div style="margin-right: 1em"> + <h6>Pick a date:</h6> + </div> + <div> + <select id="bookDates" name="bookDateWeekOffset" class="form-control"> + <option th:each="date,iStat: ${targetBookDates}" th:text="${date}" th:value="${iStat.index}"></option> + </select> + </div> </div> - <h6>End time:</h6> - <div> - <label for="endHour">Hours:</label> - <input id = "endHour" name = "endHourForm" type = "number" min="0" max="23" value="0"/> + <br/> + <div style="flex-direction: row; display: flex; justify-content: center; align-items: center"> + <div> + <h6>Start time:</h6> + </div> + <div style="padding: 0em 0.5em"> + <input class="form-control" id = "startTime" name = "startTimeForm" type = "text" placeholder="11.59"/> + </div> </div> - <div> - <label for="endMinute">Minute:</label> - <input id = "endMinute" name = "endMinuteForm" type = "number" min="0" max="59" value="0"/> + <br/> + <div style="flex-direction: row; display: flex; justify-content: center; align-items: center"> + <div> + <h6>End time:</h6> + </div> + <div style="padding: 0em 0.5em"> + <input class="form-control" id = "endTime" name = "endTimeForm" type = "text" placeholder="13.05"/> + </div> </div> <p th:value="${error}"></p> diff --git a/src/main/resources/templates/book_appointment/see_ta_schedule.html b/src/main/resources/templates/book_appointment/see_ta_schedule.html index 3c830817a245766aadfc4084b296441ee0829c6b..40a69f9abbab8bf49c18552d761c17b807ac80e4 100644 --- a/src/main/resources/templates/book_appointment/see_ta_schedule.html +++ b/src/main/resources/templates/book_appointment/see_ta_schedule.html @@ -39,7 +39,7 @@ <tbody> <tr th:each="slot: ${slots}"> <td th:text="${slot.day.toString()}"></td> - <td th:text="${(slot.startTime / (1000 * 60 * 60))%24}+'.'+${(slot.startTime / 60000) % 60}+' - '+${(slot.finishTime / (1000 * 60 * 60))%24}+'.'+${(slot.finishTime / 60000) % 60}"></td> + <td th:text="${#dates.format(slot.startTime-25200000, 'HH:mm')}+' - '+${#dates.format(slot.finishTime-25200000,'HH:mm')}"></td> <td> <form th:action="@{/book-appointment/book}"> <input id = "startTime" name = "startTime" type = "hidden" th:value = "${slot.startTime}"/> diff --git a/src/main/resources/templates/create_slot.html b/src/main/resources/templates/create_slot.html index b0f39879df5a6718f97207c1e2d51e6788d2cb34..4f63918e74ec38f0784b17353edef6cadcdbc001 100644 --- a/src/main/resources/templates/create_slot.html +++ b/src/main/resources/templates/create_slot.html @@ -16,7 +16,7 @@ </head> <body> <nav th:replace="${currentUser.getRole().toString() == 'ROLE_TEACHING_ASSISTANT'} ? ~{fragments/navbar_TA :: navbar}"></nav> -<div style="padding:2em; text-align: center"> +<div style="padding:2em 4em;"> <div> <form th:action="${currentUser.getRole().toString() == 'ROLE_TEACHING_ASSISTANT'} ? @{/create-schedule}"> <button type="submit" class="btn btn-danger">Back</button> @@ -27,34 +27,57 @@ </div> <div> <form th:action="@{/add-slot/post}" method="POST" > - <div> - <label for="day">Day:</label> - <select id="day" name="day"> - <option th:each="day,iStat: ${days}" th:text="${day}" th:value="${iStat.index}"></option> - </select> + <div class="form-group" style="flex-direction: row; display: flex"> + <div style="margin-right: 1em"> + <h5>Pick a day:</h5> + </div> + <div> + <select id="day" name="day" class="form-control"> + <option th:each="day,iStat: ${days}" th:text="${day}" th:value="${iStat.index}"></option> + </select> + </div> </div> - <h4>Start time:</h4> - <div> - <label for="startHour">Hours:</label> - <input id = "startHour" name = "startHour" type = "number" min="0" max="23" value="0"/> + <div class="form-group" style="flex-direction: row; display: flex;"> + <div> + <h5>Start time:</h5> + </div> + <div style="padding: 0em 0.5em"> + <input class="form-control" id = "startTime" name = "startTime" type = "text" placeholder="11.59"/> + </div> </div> - <div> - <label for="startMinute">Minute:</label> - <input id = "startMinute" name = "startMinute" type = "number" min="0" max="59" value="0"/> + <br/> + <div class="form-group" style="flex-direction: row; display: flex"> + <div> + <h5>End time:</h5> + </div> + <div style="padding: 0em 0.5em"> + <input class="form-control" id = "endTime" name = "endTime" type = "text" placeholder="13.05"/> + </div> </div> - <h4>End time:</h4> - <div> - <label for="endHour">Hours:</label> - <input id = "endHour" name = "endHour" type = "number" min="0" max="23" value="0"/> - </div> +<!-- <h4>Start time:</h4>--> +<!-- <div>--> +<!-- <label for="startHour">Hours:</label>--> +<!-- <input id = "startHour" name = "startHour" type = "number" min="0" max="23" value="0"/>--> +<!-- </div>--> - <div> - <label for="endMinute">Minute:</label> - <input id = "endMinute" name = "endMinute" type = "number" min="0" max="59" value="0"/> - </div> +<!-- <div>--> +<!-- <label for="startMinute">Minute:</label>--> +<!-- <input id = "startMinute" name = "startMinute" type = "number" min="0" max="59" value="0"/>--> +<!-- </div>--> + +<!-- <h4>End time:</h4>--> +<!-- <div>--> +<!-- <label for="endHour">Hours:</label>--> +<!-- <input id = "endHour" name = "endHour" type = "number" min="0" max="23" value="0"/>--> +<!-- </div>--> + +<!-- <div>--> +<!-- <label for="endMinute">Minute:</label>--> +<!-- <input id = "endMinute" name = "endMinute" type = "number" min="0" max="59" value="0"/>--> +<!-- </div>--> <button type="submit" class="btn btn-success" >Submit</button> </form> diff --git a/src/main/resources/templates/display_schedule.html b/src/main/resources/templates/display_schedule.html index f178a9acb09b6e1ab3228cd735d010ed6a6717e6..257631eeb1b02e9a25f4262f8b8927078473c76a 100644 --- a/src/main/resources/templates/display_schedule.html +++ b/src/main/resources/templates/display_schedule.html @@ -36,7 +36,7 @@ <tbody> <tr th:each="slot: ${slots}"> <td th:text="${slot.day.toString()}"></td> - <td th:text="${(slot.startTime / (1000 * 60 * 60))%24}+'.'+${(slot.startTime / 60000) % 60}+' - '+${(slot.finishTime / (1000 * 60 * 60))%24}+'.'+${(slot.finishTime / 60000) % 60}"></td> + <td th:text="${#dates.format(slot.startTime-25200000, 'HH:mm')}+' - '+${#dates.format(slot.finishTime-25200000,'HH:mm')}"></td> </tr> </tbody> </table>