From 738303e7883ba7369293d331df48223cd609d87d Mon Sep 17 00:00:00 2001 From: Aqua-sc <108478185+Aqua-sc@users.noreply.github.com> Date: Fri, 26 Apr 2024 16:29:16 +0200 Subject: [PATCH] Add year to return body,change json parameter name --- .../ugent/pidgeon/controllers/CourseController.java | 12 ++++++------ .../com/ugent/pidgeon/model/json/CourseJson.java | 12 ++++++------ .../ugent/pidgeon/model/json/CourseWithInfoJson.java | 3 ++- .../main/java/com/ugent/pidgeon/util/CourseUtil.java | 4 +++- .../ugent/pidgeon/util/EntityToJsonConverter.java | 3 ++- .../pidgeon/controllers/CourseControllerTest.java | 4 ++-- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/backend/app/src/main/java/com/ugent/pidgeon/controllers/CourseController.java b/backend/app/src/main/java/com/ugent/pidgeon/controllers/CourseController.java index 63382a60..15218a66 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/controllers/CourseController.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/controllers/CourseController.java @@ -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); @@ -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); } @@ -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(); @@ -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); diff --git a/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseJson.java b/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseJson.java index 61b8806e..ad7674f8 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseJson.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseJson.java @@ -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() { @@ -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; } } diff --git a/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseWithInfoJson.java b/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseWithInfoJson.java index 0ba3cef9..d7464a18 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseWithInfoJson.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/model/json/CourseWithInfoJson.java @@ -13,6 +13,7 @@ public record CourseWithInfoJson ( String joinUrl, String joinKey, OffsetDateTime archivedAt, - OffsetDateTime createdAt + OffsetDateTime createdAt, + Integer year ) {} diff --git a/backend/app/src/main/java/com/ugent/pidgeon/util/CourseUtil.java b/backend/app/src/main/java/com/ugent/pidgeon/util/CourseUtil.java index bda7a51d..c73c16a1 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/util/CourseUtil.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/util/CourseUtil.java @@ -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; @@ -260,7 +261,8 @@ public CheckResult 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); } diff --git a/backend/app/src/main/java/com/ugent/pidgeon/util/EntityToJsonConverter.java b/backend/app/src/main/java/com/ugent/pidgeon/util/EntityToJsonConverter.java index 604a3f7d..8111abd2 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/util/EntityToJsonConverter.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/util/EntityToJsonConverter.java @@ -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() ); } diff --git a/backend/app/src/test/java/com/ugent/pidgeon/controllers/CourseControllerTest.java b/backend/app/src/test/java/com/ugent/pidgeon/controllers/CourseControllerTest.java index 639dcf87..5433924f 100644 --- a/backend/app/src/test/java/com/ugent/pidgeon/controllers/CourseControllerTest.java +++ b/backend/app/src/test/java/com/ugent/pidgeon/controllers/CourseControllerTest.java @@ -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) @@ -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"))