Skip to content

Commit

Permalink
Add year to return body,change json parameter name
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqua-sc committed Apr 26, 2024
1 parent 469e454 commit 738303e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
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(), courseJson.getCourseYear());
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,7 +143,7 @@ private ResponseEntity<?> doCourseUpdate(CourseEntity courseEntity, CourseJson c
}
courseEntity.setName(courseJson.getName());
courseEntity.setDescription(courseJson.getDescription());
courseEntity.setCourseYear(courseJson.getCourseYear());
courseEntity.setCourseYear(courseJson.getYear());
if (courseJson.getArchived() != null) {
courseEntity.setArchivedAt(courseJson.getArchived() ? OffsetDateTime.now() : null);
}
Expand Down Expand Up @@ -192,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 && courseJson.getCourseYear() == 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 @@ -203,8 +203,8 @@ public ResponseEntity<?> patchCourse(@RequestBody CourseJson courseJson, @PathVa
if (courseJson.getDescription() == null) {
courseJson.setDescription(courseEntity.getDescription());
}
if (courseJson.getCourseYear() == null) {
courseJson.setCourseYear(courseEntity.getCourseYear());
if (courseJson.getYear() == null) {
courseJson.setYear(courseEntity.getCourseYear());
}

return doCourseUpdate(courseEntity, courseJson, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public class CourseJson{

private Boolean isArchived;

private Integer courseYear;
private Integer year;

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

public String getName() {
Expand All @@ -41,12 +41,12 @@ public void setArchived(Boolean isArchived) {
this.isArchived = isArchived;
}

public Integer getCourseYear() {
return courseYear;
public Integer getYear() {
return year;
}

public void setCourseYear(Integer courseYear) {
this.courseYear = courseYear;
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 @@ -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,7 +261,8 @@ public CheckResult<Void> checkCourseJson(CourseJson courseJson, UserEntity user,
}


if (courseJson.getName() == null || courseJson.getDescription() == null || courseJson.getCourseYear() == 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);
}

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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void testCreateCourse() throws Exception {
when(courseUtil.getJoinLink(any(), any())).thenReturn("");
when(entityToJsonConverter.courseEntityToCourseWithInfo(any(), any(), anyBoolean())).
thenReturn(new CourseWithInfoJson(0L, "", "", new UserReferenceJson("", "", 0L),
new ArrayList<>(), "", "", "", OffsetDateTime.now(), OffsetDateTime.now()));
new ArrayList<>(), "", "", "", OffsetDateTime.now(), OffsetDateTime.now(), 2013));

mockMvc.perform(MockMvcRequestBuilders.post(ApiRoutes.COURSE_BASE_PATH)
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -217,7 +217,7 @@ public void testGetCourseByCourseId() throws Exception {
when(courseUtil.getJoinLink(any(), any())).thenReturn("");
when(entityToJsonConverter.courseEntityToCourseWithInfo(any(), any(), anyBoolean())).
thenReturn(new CourseWithInfoJson(0L, "", "", new UserReferenceJson("", "", 0L),
new ArrayList<>(), "", "", "", OffsetDateTime.now(), OffsetDateTime.now()));
new ArrayList<>(), "", "", "", OffsetDateTime.now(), OffsetDateTime.now(), 2023));
when(courseUtil.getCourseIfUserInCourse(anyLong(), any(UserEntity.class))).
thenReturn(new CheckResult<>(HttpStatus.OK, "", new Pair<>(new CourseEntity(), CourseRelation.course_admin)));
mockMvc.perform(MockMvcRequestBuilders.get(ApiRoutes.COURSE_BASE_PATH + "/1"))
Expand Down

0 comments on commit 738303e

Please sign in to comment.