Skip to content

Commit

Permalink
added shelters and volunteers, also added a better mesage on voluntee…
Browse files Browse the repository at this point in the history
…r from different shelter error
  • Loading branch information
xipi3000 committed Jun 7, 2024
1 parent d185971 commit 8eb6f9e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cat.udl.eps.softarch.demo.config;

import cat.udl.eps.softarch.demo.domain.Admin;
import cat.udl.eps.softarch.demo.domain.Shelter;
import cat.udl.eps.softarch.demo.domain.ShelterVolunteer;
import cat.udl.eps.softarch.demo.domain.User;
import cat.udl.eps.softarch.demo.repository.AdminRepository;
import cat.udl.eps.softarch.demo.repository.ShelterRepository;
import cat.udl.eps.softarch.demo.repository.UserRepository;
import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -21,13 +23,15 @@ public class DBInitialization {
private final UserRepository userRepository;
private final AdminRepository adminRepository;
private final ShelterVolunteerRepository shelterVolunteerRepository;

private final ShelterRepository shelterRepository;
public DBInitialization(UserRepository userRepository,
AdminRepository adminRepository,
ShelterVolunteerRepository shelterVolunteerRepository) {
ShelterVolunteerRepository shelterVolunteerRepository,
ShelterRepository shelterRepository) {
this.userRepository = userRepository;
this.adminRepository = adminRepository;
this.shelterVolunteerRepository = shelterVolunteerRepository;
this.shelterRepository = shelterRepository;
}

@PostConstruct
Expand All @@ -51,17 +55,65 @@ public void initializeDatabase() {
admin.encodePassword();
userRepository.save(admin);
}
if (!adminRepository.existsById("admin")) {
Admin admin = new Admin();
admin.setEmail("[email protected]");
admin.setId("admin");
admin.setPassword(defaultPassword);
admin.encodePassword();
userRepository.save(admin);
}

// Default ShelterVolunteer
if(shelterRepository.findByEmail("[email protected]").isEmpty()) {
Shelter shelter = new Shelter();
shelter.setName("shelter");
shelter.setEmail("[email protected]");
shelter.setMobile("999999990");
shelter.setActive(true);
shelterRepository.save(shelter);
}
if(shelterRepository.findByEmail("[email protected]").isEmpty()) {
Shelter shelter = new Shelter();
shelter.setName("shelter1");
shelter.setEmail("[email protected]");
shelter.setMobile("999999999");
shelter.setActive(true);
shelterRepository.save(shelter);

if (!shelterVolunteerRepository.existsById("volunteer")) {
ShelterVolunteer volunteer = new ShelterVolunteer();
volunteer.setEmail("[email protected]");
volunteer.setId("volunteer");
volunteer.setPassword(defaultPassword);
volunteer.encodePassword();
volunteer.setUserShelter(shelter);
shelterVolunteerRepository.save(volunteer);
}
if (!shelterVolunteerRepository.existsById("volunteer1")) {
ShelterVolunteer volunteer = new ShelterVolunteer();
volunteer.setEmail("[email protected]");
volunteer.setId("volunteer1");
volunteer.setPassword(defaultPassword);
volunteer.setUserShelter(shelter);
volunteer.encodePassword();
shelterVolunteerRepository.save(volunteer);
}

if (!shelterVolunteerRepository.existsById("volunteer2")) {
ShelterVolunteer volunteer = new ShelterVolunteer();
volunteer.setEmail("[email protected]");
volunteer.setId("volunteer2");
volunteer.setPassword(defaultPassword);
volunteer.setUserShelter(shelter);
volunteer.encodePassword();
shelterVolunteerRepository.save(volunteer);
}


}


if (Arrays.asList(activeProfiles.split(",")).contains("test")) {
// Testing instances
if (!userRepository.existsById("test")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cat.udl.eps.softarch.demo.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.PRECONDITION_FAILED, reason = "El usuario debe ser de tipo shelterVolunteer")
@ResponseStatus(code = HttpStatus.PRECONDITION_FAILED, reason = "The volunteers must be in the same shelter")
public class VolunteerFromDifferentShelter extends RuntimeException { }

0 comments on commit 8eb6f9e

Please sign in to comment.