Skip to content

Commit

Permalink
Merge pull request #225 from SELab-2/feature/224-jaartallen
Browse files Browse the repository at this point in the history
adding course year to backend
  • Loading branch information
Aqua-sc authored Apr 27, 2024
2 parents c1aa11c + c71e4cd commit f17d6e9
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class CourseController {
*/
@GetMapping(ApiRoutes.COURSE_BASE_PATH)
@Roles({UserRole.teacher, UserRole.student})
public ResponseEntity<?> getUserCourses(Auth auth, @RequestParam(value="archived", required = false) Boolean archived) {
public ResponseEntity<?> getUserCourses(Auth auth, @RequestParam(value = "archived", required = false) Boolean archived) {
long userID = auth.getUserEntity().getId();
try {
Logger.getGlobal().info("Archived: " + archived);
Expand Down Expand Up @@ -111,7 +111,7 @@ public ResponseEntity<?> createCourse(@RequestBody CourseJson courseJson, Auth a
}

// Create new course
CourseEntity courseEntity = new CourseEntity(courseJson.getName(), courseJson.getDescription());
CourseEntity courseEntity = new CourseEntity(courseJson.getName(), courseJson.getDescription(), courseJson.getYear());
// Get current time and convert to SQL Timestamp
OffsetDateTime currentTimestamp = OffsetDateTime.now();
courseEntity.setCreatedAt(currentTimestamp);
Expand Down Expand Up @@ -143,6 +143,7 @@ private ResponseEntity<?> doCourseUpdate(CourseEntity courseEntity, CourseJson c
}
courseEntity.setName(courseJson.getName());
courseEntity.setDescription(courseJson.getDescription());
courseEntity.setCourseYear(courseJson.getYear());
if (courseJson.getArchived() != null) {
courseEntity.setArchivedAt(courseJson.getArchived() ? OffsetDateTime.now() : null);
}
Expand Down Expand Up @@ -191,8 +192,8 @@ public ResponseEntity<?> patchCourse(@RequestBody CourseJson courseJson, @PathVa
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage());
}

if (courseJson.getName() == null && courseJson.getDescription() == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Name or description is required");
if (courseJson.getName() == null && courseJson.getDescription() == null && courseJson.getYear() == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Name, description or year is required");
}

CourseEntity courseEntity = checkResult.getData();
Expand All @@ -202,6 +203,9 @@ public ResponseEntity<?> patchCourse(@RequestBody CourseJson courseJson, @PathVa
if (courseJson.getDescription() == null) {
courseJson.setDescription(courseEntity.getDescription());
}
if (courseJson.getYear() == null) {
courseJson.setYear(courseEntity.getCourseYear());
}

return doCourseUpdate(courseEntity, courseJson, user);
} catch (Exception e) {
Expand Down Expand Up @@ -234,7 +238,6 @@ public ResponseEntity<?> getCourseByCourseId(@PathVariable long courseId, Auth a
}



/**
* Function to delete a course by its ID
*
Expand Down Expand Up @@ -319,7 +322,7 @@ public ResponseEntity<?> getProjectsByCourseId(@PathVariable Long courseId, Auth
if (relation.equals(CourseRelation.enrolled)) {
projects = projects.stream().filter(ProjectEntity::isVisible).toList();
}
List<ProjectResponseJson> projectResponseJsons = projects.stream().map(projectEntity ->
List<ProjectResponseJson> projectResponseJsons = projects.stream().map(projectEntity ->
entityToJsonConverter.projectEntityToProjectResponseJson(projectEntity, course, user)
).toList();

Expand All @@ -340,7 +343,7 @@ private ResponseEntity<?> getJoinLinkPostResponseEntity(long courseId, String co
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add user to individual group, contact admin.");
}
courseUserRepository.save(new CourseUserEntity(courseId, user.getId(), CourseRelation.enrolled));
return ResponseEntity.ok(entityToJsonConverter.courseEntityToCourseWithInfo(course, courseUtil.getJoinLink(course.getJoinKey(),"" + course.getId()), false));
return ResponseEntity.ok(entityToJsonConverter.courseEntityToCourseWithInfo(course, courseUtil.getJoinLink(course.getJoinKey(), "" + course.getId()), false));
}

private ResponseEntity<?> getJoinLinkGetResponseEntity(long courseId, String courseKey, UserEntity user) {
Expand All @@ -360,8 +363,8 @@ private ResponseEntity<?> getJoinLinkGetResponseEntity(long courseId, String cou
/**
* Function to join course with key
*
* @param auth authentication object of the requesting user
* @param courseId ID of the course to join
* @param auth authentication object of the requesting user
* @param courseId ID of the course to join
* @param courseKey key of the course to join
* @return ResponseEntity with a statuscode and no body
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6698810">apiDog documentation</a>
Expand All @@ -378,8 +381,8 @@ public ResponseEntity<?> joinCourse(Auth auth, @PathVariable Long courseId, @Pat
/**
* Function to get course information for joining course with key
*
* @param auth authentication object of the requesting user
* @param courseId ID of the course to get the join key from
* @param auth authentication object of the requesting user
* @param courseId ID of the course to get the join key from
* @param courseKey key of the course to get the join key from
* @return ResponseEntity with a statuscode and a JSON object containing the course information
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6698818">apiDog documentation</a>
Expand All @@ -396,7 +399,7 @@ public ResponseEntity<?> getCourseJoinKey(Auth auth, @PathVariable Long courseId
/**
* Function to join course without key
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to join
* @return ResponseEntity with a statuscode and no body
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6698821">apiDog documentation</a>
Expand All @@ -413,7 +416,7 @@ public ResponseEntity<?> joinCourse(Auth auth, @PathVariable Long courseId) {
/**
* Function to get course information for joining course without key
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to get the join key from
* @return ResponseEntity with a statuscode and a JSON object containing the course information
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6698822">apiDog documentation</a>
Expand All @@ -431,7 +434,7 @@ public ResponseEntity<?> getCourseJoinKey(Auth auth, @PathVariable Long courseId
* Function to leave a course
*
* @param courseId ID of the course to leave
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @return ResponseEntity with a statuscode and no body
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6698775">apiDog documentation</a>
* @HttpMethod DELETE
Expand Down Expand Up @@ -466,9 +469,9 @@ public ResponseEntity<?> leaveCourse(@PathVariable long courseId, Auth auth) {
/**
* Function to remove a different user from a course
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to leave
* @param userId JSON object containing the user id
* @param userId JSON object containing the user id
* @return ResponseEntity with a statuscode and no body
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-5883724">apiDog documentation</a>
* @HttpMethod DELETE
Expand Down Expand Up @@ -496,9 +499,9 @@ public ResponseEntity<?> removeCourseMember(Auth auth, @PathVariable Long course
/**
* Function to add a different user to a course
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to add the user to
* @param request JSON object containing the user id and relation
* @param request JSON object containing the user id and relation
* @return ResponseEntity with a statuscode and no body
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-5883723">apiDog documentation</a>
* @HttpMethod POST
Expand Down Expand Up @@ -531,9 +534,9 @@ public ResponseEntity<?> addCourseMember(Auth auth, @PathVariable Long courseId,
/**
* Function to update the relation of a user in a course
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to update the user in
* @param request JSON object containing the user id and relation
* @param request JSON object containing the user id and relation
* @return ResponseEntity with a statuscode and no body
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-5883731">apiDog documentation</a>
* @HttpMethod PATCH
Expand Down Expand Up @@ -577,7 +580,7 @@ public ResponseEntity<?> updateCourseMember(Auth auth, @PathVariable Long course
/**
* Function to get all members of a course
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to get the members from
* @return ResponseEntity with a JSON object containing the members of the course
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-5724006">apiDog documentation</a>
Expand Down Expand Up @@ -610,7 +613,7 @@ public ResponseEntity<?> getCourseMembers(Auth auth, @PathVariable Long courseId
/**
* Function to get the join link of a course
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to get the join link from
* @return ResponseEntity with the join link of the course
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6698763">apiDog documentation</a>
Expand All @@ -629,10 +632,11 @@ public ResponseEntity<String> getCourseKey(Auth auth, @PathVariable Long courseI
}

// Function for invalidating the previous key and generating a new one, can be useful when starting a new year.

/**
* Function to generate a new join link for a course
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to generate the join link for
* @return ResponseEntity with the new join link of the course
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6691656">apiDog documentation</a>
Expand All @@ -658,7 +662,7 @@ public ResponseEntity<?> getAndGenerateCourseKey(Auth auth, @PathVariable Long c
/**
* Function to remove the joinKey from the joinLink of a course
*
* @param auth authentication object of the requesting user
* @param auth authentication object of the requesting user
* @param courseId ID of the course to remove the join link from
* @return ResponseEntity with the new join link of the course (without the key)
* @ApiDog <a href="https://apidog.com/apidoc/project-467959/api-6698823">apiDog documentation</a>
Expand All @@ -679,4 +683,5 @@ public ResponseEntity<String> deleteCourseKey(Auth auth, @PathVariable Long cour
return ResponseEntity.ok("");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ public class CourseJson{

private Boolean isArchived;

public CourseJson(String name, String description, Boolean isArchived) {
private Integer year;

public CourseJson(String name, String description, Boolean isArchived, Integer courseYear) {
this.name = name;
this.description = description;
this.isArchived = isArchived;
this.year = courseYear;
}

public String getName() {
Expand All @@ -37,5 +40,13 @@ public Boolean getArchived() {
public void setArchived(Boolean isArchived) {
this.isArchived = isArchived;
}

public Integer getYear() {
return year;
}

public void setYear(Integer courseYear) {
this.year = courseYear;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public record CourseWithInfoJson (
String joinUrl,
String joinKey,
OffsetDateTime archivedAt,
OffsetDateTime createdAt
OffsetDateTime createdAt,
Integer year
) {}

Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import java.time.OffsetDateTime;

public record CourseWithRelationJson (String url, CourseRelation relation, String name, Long courseId,
OffsetDateTime archivedAt, Integer memberCount, OffsetDateTime createdAt) { }
OffsetDateTime archivedAt, Integer memberCount, OffsetDateTime createdAt, Integer year) { }

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class CourseEntity {
@Column(name = "description", nullable=false)
private String description;

@Column(name = "course_year", nullable = true)
private Integer courseYear;


@Column(name = "created_at")
private OffsetDateTime createdAt;

Expand All @@ -35,9 +39,10 @@ public void setJoinKey(String joinKey) {
@Column(name = "join_key", nullable=true)
private String joinKey;

public CourseEntity(String name, String description) {
public CourseEntity(String name, String description,Integer courseYear) {
this.name = name;
this.description = description;
this.courseYear = courseYear;
}

public CourseEntity() {
Expand Down Expand Up @@ -71,6 +76,7 @@ public void setDescription(String description) {
}



public OffsetDateTime getCreatedAt() {
return createdAt;
}
Expand All @@ -86,4 +92,14 @@ public OffsetDateTime getArchivedAt() {
public void setArchivedAt(OffsetDateTime archivedAt) {
this.archivedAt = archivedAt;
}

public int getCourseYear() {
return courseYear;
}
public void setCourseYear(int courseYear){
this.courseYear = courseYear;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ugent.pidgeon.postgre.models.types.CourseRelation;
import com.ugent.pidgeon.postgre.models.types.UserRole;
import com.ugent.pidgeon.postgre.repository.*;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -260,8 +261,9 @@ public CheckResult<Void> checkCourseJson(CourseJson courseJson, UserEntity user,
}


if (courseJson.getName() == null || courseJson.getDescription() == null) {
return new CheckResult<>(HttpStatus.BAD_REQUEST, "name and description are required", null);
if (courseJson.getName() == null || courseJson.getDescription() == null || courseJson.getYear() == null) {
Logger.getGlobal().info(""+ courseJson.getYear());
return new CheckResult<>(HttpStatus.BAD_REQUEST, "name, description and year are required", null);
}

if (courseJson.getName().isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public CourseWithInfoJson courseEntityToCourseWithInfo(CourseEntity course, Stri
hideKey ? null : joinLink,
hideKey ? null : course.getJoinKey(),
course.getArchivedAt(),
course.getCreatedAt()
course.getCreatedAt(),
course.getCourseYear()
);
}

Expand All @@ -109,7 +110,8 @@ public CourseWithRelationJson courseEntityToCourseWithRelation(CourseEntity cour
course.getId(),
course.getArchivedAt(),
memberCount,
course.getCreatedAt()
course.getCreatedAt(),
course.getCourseYear()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void setup() {
}))
.build();

courseEntity = new CourseEntity("name", "description");
courseEntity = new CourseEntity("name", "description",2024);
groupClusterEntity = new GroupClusterEntity(1L, 20, "clustername", 5);
groupEntity = new GroupEntity("groupName", 1L);
}
Expand Down
Loading

0 comments on commit f17d6e9

Please sign in to comment.