From 8d1a7c4cecfb2a681f2c3fc2d85445599c1b715d Mon Sep 17 00:00:00 2001 From: jay pratap singh <jaypratap.singh@tarento.com> Date: Thu, 17 Aug 2023 15:36:10 +0530 Subject: [PATCH] changes for exam cycle --- pom.xml | 36 ++++++---- .../controller/ExamCycleController.java | 65 ++++++++++++++++--- .../exception/ServiceException.java | 16 +++++ .../examsAndAdmissions/model/ExamCycle.java | 20 +++++- .../model/dto/ExamCycleDTO.java | 14 +++- .../service/ExamCycleService.java | 55 +++++++++++++++- 6 files changed, 182 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/tarento/upsmf/examsAndAdmissions/exception/ServiceException.java diff --git a/pom.xml b/pom.xml index 6a80476..2e5ef99 100644 --- a/pom.xml +++ b/pom.xml @@ -21,11 +21,32 @@ <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> + <!-- Bean Validation API --> <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-webflux</artifactId> + <groupId>javax.validation</groupId> + <artifactId>validation-api</artifactId> </dependency> + <!-- Hibernate Validator (implementation of the Bean Validation API) --> + <dependency> + <groupId>org.hibernate.validator</groupId> + <artifactId>hibernate-validator</artifactId> + </dependency> + + <dependency> + <groupId>org.modelmapper</groupId> + <artifactId>modelmapper</artifactId> + <version>3.0.0</version> + </dependency> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> @@ -48,15 +69,8 @@ <optional>true</optional> </dependency> <dependency> - <groupId>io.r2dbc</groupId> - <artifactId>r2dbc-postgresql</artifactId> - <version>0.8.11.RELEASE</version> - <scope>runtime</scope> - </dependency> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - <scope>runtime</scope> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> </dependency> </dependencies> diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamCycleController.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamCycleController.java index 9c188db..57c7ed9 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamCycleController.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamCycleController.java @@ -1,33 +1,82 @@ package com.tarento.upsmf.examsAndAdmissions.controller; +import com.tarento.upsmf.examsAndAdmissions.exception.ServiceException; import com.tarento.upsmf.examsAndAdmissions.model.ExamCycle; import com.tarento.upsmf.examsAndAdmissions.model.dto.ExamCycleDTO; import com.tarento.upsmf.examsAndAdmissions.service.ExamCycleService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/exam-cycles") +@RequestMapping("/api/v1/admin/examCycle") public class ExamCycleController { @Autowired private ExamCycleService examCycleService; - @PostMapping("admin/enrollment/exam-cycles/institute") + @PostMapping("/create") public ResponseEntity<ExamCycle> createExamCycle(@RequestBody ExamCycleDTO examCycleDTO) { - return ResponseEntity.ok(examCycleService.createExamCycle(examCycleDTO)); + try { + ExamCycle createdExamCycle = examCycleService.createExamCycle(examCycleDTO); + return new ResponseEntity<>(createdExamCycle, HttpStatus.CREATED); + } catch (Exception e) { + throw new ServiceException("Failed to create ExamCycle.", HttpStatus.INTERNAL_SERVER_ERROR); + } } - - @GetMapping + @GetMapping("/list") public ResponseEntity<List<ExamCycle>> getAllExamCycles() { - return ResponseEntity.ok(examCycleService.getAllExamCycles()); + try { + List<ExamCycle> examCycles = examCycleService.getAllExamCycles(); + if (examCycles.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + return new ResponseEntity<>(examCycles, HttpStatus.OK); + } catch (Exception e) { + throw new ServiceException("Failed to fetch all ExamCycles.", HttpStatus.INTERNAL_SERVER_ERROR); + } } @GetMapping("/{id}") public ResponseEntity<ExamCycle> getExamCycleById(@PathVariable Long id) { - return ResponseEntity.ok(examCycleService.getExamCycleById(id)); + try { + ExamCycle examCycle = examCycleService.getExamCycleById(id); + if (examCycle == null) { + throw new ServiceException("ExamCycle not found with ID: " + id, HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(examCycle, HttpStatus.OK); + } catch (ServiceException se) { + throw se; + } catch (Exception e) { + throw new ServiceException("Failed to fetch ExamCycle with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + @DeleteMapping("/delete/{id}") + public ResponseEntity<Void> deleteExamCycle(@PathVariable Long id) { + try { + examCycleService.deleteExamCycleById(id); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } catch (Exception e) { + throw new ServiceException("Failed to delete ExamCycle with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } } -} + + @PutMapping("/update/{id}") + public ResponseEntity<ExamCycle> updateExamCycle(@PathVariable Long id, @RequestBody ExamCycleDTO examCycleDTO) { + try { + ExamCycle updatedExamCycle = examCycleService.updateExamCycle(id, examCycleDTO); + if (updatedExamCycle == null) { + throw new ServiceException("ExamCycle not found with ID: " + id, HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(updatedExamCycle, HttpStatus.OK); + } catch (ServiceException se) { + throw se; + } catch (Exception e) { + throw new ServiceException("Failed to update ExamCycle with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/exception/ServiceException.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/exception/ServiceException.java new file mode 100644 index 0000000..b6d2808 --- /dev/null +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/exception/ServiceException.java @@ -0,0 +1,16 @@ +package com.tarento.upsmf.examsAndAdmissions.exception; + +import org.springframework.http.HttpStatus; + +public class ServiceException extends RuntimeException { + private HttpStatus status; + + public ServiceException(String message, HttpStatus status) { + super(message); + this.status = status; + } + + public HttpStatus getStatus() { + return status; + } +} diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/ExamCycle.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/ExamCycle.java index 5b437d5..5f4b12b 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/ExamCycle.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/ExamCycle.java @@ -1,10 +1,12 @@ package com.tarento.upsmf.examsAndAdmissions.model; +import com.tarento.upsmf.examsAndAdmissions.model.Course; import com.tarento.upsmf.examsAndAdmissions.model.CourseDetails; import lombok.*; import javax.persistence.*; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; @Entity @@ -21,9 +23,23 @@ public class ExamCycle { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - private LocalDate startDate; - private LocalDate endDate; + private String examCycleName; // New field + + @ManyToOne + @JoinColumn(name = "course_id") + private Course course; // Represents the Course-ID + + private LocalDate startDate; // Represents start_date + private LocalDate endDate; // Represents end_date + private String createdBy; + private LocalDateTime createdOn; + private String modifiedBy; + private LocalDateTime modifiedOn; + private String status; // Can be "Publish" or "Draft" + private Boolean isObsolete; // Represents Obselete [0/1] @OneToMany(mappedBy = "examCycle", cascade = CascadeType.ALL) private List<CourseDetails> courseDetails; + + // ... rest of your code ... } diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/dto/ExamCycleDTO.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/dto/ExamCycleDTO.java index 263a0f3..c1e2f73 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/dto/ExamCycleDTO.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/dto/ExamCycleDTO.java @@ -1,5 +1,6 @@ package com.tarento.upsmf.examsAndAdmissions.model.dto; +import com.tarento.upsmf.examsAndAdmissions.model.dto.CourseDetailDTO; import lombok.*; import java.util.List; @@ -10,8 +11,19 @@ import java.util.List; @NoArgsConstructor @Builder public class ExamCycleDTO { + + private Long id; + private String examCycleName; private String startDate; private String endDate; + private String createdBy; + private String createdOn; + private String modifiedBy; + private String modifiedOn; + private String status; + private Boolean isObsolete; + private List<CourseDetailDTO> courseDetails; -} \ No newline at end of file + // getters and setters +} diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamCycleService.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamCycleService.java index 21400ed..35b19f3 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamCycleService.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamCycleService.java @@ -33,16 +33,66 @@ public class ExamCycleService { ExamCycle examCycle = convertToEntity(examCycleDTO); return examCycleRepository.save(examCycle); } + public ExamCycle updateExamCycle(Long id, ExamCycleDTO dto) { + ExamCycle existingExamCycle = examCycleRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException("ExamCycle not found with ID: " + id)); + + // Set updated fields from the DTO to the existingExamCycle + existingExamCycle.setStartDate(LocalDate.parse(dto.getStartDate())); + existingExamCycle.setEndDate(LocalDate.parse(dto.getEndDate())); + existingExamCycle.setExamCycleName(dto.getExamCycleName()); + existingExamCycle.setCreatedBy(dto.getCreatedBy()); + existingExamCycle.setCreatedOn(LocalDateTime.parse(dto.getCreatedOn())); + existingExamCycle.setModifiedBy(dto.getModifiedBy()); + existingExamCycle.setModifiedOn(LocalDateTime.parse(dto.getModifiedOn())); + existingExamCycle.setStatus(dto.getStatus()); + existingExamCycle.setIsObsolete(dto.getIsObsolete()); + + List<CourseDetails> courseDetailsList = new ArrayList<>(); + for (CourseDetailDTO cdDto : dto.getCourseDetails()) { + CourseDetails cd = new CourseDetails(); + Optional<Course> course = courseRepository.findById(cdDto.getCourseId()); + if (!course.isPresent()) { + throw new EntityNotFoundException("Course not found with ID: " + cdDto.getCourseId()); + } + cd.setCourse(course.get()); + + List<Exam> exams = new ArrayList<>(); + for (ExamDTO eDto : cdDto.getExams()) { + Exam exam = new Exam(); + exam.setExamName(eDto.getExamName()); + exam.setExamDate(LocalDateTime.parse(eDto.getExamDate())); + exam.setExamDuration(Duration.parse(eDto.getExamDuration())); + exams.add(exam); + } + cd.setExams(exams); + courseDetailsList.add(cd); + } + existingExamCycle.setCourseDetails(courseDetailsList); + + return examCycleRepository.save(existingExamCycle); + } + public void deleteExamCycleById(Long id) { + examCycleRepository.deleteById(id); + } + private ExamCycle convertToEntity(ExamCycleDTO dto) { ExamCycle examCycle = new ExamCycle(); examCycle.setStartDate(LocalDate.parse(dto.getStartDate())); examCycle.setEndDate(LocalDate.parse(dto.getEndDate())); + examCycle.setExamCycleName(dto.getExamCycleName()); + examCycle.setCreatedBy(dto.getCreatedBy()); + examCycle.setCreatedOn(LocalDateTime.parse(dto.getCreatedOn())); + examCycle.setModifiedBy(dto.getModifiedBy()); + examCycle.setModifiedOn(LocalDateTime.parse(dto.getModifiedOn())); + examCycle.setStatus(dto.getStatus()); + examCycle.setIsObsolete(dto.getIsObsolete()); List<CourseDetails> courseDetailsList = new ArrayList<>(); for (CourseDetailDTO cdDto : dto.getCourseDetails()) { CourseDetails cd = new CourseDetails(); - Optional<Course> course = courseRepository.findById( cdDto.getCourseId()); - if (course == null) { + Optional<Course> course = courseRepository.findById(cdDto.getCourseId()); + if (!course.isPresent()) { throw new EntityNotFoundException("Course not found with ID: " + cdDto.getCourseId()); } cd.setCourse(course.get()); @@ -64,6 +114,7 @@ public class ExamCycleService { } + public List<ExamCycle> getAllExamCycles() { return examCycleRepository.findAll(); } -- GitLab