Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
import org.springframework.transaction.annotation.Transactional;
import org.unilab.uniplan.category.dto.CategoryRequestDto;
import org.unilab.uniplan.category.dto.CategoryResponseDto;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.exception.CategoryNotFoundException;
import java.util.List;
import java.util.UUID;

import static org.unilab.uniplan.utils.ErrorConstants.CATEGORY_NOT_FOUND;

@Component
@Slf4j
@RequiredArgsConstructor
Expand Down Expand Up @@ -51,10 +49,10 @@ public void deleteCategory(final UUID id) {
public List<CategoryResponseDto> getAllCategories() {
return categoryMapper.toResponseDtoList(categoryService.getAll());
}

private Category getCategoryOrThrow(final UUID id) {
return categoryService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(
CATEGORY_NOT_FOUND.getMessage(String.valueOf(id)))
.orElseThrow(() -> new CategoryNotFoundException(id)
);
}
}
12 changes: 4 additions & 8 deletions src/main/java/org/unilab/uniplan/course/CourseService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.unilab.uniplan.course;

import static org.unilab.uniplan.utils.ErrorConstants.COURSE_NOT_FOUND;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.unilab.uniplan.course.dto.CourseDto;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.exception.CourseNotFoundException;

@Service
@RequiredArgsConstructor
Expand All @@ -25,8 +24,7 @@ public CourseDto createCourse(final CourseDto courseDTO) {
public CourseDto findCourseById(final UUID id) {
return courseRepository.findById(id)
.map(courseMapper::toDto)
.orElseThrow(() -> new ResourceNotFoundException(COURSE_NOT_FOUND.getMessage(
String.valueOf(id))));
.orElseThrow(() -> new CourseNotFoundException(id));
}

public List<CourseDto> findAllByMajorId(final UUID majorId) {
Expand All @@ -46,15 +44,13 @@ public CourseDto updateCourse(final UUID id, final CourseDto courseDTO) {
existingCourse -> updateEntityAndConvertToDto(
courseDTO,
existingCourse))
.orElseThrow(() -> new ResourceNotFoundException(COURSE_NOT_FOUND.getMessage(
String.valueOf(id))));
.orElseThrow(() -> new CourseNotFoundException(id));
}

@Transactional
public void deleteCourse(final UUID id) {
final Course course = courseRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(
COURSE_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new CourseNotFoundException(id));
courseRepository.delete(course);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.unilab.uniplan.coursegroup;

import static org.unilab.uniplan.utils.ErrorConstants.COURSE_GROUP_NOT_FOUND;

import jakarta.transaction.Transactional;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.unilab.uniplan.coursegroup.dto.CourseGroupDto;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.exception.CourseGroupNotFoundException;

@Service
@RequiredArgsConstructor
Expand All @@ -26,8 +24,7 @@ public CourseGroupDto createCourseGroup(final CourseGroupDto courseGroupDTO) {
public CourseGroupDto findCourseGroupById(final UUID id) {
return courseGroupRepository.findById(id)
.map(courseGroupMapper::toDto)
.orElseThrow(() -> new ResourceNotFoundException(
COURSE_GROUP_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new CourseGroupNotFoundException(id));
}

public List<CourseGroupDto> findAll() {
Expand All @@ -37,21 +34,19 @@ public List<CourseGroupDto> findAll() {

@Transactional
public CourseGroupDto updateCourseGroup(final UUID id,
final CourseGroupDto courseGroupDTO) {
final CourseGroupDto courseGroupDTO) {

return courseGroupRepository.findById(id).map(
existingCourseGroup -> updateEntityAndConvertToDto(courseGroupDTO,
existingCourseGroup))
.orElseThrow(() -> new ResourceNotFoundException(
COURSE_GROUP_NOT_FOUND.getMessage(String.valueOf(id))));
existingCourseGroup -> updateEntityAndConvertToDto(courseGroupDTO,
existingCourseGroup))
.orElseThrow(() -> new CourseGroupNotFoundException(id));
}

@Transactional
public void deleteCourseGroup(final UUID id) {
final CourseGroup courseGroup = courseGroupRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(
COURSE_GROUP_NOT_FOUND.getMessage(
String.valueOf(id))));
.orElseThrow(
() -> new CourseGroupNotFoundException(id));
courseGroupRepository.delete(courseGroup);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.unilab.uniplan.department;

import static org.unilab.uniplan.utils.ErrorConstants.DEPARTMENT_NOT_FOUND;
import static org.unilab.uniplan.utils.ErrorConstants.FACULTY_NOT_FOUND;

import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.unilab.uniplan.department.dto.DepartmentDto;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.exception.DepartmentNotFoundException;
import org.unilab.uniplan.exception.FacultyNotFoundException;
import org.unilab.uniplan.faculty.Faculty;
import org.unilab.uniplan.faculty.FacultyRepository;

Expand Down Expand Up @@ -37,8 +35,7 @@ public List<DepartmentDto> getAllDepartments() {
public DepartmentDto getDepartmentById(final UUID id) {
return departmentRepository.findById(id)
.map(departmentMapper::toDto)
.orElseThrow(() -> new ResourceNotFoundException(
DEPARTMENT_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new DepartmentNotFoundException(id));
}

@Transactional
Expand All @@ -47,20 +44,18 @@ public DepartmentDto updateDepartment(final UUID id,
return departmentRepository.findById(id)
.map(existingDepartment -> {
final Faculty faculty = facultyRepository.findById(departmentDto.facultyId())
.orElseThrow(() -> new ResourceNotFoundException(
FACULTY_NOT_FOUND.getMessage(String.valueOf(departmentDto.facultyId()))));
.orElseThrow(
() -> new FacultyNotFoundException(departmentDto.facultyId()));
existingDepartment.setFaculty(faculty);
return updateEntityAndConvertToDto(departmentDto, existingDepartment);
})
.orElseThrow(() -> new ResourceNotFoundException(
DEPARTMENT_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new DepartmentNotFoundException(id));
}

@Transactional
public void deleteDepartment(final UUID id) {
final Department department = departmentRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(
DEPARTMENT_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new DepartmentNotFoundException(id));
departmentRepository.delete(department);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.unilab.uniplan.discipline;

import static org.unilab.uniplan.utils.ErrorConstants.DISCIPLINE_NOT_FOUND;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;
Expand All @@ -10,7 +8,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.unilab.uniplan.discipline.dto.DisciplineDto;
import org.unilab.uniplan.exception.ResourceNotFoundException;
import org.unilab.uniplan.exception.DisciplineNotFoundException;

@Service
@RequiredArgsConstructor
Expand All @@ -36,8 +34,7 @@ public List<DisciplineDto> getAllDisciplines() {
public DisciplineDto getDisciplineById(@NotNull UUID id) {
return disciplineRepository.findById(id)
.map(disciplineMapper::toDto)
.orElseThrow(() -> new ResourceNotFoundException(
DISCIPLINE_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new DisciplineNotFoundException(id));
}

@Transactional
Expand All @@ -46,15 +43,13 @@ public DisciplineDto updateDiscipline(UUID id, @Valid DisciplineDto disciplineDt
.map(existingDiscipline -> updateEntityAndConvertToDto(
disciplineDto,
existingDiscipline))
.orElseThrow(() -> new ResourceNotFoundException(
DISCIPLINE_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new DisciplineNotFoundException(id));
}

@Transactional
public void deleteDiscipline(UUID id) {
final Discipline discipline = disciplineRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(
DISCIPLINE_NOT_FOUND.getMessage(String.valueOf(id))));
.orElseThrow(() -> new DisciplineNotFoundException(id));
disciplineRepository.delete(discipline);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class CategoryNotFoundException extends ResourceNotFoundException {

public CategoryNotFoundException(final UUID id) {
super("category_not_found", id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class CourseGroupNotFoundException extends ResourceNotFoundException {

public CourseGroupNotFoundException(final UUID id) {
super("course_group_not_found", id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class CourseNotFoundException extends ResourceNotFoundException {

public CourseNotFoundException(final UUID id) {
super("course_not_found", id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class DepartmentNotFoundException extends ResourceNotFoundException {

public DepartmentNotFoundException(final UUID id) {
super("department_not_found", id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class DisciplineNotFoundException extends ResourceNotFoundException {

public DisciplineNotFoundException(final UUID id) {
super("discipline_not_found", id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class FacultyNotFoundException extends ResourceNotFoundException {

public FacultyNotFoundException(final UUID id) {
super("faculty_not_found", id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ public class GlobalExceptionHandler {

//Handles resource not found exceptions triggered by element not found by search parameters
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorResponse> handleValidationException(final ResourceNotFoundException ex,
public ResponseEntity<ErrorResponse> handleResourceNotFoundException(final ResourceNotFoundException ex,
final HttpServletRequest request,
final Locale locale) {
final Locale locale) {
String message = messageSource.getMessage(
ex.getMessageKey(),
ex.getArgs(),
locale);
log.info(ex.getMessage());

return new ResponseEntity<>(
new ErrorResponse(
messageSource.getMessage("resource_not_found", null, locale),
message,
HttpStatus.NOT_FOUND.value(),
LocalDateTime.now(),
request.getRequestURI()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class LectorNotFoundException extends ResourceNotFoundException {

public LectorNotFoundException(final UUID id) {
super("lector_not_found", id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class MajorNotFoundException extends ResourceNotFoundException {

public MajorNotFoundException(final UUID id) {
super("major_not_found", id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;


import org.unilab.uniplan.programdisciplinelector.ProgramDisciplineLectorId;

public class ProgramDisciplineLectorNotFoundException extends ResourceNotFoundException {

public ProgramDisciplineLectorNotFoundException(final ProgramDisciplineLectorId id) {
super("program_discipline_lector_not_found", id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;


import org.unilab.uniplan.programdiscipline.ProgramDisciplineId;

public class ProgramDisciplineNotFoundException extends ResourceNotFoundException {

public ProgramDisciplineNotFoundException(final ProgramDisciplineId id) {
super("program_discipline_not_found", id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.unilab.uniplan.exception;

import java.util.UUID;

public class ProgramNotFoundException extends ResourceNotFoundException {

public ProgramNotFoundException(final UUID id) {
super("program_not_found", id);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package org.unilab.uniplan.exception;

import lombok.Getter;

@Getter
public class ResourceNotFoundException extends RuntimeException {

public ResourceNotFoundException(String message) {
super(message);
private final String messageKey; //ex. resource_not_found
private final Object[] args; //ex. id

public ResourceNotFoundException(String messageKey, Object... args) {
super(messageKey);
this.messageKey = messageKey;
this.args = args;
}
}

}
Loading
Loading