From 9a66ddada369d8c272279f430343629a76dc59f3 Mon Sep 17 00:00:00 2001 From: Eden Xu <130119691+FlyingPufferFish@users.noreply.github.com> Date: Mon, 7 Oct 2024 12:15:01 +1100 Subject: [PATCH] feat(timetables): modify update endpoint to receive list of unit updates --- .vscode/settings.json | 6 +- .../main/java/org/acme/TimetableResource.java | 84 ++++++++++++++++++- 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8a4aef0..5a04e02 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,7 @@ { - "conventionalCommits.scopes": ["database", "display ganttchart"] + "conventionalCommits.scopes": [ + "database", + "display ganttchart", + "timetables" + ] } diff --git a/backend/src/main/java/org/acme/TimetableResource.java b/backend/src/main/java/org/acme/TimetableResource.java index 1256789..2d73625 100644 --- a/backend/src/main/java/org/acme/TimetableResource.java +++ b/backend/src/main/java/org/acme/TimetableResource.java @@ -1,14 +1,19 @@ package org.acme; import ai.timefold.solver.core.api.solver.SolverManager; +import io.smallrye.mutiny.Uni; import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.POST; +import jakarta.ws.rs.PUT; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; +import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + import org.acme.domain.Room; import org.acme.domain.Student; import org.acme.domain.Timetable; @@ -62,11 +67,82 @@ public List view() { return Timetable.listAll(); } - @Path("/unit") - @POST + + /** + * Takes a list of Units as input to updates exisiting timetable once users + * make drag-and-drop modifications in frontend + * + * @param updatedUnits List of all Unit updates + * @return A Response object indicating status + */ + @Path("/update") + @PUT + @Transactional @Consumes(MediaType.APPLICATION_JSON) - public Unit handleUnit(Unit unit) { - return unit; + public Response timetableUpdate(List updatedUnits) { + List dbUnits = Unit.listAll(); + for (Unit updatedUnit : updatedUnits) { + if (unitUpdate(updatedUnit, dbUnits) == null) { + return Response.serverError().build(); + } + } + return Response.ok().build(); + } + + + /** + * Update the time/location of a unit + * + * @param updatedUnit Unit object with updated time/location + * @param dbUnits List of all Unit objects in the database + * @return The updated Unit, null otherwise + */ + @PUT + @Transactional + @Consumes(MediaType.APPLICATION_JSON) + public Unit unitUpdate(Unit updatedUnit, List dbUnits) { + + // find existing Unit obj in db by unitId + for (Unit unit : dbUnits) { + if (updatedUnit.unitId == unit.unitId) { + assert(unit.isPersistent()); + // update day of week + unit.dayOfWeek = updatedUnit.dayOfWeek; + // update startTime + unit.startTime = updatedUnit.startTime; + // if room is different + if (unit.room.roomCode != updatedUnit.room.roomCode + || !unit.room.buildingId.equals(updatedUnit.room.buildingId)) { + // update room + Room newRoom; + if ((newRoom = findRoom(updatedUnit.room)) == null) { + throw new WebApplicationException("Room with building ID " + updatedUnit.room.buildingId + ", and room code " + updatedUnit.room.roomCode + " not found", 404); + } + unit.room = newRoom; + } + return unit; + } + } + return null; + } + + /** + * Find existing Room object inside the database with + * desired buildingId and roomCode + * + * @param inputRoom Room object with buildingId and roomCode of desired Room + * @return Room object meeting the input criteria, null otherwise + */ + public Room findRoom(Room inputRoom) { + List rooms = Room.listAll(); + for (Room room : rooms) { + if (room.roomCode.equals(inputRoom.roomCode) + && room.buildingId.equals(inputRoom.buildingId)) { + assert(room.isPersistent()); + return room; + } + } + return null; } public void findByCampusAndDelete(String campusName) {