Skip to content

Commit

Permalink
Merge pull request #38 from hotungkhanh/reformat-code
Browse files Browse the repository at this point in the history
refactor: ♻️ refactor files, bring to "rest" package, reformat code
  • Loading branch information
hotungkhanh authored Oct 7, 2024
2 parents e7cc231 + f8e7d4e commit 3507b4f
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 110 deletions.
27 changes: 11 additions & 16 deletions backend/src/main/java/org/acme/domain/Room.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package org.acme.domain;

import java.util.ArrayList;
import java.util.List;

import ai.timefold.solver.core.api.domain.lookup.PlanningId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import ai.timefold.solver.core.api.domain.lookup.PlanningId;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.OneToMany;
import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;

/**
* Represents a room.
Expand All @@ -21,7 +16,7 @@
*/
@Entity
public class Room extends PanacheEntity {

@PlanningId
public String roomCode;

Expand Down Expand Up @@ -55,11 +50,11 @@ public Room() {
/**
* Creates a room with its ID and capacity.
*
* @param id The room’s id.
* @param buildingId The building that the room belongs to.
* @param campus The campus that the room belongs to.
* @param capacity The room's capacity.
* @param isLab Whether the room is a laboratory.
* @param id The room’s id.
* @param buildingId The building that the room belongs to.
* @param campus The campus that the room belongs to.
* @param capacity The room's capacity.
* @param isLab Whether the room is a laboratory.
*/
public Room(String id, String buildingId, String campus, int capacity, boolean isLab) {
this.roomCode = id;
Expand Down
24 changes: 9 additions & 15 deletions backend/src/main/java/org/acme/domain/Student.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
package org.acme.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Represents a student.
*
* @author Jet Edge
*/
@Entity
public class Student extends PanacheEntity{
public class Student extends PanacheEntity {

public String name;

@JsonIgnoreProperties("students")
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "student_unit",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
name = "student_unit",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
)
@JsonIgnore
public List<Unit> units = new ArrayList<Unit>();
Expand Down
27 changes: 10 additions & 17 deletions backend/src/main/java/org/acme/domain/Timetable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@
import ai.timefold.solver.core.api.domain.solution.ProblemFactCollectionProperty;
import ai.timefold.solver.core.api.domain.valuerange.ValueRangeProvider;
import ai.timefold.solver.core.api.score.buildin.hardsoft.HardSoftScore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.*;

import java.time.DayOfWeek;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Represents a timetable, the solution from the program.
*
Expand All @@ -44,16 +37,16 @@ public class Timetable extends PanacheEntity {

/*
* Rooms can belong to multiple timetables because timetables are generated
* on a per-campus basis, and although each room can only belong to one
* on a per-campus basis, and although each room can only belong to one
* campus, the user may choose to generate multiple timetables for each
* campus, hence the many-to-many relationship
*/
@JsonIgnoreProperties("timetables")
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "room_timetable",
joinColumns = @JoinColumn(name = "timetable_id"),
inverseJoinColumns = @JoinColumn(name = "room_id")
name = "room_timetable",
joinColumns = @JoinColumn(name = "timetable_id"),
inverseJoinColumns = @JoinColumn(name = "room_id")
)
@ProblemFactCollectionProperty
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
Expand All @@ -68,9 +61,9 @@ public class Timetable extends PanacheEntity {
@JsonIgnoreProperties("timetables")
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "unit_timetable",
joinColumns = @JoinColumn(name = "timetable_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
name = "unit_timetable",
joinColumns = @JoinColumn(name = "timetable_id"),
inverseJoinColumns = @JoinColumn(name = "unit_id")
)
@PlanningEntityCollectionProperty
public List<Unit> units;
Expand Down
21 changes: 7 additions & 14 deletions backend/src/main/java/org/acme/domain/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@
import ai.timefold.solver.core.api.domain.entity.PlanningEntity;
import ai.timefold.solver.core.api.domain.lookup.PlanningId;
import ai.timefold.solver.core.api.domain.variable.PlanningVariable;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Transient;
import jakarta.persistence.*;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* Represents a unit.
*
Expand Down Expand Up @@ -55,8 +48,8 @@ public class Unit extends PanacheEntity {

/*
* currently each unit only has 1 'slot' on the timetable, so it can only
* be associated with one room, but in the final product, we would most
* likely have to change this to a many-to-many relationship
* be associated with one room, but in the final product, we would most
* likely have to change this to a many-to-many relationship
* i.e. list of Rooms, because we might want to separate lecture/tutorial
* etc.
*/
Expand Down Expand Up @@ -128,7 +121,7 @@ public Unit(int unitID, String name, String course, Duration duration, List<Stud
* @param duration The unit’s duration.
* @param students The list of students enrolled in the unit.
* @param wantsLab Whether the unit wants a laboratory room.
* @param room The unit's room.
* @param room The unit's room.
*/
public Unit(int unitID, String name, String course, DayOfWeek dayOfWeek, LocalTime startTime, Duration duration, List<Student> students, boolean wantsLab, Room room) {
this.unitId = unitID;
Expand Down Expand Up @@ -194,7 +187,7 @@ public List<Student> getStudents() {

public void setStudents(List<Student> students) {
this.students = students;
this.setStudentsUnits();;
this.setStudentsUnits();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.acme.domain;

import java.util.List;
package org.acme.rest;

import jakarta.annotation.security.RolesAllowed;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.acme.domain.Room;

import java.util.List;

@Path("/rooms")
@RolesAllowed({"user"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.acme.domain;

import java.util.List;
package org.acme.rest;

import jakarta.transaction.Transactional;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.acme.domain.Student;

import java.util.List;

@Path("/students")
public class StudentResource {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.acme;
package org.acme.rest;

import ai.timefold.solver.core.api.solver.SolverManager;
import jakarta.annotation.security.RolesAllowed;
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.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.acme.domain.Room;
import org.acme.domain.Student;
Expand All @@ -19,9 +15,8 @@
import java.time.Duration;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.ExecutionException;

import java.util.UUID;
import java.util.concurrent.ExecutionException;

/**
* Entry to the timetabling program.
Expand Down Expand Up @@ -135,23 +130,23 @@ public Timetable solveExample() throws ExecutionException, InterruptedException
/*
* During this solving phase, new Unit objects will be created with the
* allotted date and Room assignment.
*
*
* Currently, the 'old' Unit objects in the 'problem' variable and the
* 'new' Unit objects in the 'solution' variable are stored as different
* Units in the database due to our inability to control the behaviour
* of solverManager.solve
*
*
* i.e. after solving, there will be 2 copies of each Unit in the
* database, where the 'old' Unit has the list of students but no
* timetable assignment, while the 'new' Unit does not have the list
* database, where the 'old' Unit has the list of students but no
* timetable assignment, while the 'new' Unit does not have the list
* of students enrolled, but does have the assigned date and room
*/

findByCampusAndDelete(problem.campusName);

Timetable solution = solverManager.solve("job 1", problem).getFinalBestSolution();

solution.persist();
solution.persist();
// saves the solution timetable and all related entities to database

return solution;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.acme.domain;

import java.util.List;
package org.acme.rest;

import jakarta.annotation.security.RolesAllowed;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.acme.domain.Unit;

import java.util.List;

@Path("/units")
@RolesAllowed({"user"})
Expand Down
1 change: 0 additions & 1 deletion backend/src/main/java/org/acme/security/jpa/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.transaction.Transactional;

import org.eclipse.microprofile.config.Config;

@Singleton
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/org/acme/security/jpa/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public class User extends PanacheEntity {

/**
* Adds a new user to the database
*
* @param username the username
* @param password the unencrypted password (it is encrypted with bcrypt)
* @param role the comma-separated roles
* @param role the comma-separated roles
*/
public static void add(String username, String password, String role) {
User user = new User();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package org.acme.solver;

import ai.timefold.solver.test.api.score.stream.ConstraintVerifier;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.acme.domain.*;
import org.junit.jupiter.api.Test;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalTime;
import java.util.List;

import org.acme.domain.*;

import jakarta.inject.Inject;
import ai.timefold.solver.test.api.score.stream.ConstraintVerifier;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class TimetableConstraintProviderTest {
private static final Room ROOM1 = new Room("1", "building A", "campus A", 2, true);
Expand All @@ -37,7 +35,7 @@ public class TimetableConstraintProviderTest {
void studentConflict() {
Unit firstUnit = new Unit(1, "unit1", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), true, ROOM1);
Unit conflictingUnit = new Unit(2, "unit2", "Course A", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT1), false, ROOM2);
Unit nonConflictingUnit = new Unit(3, "unit3","Course B", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM3);
Unit nonConflictingUnit = new Unit(3, "unit3", "Course B", DAY_OF_WEEK, START_TIME, DURATION, List.of(STUDENT2), true, ROOM3);
ConflictingUnit conflictingUnitPair = new ConflictingUnit(firstUnit, conflictingUnit, 1);

constraintVerifier.verifyThat(TimetableConstraintProvider::studentConflict)
Expand Down

0 comments on commit 3507b4f

Please sign in to comment.