Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/main/java/org/pkwmtt/examCalendar/ExamController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.pkwmtt.examCalendar.dto.ExamDto;
import org.pkwmtt.examCalendar.dto.RequestExamDto;
import org.pkwmtt.examCalendar.dto.ResponseExamDto;
import org.pkwmtt.examCalendar.entity.Exam;
import org.pkwmtt.examCalendar.entity.ExamType;
import org.pkwmtt.examCalendar.mapper.ExamDtoMapper;
Expand All @@ -26,13 +27,13 @@ public class ExamController {
private final ExamService examService;

/**
* @param examDto details of exam
* @param requestExamDto details of exam
* @return 201 created with URI to GET method which returns created resource
*/
@PostMapping("")
@SecurityRequirement(name = "bearerAuth")
public ResponseEntity<Void> addExam(@RequestBody @Valid ExamDto examDto){
int id = examService.addExam(examDto);
public ResponseEntity<Void> addExam(@RequestBody @Valid RequestExamDto requestExamDto){
int id = examService.addExam(requestExamDto);
URI uri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
Expand All @@ -43,13 +44,13 @@ public ResponseEntity<Void> addExam(@RequestBody @Valid ExamDto examDto){

/**
* @param id of exam or test
* @param examDto new details of exam or test
* @param requestExamDto new details of exam or test
* @return 204 no content
*/
@PutMapping("/{id}")
@SecurityRequirement(name = "bearerAuth")
public ResponseEntity<Void> modifyExam(@PathVariable @Positive int id, @RequestBody @Valid ExamDto examDto) {
examService.modifyExam(examDto, id);
public ResponseEntity<Void> modifyExam(@PathVariable @Positive int id, @RequestBody @Valid RequestExamDto requestExamDto) {
examService.modifyExam(requestExamDto, id);
return ResponseEntity.noContent().build();
}

Expand All @@ -68,7 +69,7 @@ public ResponseEntity<Void> deleteExam(@PathVariable int id) {
* @param id of exam or test
* @return 200 ok with single exam or test details
*/
@GetMapping("/{id}")
// @GetMapping("/{id}")
public ResponseEntity<Exam> getExam(@PathVariable int id) {
return ResponseEntity.ok(examService.getExamById(id));
}
Expand All @@ -77,10 +78,10 @@ public ResponseEntity<Exam> getExam(@PathVariable int id) {
* when subgroups isn't null all generalGroups must be form the same year of study. e.g. 12K2, 12K1 is from 12K
* @param generalGroups set of general groups e.g. 12K2
* @param subgroups set of subgroups of general group e.g. L04
* @return List of ExamDto for specific groups
* @return List of RequestExamDto for specific groups
*/
@GetMapping("/by-groups")
public ResponseEntity<List<ExamDto>> getExams(
public ResponseEntity<List<ResponseExamDto>> getExams(
@RequestParam Set<String> generalGroups,
@RequestParam(required = false) Set<String> subgroups
){
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/org/pkwmtt/examCalendar/ExamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.pkwmtt.examCalendar.dto.ExamDto;
import org.pkwmtt.examCalendar.dto.RequestExamDto;
import org.pkwmtt.examCalendar.entity.Exam;
import org.pkwmtt.examCalendar.entity.ExamType;
import org.pkwmtt.examCalendar.entity.StudentGroup;
Expand Down Expand Up @@ -34,19 +34,19 @@ public class ExamService {
private final TimetableService timetableService;

/**
* @param examDto details of exam
* @param requestExamDto details of exam
* @return id of exam added to database
*/
public int addExam(ExamDto examDto) {
public int addExam(RequestExamDto requestExamDto) {

verifyGroupPermissionsForNewResource(examDto.getGeneralGroups());
verifyGroupPermissionsForNewResource(requestExamDto.getGeneralGroups());

Set<StudentGroup> groups = verifyAndUpdateExamGroups(examDto);
Set<StudentGroup> groups = verifyAndUpdateExamGroups(requestExamDto);

ExamType examType = examTypeRepository.findByName(examDto.getExamType())
.orElseThrow(() -> new ExamTypeNotExistsException(examDto.getExamType()));
ExamType examType = examTypeRepository.findByName(requestExamDto.getExamType())
.orElseThrow(() -> new ExamTypeNotExistsException(requestExamDto.getExamType()));

Exam exam = ExamDtoMapper.mapToNewExam(examDto, groups, examType);
Exam exam = ExamDtoMapper.mapToNewExam(requestExamDto, groups, examType);
Set<Exam> existingExam = examRepository.findAllByTitle(exam.getTitle());

if (existingExam.contains(exam))
Expand All @@ -55,21 +55,21 @@ public int addExam(ExamDto examDto) {
}

/**
* @param examDto new details of exam that overwrite old ones
* @param requestExamDto new details of exam that overwrite old ones
* @param id of exam that need to be modified
*/
public void modifyExam(ExamDto examDto, int id) {
public void modifyExam(RequestExamDto requestExamDto, int id) {

examRepository.findById(id).orElseThrow(() -> new NoSuchElementWithProvidedIdException(id));

verifyGroupPermissionsForModifiedResource(examDto.getGeneralGroups(), id);
verifyGroupPermissionsForModifiedResource(requestExamDto.getGeneralGroups(), id);

Set<StudentGroup> groups = verifyAndUpdateExamGroups(examDto);
Set<StudentGroup> groups = verifyAndUpdateExamGroups(requestExamDto);

ExamType examType = examTypeRepository.findByName(examDto.getExamType())
.orElseThrow(() -> new ExamTypeNotExistsException(examDto.getExamType()));
ExamType examType = examTypeRepository.findByName(requestExamDto.getExamType())
.orElseThrow(() -> new ExamTypeNotExistsException(requestExamDto.getExamType()));

examRepository.save(ExamDtoMapper.mapToExistingExam(examDto, groups, examType, id));
examRepository.save(ExamDtoMapper.mapToExistingExam(requestExamDto, groups, examType, id));
}

/**
Expand Down Expand Up @@ -119,13 +119,13 @@ public List<ExamType> getExamTypes() {
/**
* verify if groups exists and updates database when it exists, but repository doesn't contain it.
* When timetable service is unavailable verifies groups using groupsRepository
* @param examDto containing groups for verification
* @param requestExamDto containing groups for verification
* @return single set of all kinds of provided groups as StudentGroup entities
* that are in database and could be safely attach to Exam entity
*/
private Set<StudentGroup> verifyAndUpdateExamGroups(ExamDto examDto) {
Set<String> generalGroups = examDto.getGeneralGroups();
Set<String> subgroups = examDto.getSubgroups();
private Set<StudentGroup> verifyAndUpdateExamGroups(RequestExamDto requestExamDto) {
Set<String> generalGroups = requestExamDto.getGeneralGroups();
Set<String> subgroups = requestExamDto.getSubgroups();

if (generalGroups == null || generalGroups.isEmpty())
throw new InvalidGroupIdentifierException("general group is missing");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
package org.pkwmtt.examCalendar.dto;

import jakarta.validation.constraints.*;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.pkwmtt.examCalendar.entity.StudentGroup;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;
import java.util.Set;

@Getter
@RequiredArgsConstructor
@Builder
public class ExamDto {
@SuperBuilder
public class RequestExamDto {

@NotBlank
@Size(max = 255, message = "max size of field is 255")
private final String title;
private String title;

@Size(max = 255, message = "max size of field is 255")
private final String description;
private String description;

@Future(message = "Date must be in the future")
@NotNull
private final LocalDateTime date;
private LocalDateTime date;

@NotNull
private final String examType;
private String examType;

@NotEmpty
@Size(min = 1)
private final Set<String> generalGroups;
private Set<String> generalGroups;

private final Set<String> subgroups;
private Set<String> subgroups;
}
12 changes: 12 additions & 0 deletions src/main/java/org/pkwmtt/examCalendar/dto/ResponseExamDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.pkwmtt.examCalendar.dto;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@Getter
@SuperBuilder
public class ResponseExamDto extends RequestExamDto {

private int examId;

}
34 changes: 18 additions & 16 deletions src/main/java/org/pkwmtt/examCalendar/mapper/ExamDtoMapper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.pkwmtt.examCalendar.mapper;

import lombok.extern.slf4j.Slf4j;
import org.pkwmtt.examCalendar.dto.ExamDto;
import org.pkwmtt.examCalendar.dto.RequestExamDto;
import org.pkwmtt.examCalendar.dto.ResponseExamDto;
import org.pkwmtt.examCalendar.entity.Exam;
import org.pkwmtt.examCalendar.entity.ExamType;
import org.pkwmtt.examCalendar.entity.StudentGroup;
Expand All @@ -11,7 +12,7 @@
import java.util.stream.Collectors;

/**
* Utility class for mapping Exam entity to ExamDto and ExamDto to Exam entity
* Utility class for mapping Exam entity to RequestExamDto and RequestExamDto to Exam entity
*/
@Slf4j
public class ExamDtoMapper {
Expand All @@ -20,32 +21,32 @@ private ExamDtoMapper() {
}

/**
* @param examDto examDto object received from request
* @param requestExamDto requestExamDto object received from request
* @return Exam entity WITHOUT examId which should be assigned by database
* Also contains examType field converted from String do ExamType
*/
public static Exam mapToNewExam(ExamDto examDto, Set<StudentGroup> groups, ExamType examType) {
public static Exam mapToNewExam(RequestExamDto requestExamDto, Set<StudentGroup> groups, ExamType examType) {
return Exam.builder()
.title(examDto.getTitle())
.description(examDto.getDescription())
.examDate(examDto.getDate())
.title(requestExamDto.getTitle())
.description(requestExamDto.getDescription())
.examDate(requestExamDto.getDate())
.examType(examType)
.groups(groups)
.build();
}

/**
* @param examDto examDto object received from request
* @param requestExamDto requestExamDto object received from request
* @param id of Exam that need to be modified
* @return Exam entity WITH examId that allow to update entity in database instead of creating new one
* Also contains examType field converted from String do ExamType
*/
public static Exam mapToExistingExam(ExamDto examDto, Set<StudentGroup> groups, ExamType examType, int id) {
public static Exam mapToExistingExam(RequestExamDto requestExamDto, Set<StudentGroup> groups, ExamType examType, int id) {
return Exam.builder()
.examId(id)
.title(examDto.getTitle())
.description(examDto.getDescription())
.examDate(examDto.getDate())
.title(requestExamDto.getTitle())
.description(requestExamDto.getDescription())
.examDate(requestExamDto.getDate())
.examType(examType)
.groups(groups)
.build();
Expand All @@ -55,21 +56,22 @@ public static Exam mapToExistingExam(ExamDto examDto, Set<StudentGroup> groups,
* @param exams Set of Exams that would be mapped
* @return List of ExamDtos
*/
public static List<ExamDto> mapToExamDto(Set<Exam> exams) {
public static List<ResponseExamDto> mapToExamDto(Set<Exam> exams) {
return exams.stream().map(ExamDtoMapper::mapToExamDto).collect(Collectors.toList());
}

/**
* @param exam single exam that would be mapped
* @return ExamDto
* @return RequestExamDto
*/
public static ExamDto mapToExamDto(Exam exam) {
public static ResponseExamDto mapToExamDto(Exam exam) {
Set<String> groups = exam.getGroups().stream().map(StudentGroup::getName).collect(Collectors.toSet());
Set<String> generalGroups = groups.stream().filter(group -> Character.isDigit(group.charAt(0))).collect(Collectors.toSet());
Set<String> subgroups = groups.stream().filter(group -> Character.isAlphabetic(group.charAt(0))).collect(Collectors.toSet());
if(groups.size() != subgroups.size() + generalGroups.size())
log.warn("Some groups of {} were not consumed in ExamDtoMapper.mapToExamDto()", groups);
return ExamDto.builder()
return ResponseExamDto.builder()
.examId(exam.getExamId())
.title(exam.getTitle())
.description(exam.getDescription())
.date(exam.getExamDate())
Expand Down
Loading