diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamController.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamController.java new file mode 100644 index 0000000000000000000000000000000000000000..75e667a6ad3c1e47b3b885fee45a4dce6fdaaebc --- /dev/null +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamController.java @@ -0,0 +1,93 @@ +package com.tarento.upsmf.examsAndAdmissions.controller; + +import com.tarento.upsmf.examsAndAdmissions.exception.ServiceException; +import com.tarento.upsmf.examsAndAdmissions.model.Exam; +import com.tarento.upsmf.examsAndAdmissions.service.ExamService; +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("/api/v1/admin/exam") +public class ExamController { + + @Autowired + private ExamService examService; + + @PostMapping("/create") + public ResponseEntity<Exam> createExam(@RequestBody Exam exam) { + try { + Exam createdExam = examService.createExam(exam); + return new ResponseEntity<>(createdExam, HttpStatus.CREATED); + } catch (Exception e) { + throw new ServiceException("Failed to create Exam.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @GetMapping("/list") + public ResponseEntity<List<Exam>> getAllExams() { + try { + List<Exam> exams = examService.getAllExams(); + if (exams.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + return new ResponseEntity<>(exams, HttpStatus.OK); + } catch (Exception e) { + throw new ServiceException("Failed to fetch all Exams.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @GetMapping("/{id}") + public ResponseEntity<Exam> getExamById(@PathVariable Long id) { + try { + Exam exam = examService.getExamById(id); + if (exam == null) { + throw new ServiceException("Exam not found with ID: " + id, HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(exam, HttpStatus.OK); + } catch (ServiceException se) { + throw se; + } catch (Exception e) { + throw new ServiceException("Failed to fetch Exam with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @DeleteMapping("/delete/{id}") + public ResponseEntity<Void> deleteExam(@PathVariable Long id) { + try { + examService.deleteExam(id); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } catch (Exception e) { + throw new ServiceException("Failed to delete Exam with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + @PutMapping("/update/{id}") + public ResponseEntity<Exam> updateExam(@PathVariable Long id, @RequestBody Exam exam) { + try { + Exam updatedExam = examService.updateExam(id, exam); + if (updatedExam == null) { + throw new ServiceException("Exam not found with ID: " + id, HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>(updatedExam, HttpStatus.OK); + } catch (ServiceException se) { + throw se; + } catch (Exception e) { + throw new ServiceException("Failed to update Exam with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + // Restore a soft-deleted exam + @PutMapping("/{id}/restore") + public ResponseEntity<String> restoreExam(@PathVariable Long id) { + try { + examService.restoreExam(id); + return ResponseEntity.ok().build(); + } catch (Exception e) { + throw new ServiceException("Failed to restore Exam with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} 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 57c7ed9ea01196caf30078f036056c17db8e9a9f..72468a30592f4c06792b019df1242230fe8c8243 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamCycleController.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/ExamCycleController.java @@ -2,7 +2,6 @@ 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; @@ -16,12 +15,12 @@ import java.util.List; public class ExamCycleController { @Autowired - private ExamCycleService examCycleService; + private ExamCycleService service; @PostMapping("/create") - public ResponseEntity<ExamCycle> createExamCycle(@RequestBody ExamCycleDTO examCycleDTO) { + public ResponseEntity<ExamCycle> createExamCycle(@RequestBody ExamCycle examCycle) { try { - ExamCycle createdExamCycle = examCycleService.createExamCycle(examCycleDTO); + ExamCycle createdExamCycle = service.createExamCycle(examCycle); return new ResponseEntity<>(createdExamCycle, HttpStatus.CREATED); } catch (Exception e) { throw new ServiceException("Failed to create ExamCycle.", HttpStatus.INTERNAL_SERVER_ERROR); @@ -30,7 +29,7 @@ public class ExamCycleController { @GetMapping("/list") public ResponseEntity<List<ExamCycle>> getAllExamCycles() { try { - List<ExamCycle> examCycles = examCycleService.getAllExamCycles(); + List<ExamCycle> examCycles = service.getAllExamCycles(); if (examCycles.isEmpty()) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @@ -43,7 +42,7 @@ public class ExamCycleController { @GetMapping("/{id}") public ResponseEntity<ExamCycle> getExamCycleById(@PathVariable Long id) { try { - ExamCycle examCycle = examCycleService.getExamCycleById(id); + ExamCycle examCycle = service.getExamCycleById(id); if (examCycle == null) { throw new ServiceException("ExamCycle not found with ID: " + id, HttpStatus.NOT_FOUND); } @@ -54,20 +53,11 @@ public class ExamCycleController { 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) { + public ResponseEntity<ExamCycle> updateExamCycle(@PathVariable Long id, @RequestBody ExamCycle examCycle) { try { - ExamCycle updatedExamCycle = examCycleService.updateExamCycle(id, examCycleDTO); + ExamCycle updatedExamCycle = service.updateExamCycle(id, examCycle); if (updatedExamCycle == null) { throw new ServiceException("ExamCycle not found with ID: " + id, HttpStatus.NOT_FOUND); } @@ -79,4 +69,24 @@ public class ExamCycleController { } } + @DeleteMapping("/delete/{id}") + public ResponseEntity<Void> deleteExamCycle(@PathVariable Long id) { + try { + service.deleteExamCycle(id); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } catch (Exception e) { + throw new ServiceException("Failed to delete ExamCycle with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + // Restore a soft-deleted exam + @PutMapping("/{id}/restore") + public ResponseEntity<Void> restoreExamCycle(@PathVariable Long id) { + try { + service.restoreExamCycle(id); + return ResponseEntity.ok().build(); + } catch (Exception e) { + throw new ServiceException("Failed to restore ExamCycle with ID: " + id, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + } \ No newline at end of file diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/CourseDetails.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/CourseDetails.java deleted file mode 100644 index 11a23f50f9560e194015b86ef31240a88f5771b8..0000000000000000000000000000000000000000 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/CourseDetails.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.tarento.upsmf.examsAndAdmissions.model; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import lombok.*; - -import javax.persistence.*; -import java.util.List; - -@Entity -@Table(name = "course_details") -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -@ToString -@Builder -public class CourseDetails { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @ManyToOne - @JoinColumn(name = "course_id") - private Course course; - - @ManyToOne - @JoinColumn(name = "exam_cycle_id") - @JsonIgnore - private ExamCycle examCycle; - - @OneToMany(mappedBy = "courseDetails", cascade = CascadeType.ALL) - private List<Exam> exams; -} diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/Exam.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/Exam.java new file mode 100644 index 0000000000000000000000000000000000000000..00a110d8fc7d4fea752bd97f7c90737a1decb7b2 --- /dev/null +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/Exam.java @@ -0,0 +1,48 @@ +package com.tarento.upsmf.examsAndAdmissions.model; + +import lombok.*; + +import javax.persistence.*; +import java.time.LocalDate; +import java.time.LocalDateTime; + +@Entity +@Table(name = "exam") +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@ToString +@Builder +public class Exam { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "exam_cycle_id") + private Long examCycleId; // Link to the ExamCycle entity + + @Column(name = "exam_date") + private LocalDate examDate; + + @Column(name = "created_by") + private String createdBy; + + @Column(name = "created_on") + private LocalDateTime createdOn; + + @Column(name = "modified_by") + private String modifiedBy; + + @Column(name = "modified_on") + private LocalDateTime modifiedOn; + + @ManyToOne + @JoinColumn(name = "course_id") + private Course course; + + @Column(name = "obsolete", nullable = false, columnDefinition = "int default 0") + private Integer obsolete = 0; + +} 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 5f4b12b5baa1103a4634eaf6d28d3ed41081fe9f..36ec065299a952cc320e81828d26df67be54f491 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/ExamCycle.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/model/ExamCycle.java @@ -1,13 +1,10 @@ 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 @Table(name = "exam_cycle") @@ -23,23 +20,38 @@ public class ExamCycle { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - private String examCycleName; // New field + @Column(name = "exam_cycle_name", nullable = false) + private String examCycleName; - @ManyToOne - @JoinColumn(name = "course_id") - private Course course; // Represents the Course-ID + @Column(name = "course_id") + private String courseId; - private LocalDate startDate; // Represents start_date - private LocalDate endDate; // Represents end_date + @Column(name = "start_date") + private LocalDate startDate; + + @Column(name = "end_date") + private LocalDate endDate; + + @Column(name = "created_by") private String createdBy; + + @Column(name = "created_on") private LocalDateTime createdOn; + + @Column(name = "modified_by") private String modifiedBy; + + @Column(name = "modified_on") 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; + @Enumerated(EnumType.STRING) + @Column(name = "status") + private Status status; + + @Column(name = "obsolete", nullable = false, columnDefinition = "int default 0") + private Integer obsolete = 0; - // ... rest of your code ... + public enum Status { + PUBLISH, DRAFT + } } diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/repository/ExamCycleRepository.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/repository/ExamCycleRepository.java index e42d59176c7fa1f775d301ac985ba05df95b3309..ca167d5b8aa91c91715c955a2569b71d9f6f9c01 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/repository/ExamCycleRepository.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/repository/ExamCycleRepository.java @@ -1,11 +1,16 @@ package com.tarento.upsmf.examsAndAdmissions.repository; import com.tarento.upsmf.examsAndAdmissions.model.ExamCycle; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface ExamCycleRepository extends JpaRepository<ExamCycle, Long> { + + // Fetch all non-obsolete records + List<ExamCycle> findByObsolete(Integer value); + // Fetch a non-obsolete record by ID + Optional<ExamCycle> findByIdAndObsolete(Long id, Integer value); } diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/repository/ExamRepository.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/repository/ExamRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..c67e34d9a1c700de0f91a2014e729de6e8a56104 --- /dev/null +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/repository/ExamRepository.java @@ -0,0 +1,14 @@ +package com.tarento.upsmf.examsAndAdmissions.repository; + +import com.tarento.upsmf.examsAndAdmissions.model.Exam; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface ExamRepository extends JpaRepository<Exam, Long> { + + List<Exam> findByObsolete(Integer value); + + Optional<Exam> findByIdAndObsolete(Long id, Integer value); +} 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 35b19f33a0b0115651275e4623afaeea95614661..3a5fe2e503bee1105a10157d84ab537d2aedde70 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamCycleService.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamCycleService.java @@ -1,125 +1,91 @@ package com.tarento.upsmf.examsAndAdmissions.service; -import com.tarento.upsmf.examsAndAdmissions.model.Course; -import com.tarento.upsmf.examsAndAdmissions.model.CourseDetails; -import com.tarento.upsmf.examsAndAdmissions.model.Exam; import com.tarento.upsmf.examsAndAdmissions.model.ExamCycle; -import com.tarento.upsmf.examsAndAdmissions.model.dto.CourseDetailDTO; -import com.tarento.upsmf.examsAndAdmissions.model.dto.ExamCycleDTO; -import com.tarento.upsmf.examsAndAdmissions.model.dto.ExamDTO; -import com.tarento.upsmf.examsAndAdmissions.repository.CourseRepository; import com.tarento.upsmf.examsAndAdmissions.repository.ExamCycleRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import javax.persistence.EntityNotFoundException; -import java.time.Duration; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.ArrayList; +import java.util.Date; import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; @Service public class ExamCycleService { + private static final Logger logger = LoggerFactory.getLogger(ExamCycleService.class); + @Autowired - private ExamCycleRepository examCycleRepository; - @Autowired - private CourseRepository courseRepository; + private ExamCycleRepository repository; - public ExamCycle createExamCycle(ExamCycleDTO examCycleDTO) { - ExamCycle examCycle = convertToEntity(examCycleDTO); - return examCycleRepository.save(examCycle); + // Create a new exam cycle + public ExamCycle createExamCycle(ExamCycle examCycle) { + logger.info("Creating new ExamCycle: {}", examCycle); + examCycle.setObsolete(0); + return repository.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); + // Fetch all active exam cycles + public List<ExamCycle> getAllExamCycles() { + logger.info("Fetching all active ExamCycles..."); + return repository.findByObsolete(0); } - 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()); + // Fetch all soft-deleted exam cycles + public List<ExamCycle> getAllObsoleteExamCycles() { + logger.info("Fetching all soft-deleted ExamCycles..."); + return repository.findByObsolete(1); + } - 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()); + // Fetch a specific exam cycle by its ID + public ExamCycle getExamCycleById(Long id) { + logger.info("Fetching ExamCycle by ID: {}", id); + return repository.findByIdAndObsolete(id, 0).orElse(null); + } - 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); + // Update an existing exam cycle + public ExamCycle updateExamCycle(Long id, ExamCycle updatedExamCycle) { + logger.info("Updating ExamCycle with ID: {}", id); + ExamCycle existingExamCycle = repository.findById(id).orElse(null); + if (existingExamCycle != null) { + existingExamCycle.setExamCycleName(updatedExamCycle.getExamCycleName()); + existingExamCycle.setCourseId(updatedExamCycle.getCourseId()); + existingExamCycle.setStartDate(updatedExamCycle.getStartDate()); + existingExamCycle.setEndDate(updatedExamCycle.getEndDate()); + existingExamCycle.setStatus(updatedExamCycle.getStatus()); + + // Update auditing metadata + // Assuming you have a way to fetch the current user, e.g., a utility method + existingExamCycle.setModifiedBy(updatedExamCycle.getModifiedBy()); + existingExamCycle.setModifiedOn(updatedExamCycle.getModifiedOn()); // Current date/time + + return repository.save(existingExamCycle); } - examCycle.setCourseDetails(courseDetailsList); - - return examCycle; + logger.warn("ExamCycle with ID: {} not found!", id); + return null; } - - - public List<ExamCycle> getAllExamCycles() { - return examCycleRepository.findAll(); + // Soft delete an exam cycle + public void deleteExamCycle(Long id) { + logger.info("Soft-deleting ExamCycle with ID: {}", id); + ExamCycle examCycle = repository.findById(id).orElse(null); + if (examCycle != null) { + examCycle.setObsolete(1); + repository.save(examCycle); + } else { + logger.warn("ExamCycle with ID: {} not found for deletion!", id); + } } - public ExamCycle getExamCycleById(Long id) { - return examCycleRepository.findById(id).orElse(null); + // Restore a soft-deleted exam cycle + public void restoreExamCycle(Long id) { + logger.info("Restoring soft-deleted ExamCycle with ID: {}", id); + ExamCycle examCycle = repository.findById(id).orElse(null); + if (examCycle != null && examCycle.getObsolete() == 1) { + examCycle.setObsolete(0); + repository.save(examCycle); + } else { + logger.warn("ExamCycle with ID: {} not found for restoration!", id); + } } -} +} \ No newline at end of file diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamService.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamService.java new file mode 100644 index 0000000000000000000000000000000000000000..7f94ed6a7a97a44fd02a641f368bfc1d2000ef01 --- /dev/null +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/ExamService.java @@ -0,0 +1,79 @@ +package com.tarento.upsmf.examsAndAdmissions.service; + +import com.tarento.upsmf.examsAndAdmissions.model.Exam; +import com.tarento.upsmf.examsAndAdmissions.repository.ExamRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ExamService { + + private static final Logger logger = LoggerFactory.getLogger(ExamService.class); + + @Autowired + private ExamRepository repository; + + public Exam createExam(Exam exam) { + logger.info("Creating new Exam: {}", exam); + exam.setObsolete(0); + return repository.save(exam); + } + + public List<Exam> getAllExams() { + logger.info("Fetching all active Exams..."); + return repository.findByObsolete(0); + } + + public Exam getExamById(Long id) { + logger.info("Fetching Exam by ID: {}", id); + return repository.findByIdAndObsolete(id, 0).orElse(null); + } + + public Exam updateExam(Long id, Exam updatedExam) { + logger.info("Updating Exam with ID: {}", id); + Exam existingExam = repository.findById(id).orElse(null); + if (existingExam != null) { + existingExam.setExamCycleId(updatedExam.getExamCycleId()); + existingExam.setExamDate(updatedExam.getExamDate()); + + // Update auditing metadata from the payload + existingExam.setModifiedBy(updatedExam.getModifiedBy()); + existingExam.setModifiedOn(updatedExam.getModifiedOn()); + + // Soft delete or status flag, if you want to allow it from the payload: + existingExam.setObsolete(updatedExam.getObsolete()); + + return repository.save(existingExam); + } + logger.warn("Exam with ID: {} not found!", id); + return null; + } + + + + public void deleteExam(Long id) { + logger.info("Soft-deleting Exam with ID: {}", id); + Exam exam = repository.findById(id).orElse(null); + if (exam != null) { + exam.setObsolete(1); + repository.save(exam); + } else { + logger.warn("Exam with ID: {} not found for deletion!", id); + } + } + + public void restoreExam(Long id) { + logger.info("Restoring soft-deleted Exam with ID: {}", id); + Exam exam = repository.findById(id).orElse(null); + if (exam != null && exam.getObsolete() == 1) { + exam.setObsolete(0); + repository.save(exam); + } else { + logger.warn("Exam with ID: {} not found for restoration!", id); + } + } +} \ No newline at end of file