Commit 8d1a7c4c authored by jay pratap singh's avatar jay pratap singh
Browse files

changes for exam cycle

Showing with 182 additions and 24 deletions
+182 -24
......@@ -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>
......
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
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;
}
}
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 ...
}
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
}
......@@ -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();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment