From 8eb6f9ec4e13b54673e86b0bd83ba708eef1d479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Fri, 7 Jun 2024 12:56:06 +0200 Subject: [PATCH] added shelters and volunteers, also added a better mesage on volunteer from different shelter error --- .../demo/config/DBInitialization.java | 56 ++++++++++++++++++- .../VolunteerFromDifferentShelter.java | 2 +- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java b/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java index 0e7bf3c..6235332 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java +++ b/src/main/java/cat/udl/eps/softarch/demo/config/DBInitialization.java @@ -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; @@ -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 @@ -51,17 +55,65 @@ public void initializeDatabase() { admin.encodePassword(); userRepository.save(admin); } + if (!adminRepository.existsById("admin")) { + Admin admin = new Admin(); + admin.setEmail("admin@sample.app"); + admin.setId("admin"); + admin.setPassword(defaultPassword); + admin.encodePassword(); + userRepository.save(admin); + } // Default ShelterVolunteer + if(shelterRepository.findByEmail("shelter@sample.app").isEmpty()) { + Shelter shelter = new Shelter(); + shelter.setName("shelter"); + shelter.setEmail("shelter@sample.app"); + shelter.setMobile("999999990"); + shelter.setActive(true); + shelterRepository.save(shelter); + } + if(shelterRepository.findByEmail("shelter1@sample.app").isEmpty()) { + Shelter shelter = new Shelter(); + shelter.setName("shelter1"); + shelter.setEmail("shelter1@sample.app"); + shelter.setMobile("999999999"); + shelter.setActive(true); + shelterRepository.save(shelter); + if (!shelterVolunteerRepository.existsById("volunteer")) { ShelterVolunteer volunteer = new ShelterVolunteer(); volunteer.setEmail("volunteer@sample.app"); volunteer.setId("volunteer"); volunteer.setPassword(defaultPassword); volunteer.encodePassword(); + volunteer.setUserShelter(shelter); + shelterVolunteerRepository.save(volunteer); + } + if (!shelterVolunteerRepository.existsById("volunteer1")) { + ShelterVolunteer volunteer = new ShelterVolunteer(); + volunteer.setEmail("volunteer1@sample.app"); + volunteer.setId("volunteer1"); + volunteer.setPassword(defaultPassword); + volunteer.setUserShelter(shelter); + volunteer.encodePassword(); shelterVolunteerRepository.save(volunteer); } + if (!shelterVolunteerRepository.existsById("volunteer2")) { + ShelterVolunteer volunteer = new ShelterVolunteer(); + volunteer.setEmail("volunteer2@sample.app"); + 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")) { diff --git a/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java index 82b7dd4..0ad12a2 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java +++ b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java @@ -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 { }