professor and education updated
This commit is contained in:
@ -20,19 +20,14 @@ public class CourseDto {
|
||||
@NotNull
|
||||
private String description;
|
||||
|
||||
@NotNull
|
||||
private String duration;
|
||||
|
||||
@NotNull
|
||||
private Integer seats;
|
||||
|
||||
@NotNull
|
||||
private String category;
|
||||
|
||||
@NotNull
|
||||
private String level;
|
||||
|
||||
@NotNull
|
||||
private String instructor;
|
||||
|
||||
private String price;
|
||||
|
||||
@ -8,7 +8,6 @@ import net.shyshkin.study.fullstack.supportportal.backend.domain.ProfessorCatego
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.ProfessorSkill;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.dto.ProfessorDto;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.WorkingStatus;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.EmailExistsException;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.NotAnImageFileException;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.ProfessorNotFoundException;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.mapper.ProfessorMapper;
|
||||
@ -18,12 +17,12 @@ import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.transaction.Transactional;
|
||||
@ -45,7 +44,6 @@ import static org.springframework.http.MediaType.*;
|
||||
public class ProfessorServiceImpl implements ProfessorService {
|
||||
|
||||
public static final String EMAIL_NOT_FOUND_MSG = "Professor with email `%s` not found";
|
||||
public static final String EMAIL_EXISTS_MSG = "Professor with email `%s` is already registered";
|
||||
public static final String PROFESSOR_NOT_FOUND_MSG = "Professor not found";
|
||||
|
||||
private final ProfessorRepository professorRepository;
|
||||
@ -100,7 +98,7 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
public Professor findByEmail(String email) {
|
||||
return professorRepository
|
||||
.findByEmail(email)
|
||||
.orElseThrow(() -> new EmailExistsException(String.format(EMAIL_NOT_FOUND_MSG, email)));
|
||||
.orElseThrow(() -> new ProfessorNotFoundException(String.format(EMAIL_NOT_FOUND_MSG, email)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -158,19 +156,21 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
@Override
|
||||
@Transactional
|
||||
public Professor addNewProfessor(ProfessorDto professorDto) {
|
||||
validateNewEmail(professorDto.getEmail());
|
||||
// ✅ FIX: Removed validateNewEmail() - duplicate emails are now allowed
|
||||
|
||||
Professor professor = professorMapper.toEntity(professorDto);
|
||||
|
||||
// Set a unique identifier for the professor
|
||||
professor.setProfessorId(generateUuid());
|
||||
professor.setJoinDate(LocalDateTime.now());
|
||||
professor.setJoinDate(
|
||||
professorDto.getJoinDate() != null ? professorDto.getJoinDate() : LocalDateTime.now()
|
||||
);
|
||||
professor.setProfileImageUrl(generateDefaultProfileImageUrl(professor.getProfessorId()));
|
||||
|
||||
// Save the professor first to get the ID
|
||||
Professor savedProfessor = professorRepository.save(professor);
|
||||
|
||||
// Handle skills if provided
|
||||
// ✅ Handle skills if provided
|
||||
if (professorDto.getSkills() != null && !professorDto.getSkills().isEmpty()) {
|
||||
Set<ProfessorSkill> skills = professorDto.getSkills().stream()
|
||||
.filter(skillDto -> skillDto.getName() != null && !skillDto.getName().trim().isEmpty())
|
||||
@ -183,7 +183,7 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
savedProfessor.setSkills(skills);
|
||||
}
|
||||
|
||||
// Handle awards if provided
|
||||
// ✅ Handle awards if provided
|
||||
if (professorDto.getAwards() != null && !professorDto.getAwards().isEmpty()) {
|
||||
Set<ProfessorAward> awards = professorDto.getAwards().stream()
|
||||
.filter(awardDto -> awardDto.getTitle() != null && !awardDto.getTitle().trim().isEmpty())
|
||||
@ -213,9 +213,9 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
@Transactional
|
||||
public Professor updateProfessor(UUID professorId, ProfessorDto professorDto) {
|
||||
Professor professor = professorRepository.findByProfessorId(professorId)
|
||||
.orElseThrow(() -> new RuntimeException("Professor not found with id: " + professorId));
|
||||
.orElseThrow(() -> new ProfessorNotFoundException("Professor not found with id: " + professorId));
|
||||
|
||||
validateUpdateEmail(professorId, professorDto.getEmail());
|
||||
// ✅ FIX: Removed validateUpdateEmail() - duplicate emails are now allowed
|
||||
|
||||
// Update basic fields
|
||||
professor.setFirstName(professorDto.getFirstName());
|
||||
@ -226,7 +226,7 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
professor.setOfficeLocation(professorDto.getOfficeLocation());
|
||||
professor.setStatus(professorDto.getStatus());
|
||||
professor.setCategory(professorDto.getCategory());
|
||||
|
||||
|
||||
// Update extended fields
|
||||
professor.setPhone(professorDto.getPhone());
|
||||
professor.setSpecialty(professorDto.getSpecialty());
|
||||
@ -237,6 +237,7 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
professor.setDesignation(professorDto.getDesignation());
|
||||
professor.setWorkDays(professorDto.getWorkDays());
|
||||
|
||||
// ✅ Update joinDate if provided
|
||||
if (professorDto.getJoinDate() != null) {
|
||||
professor.setJoinDate(professorDto.getJoinDate());
|
||||
}
|
||||
@ -244,16 +245,12 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
// Create a final reference for lambda expressions
|
||||
final Professor professorRef = professor;
|
||||
|
||||
// ✅ CORRECT FIX: Update skills with orphanRemoval=true
|
||||
// Initialize collection if null
|
||||
// ✅ Update skills with orphanRemoval=true
|
||||
if (professor.getSkills() == null) {
|
||||
professor.setSkills(new HashSet<>());
|
||||
}
|
||||
|
||||
// Clear existing skills (orphanRemoval will delete them from DB)
|
||||
professor.getSkills().clear();
|
||||
|
||||
// Add new skills
|
||||
|
||||
if (professorDto.getSkills() != null && !professorDto.getSkills().isEmpty()) {
|
||||
Set<ProfessorSkill> newSkills = professorDto.getSkills().stream()
|
||||
.filter(skillDto -> skillDto.getName() != null && !skillDto.getName().trim().isEmpty())
|
||||
@ -263,20 +260,15 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
.professor(professorRef)
|
||||
.build())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
professor.getSkills().addAll(newSkills);
|
||||
}
|
||||
|
||||
// ✅ CORRECT FIX: Update awards with orphanRemoval=true
|
||||
// Initialize collection if null
|
||||
// ✅ Update awards with orphanRemoval=true
|
||||
if (professor.getAwards() == null) {
|
||||
professor.setAwards(new HashSet<>());
|
||||
}
|
||||
|
||||
// Clear existing awards (orphanRemoval will delete them from DB)
|
||||
professor.getAwards().clear();
|
||||
|
||||
// Add new awards
|
||||
|
||||
if (professorDto.getAwards() != null && !professorDto.getAwards().isEmpty()) {
|
||||
Set<ProfessorAward> newAwards = professorDto.getAwards().stream()
|
||||
.filter(awardDto -> awardDto.getTitle() != null && !awardDto.getTitle().trim().isEmpty())
|
||||
@ -288,12 +280,11 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
.professor(professorRef)
|
||||
.build())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
professor.getAwards().addAll(newAwards);
|
||||
}
|
||||
|
||||
Professor savedProfessor = professorRepository.save(professor);
|
||||
|
||||
|
||||
// Handle profile image if provided
|
||||
if (professorDto.getProfileImage() != null) {
|
||||
saveProfileImage(savedProfessor, professorDto.getProfileImage());
|
||||
@ -340,22 +331,4 @@ public class ProfessorServiceImpl implements ProfessorService {
|
||||
var responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<byte[]>() {});
|
||||
return responseEntity.getBody();
|
||||
}
|
||||
|
||||
private void validateNewEmail(String email) {
|
||||
if (professorRepository.existsByEmail(email)) {
|
||||
throw new EmailExistsException(String.format(EMAIL_EXISTS_MSG, email));
|
||||
}
|
||||
}
|
||||
|
||||
private Professor validateUpdateEmail(UUID professorId, String email) {
|
||||
Objects.requireNonNull(professorId);
|
||||
|
||||
Professor currentProfessor = findByProfessorId(professorId);
|
||||
|
||||
if (!Objects.equals(currentProfessor.getEmail(), email) && professorRepository.existsByEmail(email)) {
|
||||
throw new EmailExistsException(String.format(EMAIL_EXISTS_MSG, email));
|
||||
}
|
||||
|
||||
return currentProfessor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user