diff --git a/src/main/java/br/com/ages/vouimigrar/CommandLineAppStartupRunner.java b/src/main/java/br/com/ages/vouimigrar/CommandLineAppStartupRunner.java index f28c91dc44c6593a39cfea813146f912629addd3..5daa9895118f0c5fc25d7d1c4f73bf6685f07cb3 100644 --- a/src/main/java/br/com/ages/vouimigrar/CommandLineAppStartupRunner.java +++ b/src/main/java/br/com/ages/vouimigrar/CommandLineAppStartupRunner.java @@ -3,6 +3,7 @@ package br.com.ages.vouimigrar; import br.com.ages.vouimigrar.dto.UserDTO; import br.com.ages.vouimigrar.entity.*; import br.com.ages.vouimigrar.enums.EnumBoolean; +import br.com.ages.vouimigrar.enums.QuestionType; import br.com.ages.vouimigrar.enums.RequirementType; import br.com.ages.vouimigrar.enums.UserType; import br.com.ages.vouimigrar.service.answer.IAnswerService; @@ -100,67 +101,75 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { List<RequirementEntity> requirements = Arrays.asList(requirement1, requirement2, requirement3, requirement4); requirements.forEach(requirementService::save); - QuestionEntity question1 = buildQuestion(requirement1, "Qual é a sua escolaridade?", 1); - QuestionEntity question2 = buildQuestion(null, "Qual é a sua área de formação?", 2); - QuestionEntity question3 = buildQuestion(requirement2, "Qual é o seu nÃvel de conhecimento em inglês?", 3); - QuestionEntity question4 = buildQuestion(null, "Qual é o seu estado civil?", 4); - QuestionEntity question5 = buildQuestion(requirement3, "Qual é a faixa de renda que está disposto a investir?", 5); - QuestionEntity question6 = buildQuestion(requirement4, "Qual é o propósito da viagem?", 6); - QuestionEntity question7 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 7); - QuestionEntity question8 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 8); - QuestionEntity question9 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 9); - QuestionEntity question10 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 10); - QuestionEntity question11 = buildQuestion(null, "Quando você estaria pronto para viajar?", 11); - List<QuestionEntity> questions = Arrays.asList(question1, question2, question3, question4, question5, question6, question7, question8, question9, question10, question11); + QuestionEntity question0 = buildQuestion(null, "Qual a sua idade?", 1, QuestionType.OBJECTIVE); + QuestionEntity question1 = buildQuestion(requirement1, "Qual é a sua escolaridade?", 2, QuestionType.OBJECTIVE); + QuestionEntity question2 = buildQuestion(null, "Qual é a sua área de formação?", 3, QuestionType.ESSAY); + QuestionEntity question3 = buildQuestion(requirement2, "Qual é o seu nÃvel de conhecimento em inglês?", 4, QuestionType.OBJECTIVE); + QuestionEntity question4 = buildQuestion(null, "Qual é o seu estado civil?", 5, QuestionType.OBJECTIVE); + QuestionEntity question5 = buildQuestion(requirement3, "Qual é a faixa de renda que está disposto a investir?", 6, QuestionType.OBJECTIVE); + QuestionEntity question6 = buildQuestion(requirement4, "Qual é o propósito da viagem?", 7, QuestionType.OBJECTIVE); + QuestionEntity question7 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 8, QuestionType.OBJECTIVE); + QuestionEntity question8 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 9, QuestionType.OBJECTIVE); + QuestionEntity question9 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 10, QuestionType.OBJECTIVE); + QuestionEntity question10 = buildQuestion(null, "Com quais alternativas você mais se identifica?", 11, QuestionType.OBJECTIVE); + QuestionEntity question11 = buildQuestion(null, "Quando você estaria pronto para viajar?", 12, QuestionType.OBJECTIVE); + List<QuestionEntity> questions = Arrays.asList(question0, question1, question2, question3, question4, question5, question6, question7, question8, question9, question10, question11); questions.forEach(questionService::save); + + // 0 Qual a sua idade? + AnswerEntity answer00 = buildAnswerEntity(question0, "-17", EnumBoolean.N); + AnswerEntity answer01 = buildAnswerEntity(question0, "18 a 24", EnumBoolean.N); + AnswerEntity answer02 = buildAnswerEntity(question0, "25 a 30", EnumBoolean.N); + AnswerEntity answer03 = buildAnswerEntity(question0, "31 a 40", EnumBoolean.N); + AnswerEntity answer04 = buildAnswerEntity(question0, "40+", EnumBoolean.N); // 1 Qual é a sua escolaridade? AnswerEntity answer1 = buildAnswerEntity(question1, "Ensino médio", EnumBoolean.N); AnswerEntity answer2 = buildAnswerEntity(question1, "Superior Incompleto", EnumBoolean.Y); AnswerEntity answer3 = buildAnswerEntity(question1, "Superior Completo", EnumBoolean.Y); AnswerEntity answer4 = buildAnswerEntity(question1, "Pós Graduação", EnumBoolean.Y); AnswerEntity answer5 = buildAnswerEntity(question1, "Mestrado ou Doutorado", EnumBoolean.Y); - // 3 Qual é o seu nÃvel de conhecimento em inglês? + // 2 Qual é o seu nÃvel de conhecimento em inglês? AnswerEntity answer6 = buildAnswerEntity(question3, "Básico", EnumBoolean.N); AnswerEntity answer7 = buildAnswerEntity(question3, "Intermediário", EnumBoolean.Y); AnswerEntity answer8 = buildAnswerEntity(question3, "Avançado", EnumBoolean.Y); - // 4 Qual é o seu estado civil? + // 3 Qual é o seu estado civil? AnswerEntity answer9 = buildAnswerEntity(question4, "Solteiro", EnumBoolean.Y); AnswerEntity answer10 = buildAnswerEntity(question4, "União Estável", EnumBoolean.Y); AnswerEntity answer11 = buildAnswerEntity(question4, "Casado", EnumBoolean.Y); AnswerEntity answer12 = buildAnswerEntity(question4, "Divorciado", EnumBoolean.Y); AnswerEntity answer13 = buildAnswerEntity(question4, "Viúvo", EnumBoolean.Y); - // 5 Qual é a faixa de renda que está disposto a investir? + // 4 Qual é a faixa de renda que está disposto a investir? AnswerEntity answer14 = buildAnswerEntity(question5, "Até R$ 15.000", EnumBoolean.N); AnswerEntity answer15 = buildAnswerEntity(question5, "Entra 15 e 25 mil", EnumBoolean.Y); AnswerEntity answer16 = buildAnswerEntity(question5, "Entre 25 a 35 mil", EnumBoolean.Y); AnswerEntity answer17 = buildAnswerEntity(question5, "Acima 35mil", EnumBoolean.Y); - // 6 Qual é o propósito da viagem? + // 5 Qual é o propósito da viagem? AnswerEntity answer18 = buildAnswerEntity(question6, "Estudo", EnumBoolean.Y); AnswerEntity answer19 = buildAnswerEntity(question6, "Trabalho", EnumBoolean.Y); AnswerEntity answer20 = buildAnswerEntity(question6, "Tentar a Vida", EnumBoolean.N); - // 7 Com quais alternativas você mais se identifica? + // 6 Com quais alternativas você mais se identifica? AnswerEntity answer21 = buildAnswerEntity(question7, "Metrópole", EnumBoolean.Y); AnswerEntity answer22 = buildAnswerEntity(question7, "Cidade Pequena", EnumBoolean.Y); AnswerEntity answer23 = buildAnswerEntity(question7, "Sem Preferência", EnumBoolean.N); - // 8 Com quais alternativas você mais se identifica? + // 7 Com quais alternativas você mais se identifica? AnswerEntity answer24 = buildAnswerEntity(question8, "Frio", EnumBoolean.Y); AnswerEntity answer25 = buildAnswerEntity(question8, "Calor", EnumBoolean.N); AnswerEntity answer26 = buildAnswerEntity(question8, "Sem Preferência", EnumBoolean.N); - // 9 Com quais alternativas você mais se identifica? + // 8 Com quais alternativas você mais se identifica? AnswerEntity answer27 = buildAnswerEntity(question9, "Praia", EnumBoolean.N); AnswerEntity answer28 = buildAnswerEntity(question9, "Montanha", EnumBoolean.Y); AnswerEntity answer29 = buildAnswerEntity(question9, "Sem Preferência", EnumBoolean.N); - // 10 Com quais alternativas você mais se identifica? + // 9 Com quais alternativas você mais se identifica? AnswerEntity answer30 = buildAnswerEntity(question10, "Agito", EnumBoolean.Y); AnswerEntity answer31 = buildAnswerEntity(question10, "Calmaria", EnumBoolean.Y); AnswerEntity answer32 = buildAnswerEntity(question10, "Sem Preferência", EnumBoolean.N); - // 11 Quando você estaria pronto para viajar? + // 10 Quando você estaria pronto para viajar? AnswerEntity answer33 = buildAnswerEntity(question11, "Em 6 meses", EnumBoolean.Y); AnswerEntity answer34 = buildAnswerEntity(question11, "Em 1 ano", EnumBoolean.Y); AnswerEntity answer35 = buildAnswerEntity(question11, "Em 1 ano e meio", EnumBoolean.Y); AnswerEntity answer36 = buildAnswerEntity(question11, "Após 2 anos", EnumBoolean.Y); - List<AnswerEntity> answers = Arrays.asList(answer1, answer2, answer3, answer4, answer5, answer6, + List<AnswerEntity> answers = Arrays.asList(answer00, answer01, answer02, answer03, answer04, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10, answer11, answer12, answer13, answer14, answer15, answer16, answer17, answer18, answer19, answer20, answer21, answer22, answer23, answer24, answer25, answer26, answer27, answer28, answer29, answer30, answer31, answer32, answer33, answer34, answer35, answer36); @@ -186,7 +195,7 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { "Para quem gosta de metrópoles, está aà outro porque de conhecer o Canadá! " + "Cidades como Toronto, Montreal e Vancouver, são limpas, seguras, bonitas, amigáveis e multiculturais! " + "Não é à toa que elas estão sempre nas listas de melhores lugares do mundo para viver!", - ""); + "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/1/logo/canada.png"); countryService.save(country); CountryInfoEntity countryInfo1 = buildCountryInfoEntity(country, "", "Clima", @@ -204,6 +213,13 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { countryInfo5, countryInfo6, countryInfo7, countryInfo8); countryInfos.forEach(countryInfoService::save); + CountryPhotosEntity countryPhotos1 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/1/canada1.jpg"); + CountryPhotosEntity countryPhotos2 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/1/canada2.jpg"); + CountryPhotosEntity countryPhotos3 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/1/canada3.jpg"); + CountryPhotosEntity countryPhotos4 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/1/canada4.jpg"); + List<CountryPhotosEntity> countryPhotos = Arrays.asList(countryPhotos1, countryPhotos2, countryPhotos3, countryPhotos4); + countryPhotos.forEach(countryPhotosService::save); + SocialGroupEntity socialGroup1 = buildSocialGroupEntity(country, "Facebook", "facebook.com"); SocialGroupEntity socialGroup2 = buildSocialGroupEntity(country, "Instagram", "instagram.com"); List<SocialGroupEntity> socialGroups = Arrays.asList(socialGroup1, socialGroup2); @@ -222,8 +238,19 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { countryRequirement2, countryRequirement3, countryRequirement4); countryRequirements.forEach(countryRequirementService::save); - // 1 Qual é a sua escolaridade? int answerIndex = 0; + // 0 Qual a sua idade + WeightCountryAnswerEntity weightCountryAnswer00 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 0L); + WeightCountryAnswerEntity weightCountryAnswer01 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 100L); + WeightCountryAnswerEntity weightCountryAnswer02 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 100L); + WeightCountryAnswerEntity weightCountryAnswer03 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 75L); + WeightCountryAnswerEntity weightCountryAnswer04 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 25L); + // 1 Qual é a sua escolaridade? WeightCountryAnswerEntity weightCountryAnswer1 = buildWeightCountryAnswerEntity( answers.get(answerIndex++), country, 0L); WeightCountryAnswerEntity weightCountryAnswer2 = buildWeightCountryAnswerEntity( @@ -307,6 +334,8 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { answers.get(answerIndex++), country, 0L); List<WeightCountryAnswerEntity> weightsCountryAnswers = Arrays.asList( + weightCountryAnswer00, weightCountryAnswer01, weightCountryAnswer02, + weightCountryAnswer03, weightCountryAnswer04, weightCountryAnswer1, weightCountryAnswer2, weightCountryAnswer3, weightCountryAnswer4, weightCountryAnswer5, weightCountryAnswer6, weightCountryAnswer7, weightCountryAnswer8, weightCountryAnswer9, @@ -324,13 +353,22 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { } private CountryEntity createAustralia(List<RequirementEntity> requirements, List<AnswerEntity> answers) { - CountryEntity country = buildCountry("Austrália", "", ""); + CountryEntity country = buildCountry("Austrália", "Por causa das belezas naturais. Além de ser um paÃs incrÃvel por conta de suas cidades e qualidade de vida," + + " a Austrália ainda possui uma natureza muito rica. São florestas, montanhas, praias paradisÃacas, cenários de tirar o fôlego e até um deserto no meio paÃs.", + "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/3/logo/australia.png"); countryService.save(country); CountryInfoEntity countryInfo1 = buildCountryInfoEntity(country, "", "Clima", ""); List<CountryInfoEntity> countryInfos = Arrays.asList(countryInfo1); countryInfos.forEach(countryInfoService::save); + CountryPhotosEntity countryPhotos1 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/2/australia1.png"); + CountryPhotosEntity countryPhotos2 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/2/australia2.jpg"); + CountryPhotosEntity countryPhotos3 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/2/australia3.jpg"); + CountryPhotosEntity countryPhotos4 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/2/australia4.jpg"); + List<CountryPhotosEntity> countryPhotos = Arrays.asList(countryPhotos1, countryPhotos2, countryPhotos3, countryPhotos4); + countryPhotos.forEach(countryPhotosService::save); + SocialGroupEntity socialGroup1 = buildSocialGroupEntity(country, "Facebook", "facebook.com"); SocialGroupEntity socialGroup2 = buildSocialGroupEntity(country, "Instagram", "instagram.com"); List<SocialGroupEntity> socialGroups = Arrays.asList(socialGroup1, socialGroup2); @@ -349,8 +387,19 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { countryRequirement2, countryRequirement3, countryRequirement4); countryRequirements.forEach(countryRequirementService::save); - // 1 Qual é a sua escolaridade? int answerIndex = 0; + // 0 Qual a sua idade + WeightCountryAnswerEntity weightCountryAnswer00 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 0L); + WeightCountryAnswerEntity weightCountryAnswer01 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 100L); + WeightCountryAnswerEntity weightCountryAnswer02 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 100L); + WeightCountryAnswerEntity weightCountryAnswer03 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 50L); + WeightCountryAnswerEntity weightCountryAnswer04 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 25L); + // 1 Qual é a sua escolaridade? WeightCountryAnswerEntity weightCountryAnswer1 = buildWeightCountryAnswerEntity( answers.get(answerIndex++), country, 0L); WeightCountryAnswerEntity weightCountryAnswer2 = buildWeightCountryAnswerEntity( @@ -434,6 +483,8 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { answers.get(answerIndex++), country, 50L); List<WeightCountryAnswerEntity> weightsCountryAnswers = Arrays.asList( + weightCountryAnswer00, weightCountryAnswer01, weightCountryAnswer02, + weightCountryAnswer03, weightCountryAnswer04, weightCountryAnswer1, weightCountryAnswer2, weightCountryAnswer3, weightCountryAnswer4, weightCountryAnswer5, weightCountryAnswer6, weightCountryAnswer7, weightCountryAnswer8, weightCountryAnswer9, @@ -453,7 +504,7 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { private CountryEntity createNewZealand(List<RequirementEntity> requirements, List<AnswerEntity> answers) { CountryEntity country = buildCountry("Nova Zelândia", "São inúmeros os motivos para visitar a Nova Zelândia: paisagens cinematográficas, natureza exuberante, povo receptivo com rica cultura tradicional, capital dos esportes radicais e, ainda, cenário de filmes como O Senhor dos Anéis.", - ""); + "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/3/logo/new-zealand.jpg"); countryService.save(country); CountryInfoEntity countryInfo1 = buildCountryInfoEntity(country, "", "Clima", @@ -469,6 +520,13 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { countryInfo5, countryInfo6, countryInfo7, countryInfo8); countryInfos.forEach(countryInfoService::save); + CountryPhotosEntity countryPhotos1 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/3/new-zealand1.jpg"); + CountryPhotosEntity countryPhotos2 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/3/new-zealand2.jpg"); + CountryPhotosEntity countryPhotos3 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/3/new-zealand3.jpg"); + CountryPhotosEntity countryPhotos4 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/3/new-zealand4.jpg"); + List<CountryPhotosEntity> countryPhotos = Arrays.asList(countryPhotos1, countryPhotos2, countryPhotos3, countryPhotos4); + countryPhotos.forEach(countryPhotosService::save); + SocialGroupEntity socialGroup1 = buildSocialGroupEntity(country, "Facebook", "facebook.com"); SocialGroupEntity socialGroup2 = buildSocialGroupEntity(country, "Instagram", "instagram.com"); List<SocialGroupEntity> socialGroups = Arrays.asList(socialGroup1, socialGroup2); @@ -487,8 +545,19 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { countryRequirement2, countryRequirement3, countryRequirement4); countryRequirements.forEach(countryRequirementService::save); - // 1 Qual é a sua escolaridade? int answerIndex = 0; + // 0 Qual a sua idade + WeightCountryAnswerEntity weightCountryAnswer00 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 0L); + WeightCountryAnswerEntity weightCountryAnswer01 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 75L); + WeightCountryAnswerEntity weightCountryAnswer02 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 100L); + WeightCountryAnswerEntity weightCountryAnswer03 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 50L); + WeightCountryAnswerEntity weightCountryAnswer04 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 25L); + // 1 Qual é a sua escolaridade? WeightCountryAnswerEntity weightCountryAnswer1 = buildWeightCountryAnswerEntity( answers.get(answerIndex++), country, 0L); WeightCountryAnswerEntity weightCountryAnswer2 = buildWeightCountryAnswerEntity( @@ -571,6 +640,8 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { WeightCountryAnswerEntity weightCountryAnswer36 = buildWeightCountryAnswerEntity( answers.get(answerIndex++), country, 50L); List<WeightCountryAnswerEntity> weightsCountryAnswers = Arrays.asList( + weightCountryAnswer00, weightCountryAnswer01, weightCountryAnswer02, + weightCountryAnswer03, weightCountryAnswer04, weightCountryAnswer1, weightCountryAnswer2, weightCountryAnswer3, weightCountryAnswer4, weightCountryAnswer5, weightCountryAnswer6, weightCountryAnswer7, weightCountryAnswer8, weightCountryAnswer9, @@ -590,7 +661,7 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { private CountryEntity createPortugal(List<RequirementEntity> requirements, List<AnswerEntity> answers) { CountryEntity country = buildCountry("Portugal", "Portugal é um destino de viagem maravilhoso, especialmente para os brasileiros. Ao desembarcar por lá, temos a sensação de estar em casa. Você vai encontrar um local rico em cultura, boa gastronomia, ótimos vinhos e anfitriões amáveis.", - ""); + "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/4/logo/portugal.png"); countryService.save(country); CountryInfoEntity countryInfo1 = buildCountryInfoEntity(country, "", "Clima", @@ -606,6 +677,13 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { countryInfo5, countryInfo6, countryInfo7, countryInfo8); countryInfos.forEach(countryInfoService::save); + CountryPhotosEntity countryPhotos1 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/4/portugal1.jpg"); + CountryPhotosEntity countryPhotos2 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/4/portugal2.jpg"); + CountryPhotosEntity countryPhotos3 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/4/portugal3.jpg"); + CountryPhotosEntity countryPhotos4 = buildCountryPhoto(country, "https://vouimigrar.s3.us-east-2.amazonaws.com/countries/4/portugal4.jpg"); + List<CountryPhotosEntity> countryPhotos = Arrays.asList(countryPhotos1, countryPhotos2, countryPhotos3, countryPhotos4); + countryPhotos.forEach(countryPhotosService::save); + SocialGroupEntity socialGroup1 = buildSocialGroupEntity(country, "Facebook", "facebook.com"); SocialGroupEntity socialGroup2 = buildSocialGroupEntity(country, "Instagram", "instagram.com"); List<SocialGroupEntity> socialGroups = Arrays.asList(socialGroup1, socialGroup2); @@ -624,8 +702,19 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { countryRequirement2, countryRequirement3, countryRequirement4); countryRequirements.forEach(countryRequirementService::save); - // 1 Qual é a sua escolaridade? int answerIndex = 0; + // 0 Qual a sua idade + WeightCountryAnswerEntity weightCountryAnswer00 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 0L); + WeightCountryAnswerEntity weightCountryAnswer01 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 10L); + WeightCountryAnswerEntity weightCountryAnswer02 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 100L); + WeightCountryAnswerEntity weightCountryAnswer03 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 50L); + WeightCountryAnswerEntity weightCountryAnswer04 = buildWeightCountryAnswerEntity( + answers.get(answerIndex++), country, 50L); + // 1 Qual é a sua escolaridade? WeightCountryAnswerEntity weightCountryAnswer1 = buildWeightCountryAnswerEntity( answers.get(answerIndex++), country, 0L); WeightCountryAnswerEntity weightCountryAnswer2 = buildWeightCountryAnswerEntity( @@ -708,6 +797,8 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { WeightCountryAnswerEntity weightCountryAnswer36 = buildWeightCountryAnswerEntity( answers.get(answerIndex++), country, 25L); List<WeightCountryAnswerEntity> weightsCountryAnswers = Arrays.asList( + weightCountryAnswer00, weightCountryAnswer01, weightCountryAnswer02, + weightCountryAnswer03, weightCountryAnswer04, weightCountryAnswer1, weightCountryAnswer2, weightCountryAnswer3, weightCountryAnswer4, weightCountryAnswer5, weightCountryAnswer6, weightCountryAnswer7, weightCountryAnswer8, weightCountryAnswer9, @@ -763,9 +854,10 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { .build(); } - private QuestionEntity buildQuestion(RequirementEntity requirement1, String description, int position) { + private QuestionEntity buildQuestion(RequirementEntity requirement1, String description, int position, QuestionType questionType) { return QuestionEntity.builder() .description(description) + .questionType(questionType) .position(position) .requirement(requirement1) .build(); @@ -798,6 +890,13 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { .build(); } + private CountryPhotosEntity buildCountryPhoto(CountryEntity country, String url){ + return CountryPhotosEntity.builder() + .country(country) + .url(url) + .build(); + } + private SocialGroupEntity buildSocialGroupEntity(CountryEntity country, String name, String link) { return SocialGroupEntity.builder() .name(name) @@ -824,7 +923,7 @@ public class CommandLineAppStartupRunner implements CommandLineRunner { .build(); } - private MatchEntity buildMatchEntity(UserEntity user, CountryEntity country, long matchPercentage) { + private MatchEntity buildMatchEntity(UserEntity user, CountryEntity country, Long matchPercentage) { return MatchEntity.builder() .user(user) .country(country) diff --git a/src/main/java/br/com/ages/vouimigrar/controller/AnswerController.java b/src/main/java/br/com/ages/vouimigrar/controller/AnswerController.java new file mode 100644 index 0000000000000000000000000000000000000000..f91f6a6165a83d6e35677c1ae023fc27a349f74c --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/controller/AnswerController.java @@ -0,0 +1,33 @@ +package br.com.ages.vouimigrar.controller; + +import br.com.ages.vouimigrar.dto.AnswerResponseDTO; +import br.com.ages.vouimigrar.entity.AnswerEntity; +import br.com.ages.vouimigrar.service.answer.IAnswerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/answers") +public class AnswerController { + private final IAnswerService service; + + @Autowired + public AnswerController(IAnswerService service){ + this.service = service; + } + + @GetMapping + public List<AnswerResponseDTO> findAll(){ + return this.service.findAll(); + } + + @GetMapping("/findById/{id}") + public AnswerEntity findById(@PathVariable Long id){ + return this.service.findById(id); + } +} diff --git a/src/main/java/br/com/ages/vouimigrar/controller/MatchController.java b/src/main/java/br/com/ages/vouimigrar/controller/MatchController.java index e535058da8c97f5b48f9a8de3ebc0b274f510ba5..56e7ec9cb2ab91eeda2dd4c8a3ce73fcdc9adab0 100644 --- a/src/main/java/br/com/ages/vouimigrar/controller/MatchController.java +++ b/src/main/java/br/com/ages/vouimigrar/controller/MatchController.java @@ -1,18 +1,14 @@ package br.com.ages.vouimigrar.controller; -import br.com.ages.vouimigrar.dto.MatchDTO; +import br.com.ages.vouimigrar.dto.MatchResponseDTO; +import br.com.ages.vouimigrar.dto.UserAnswerDTO; import br.com.ages.vouimigrar.service.match.IMatchService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import br.com.ages.vouimigrar.entity.MatchEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; import java.util.List; - @RestController @RequestMapping("/matches") public class MatchController { @@ -25,7 +21,16 @@ public class MatchController { } @GetMapping("/findByUser") - public List<MatchEntity> findByUser() { + public List<MatchResponseDTO> findByUser() { return this.service.findByUser(); } + + @PostMapping("/calculate-match") + public ResponseEntity calculateMatchPercentage(@RequestBody List<UserAnswerDTO> userAnswers) { + this.service.calculateMatchPercentage(userAnswers); + return ResponseEntity.status(201).build(); + } + + @GetMapping("/find-all") + public List<MatchResponseDTO> findAll(){return this.service.findAll();} } \ No newline at end of file diff --git a/src/main/java/br/com/ages/vouimigrar/controller/QuestionController.java b/src/main/java/br/com/ages/vouimigrar/controller/QuestionController.java new file mode 100644 index 0000000000000000000000000000000000000000..28b7987331db4d2344aecb2d0269197420a05945 --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/controller/QuestionController.java @@ -0,0 +1,33 @@ +package br.com.ages.vouimigrar.controller; + +import br.com.ages.vouimigrar.dto.QuestionResponseDTO; +import br.com.ages.vouimigrar.entity.QuestionEntity; +import br.com.ages.vouimigrar.service.question.IQuestionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/questions") +public class QuestionController { + private final IQuestionService service; + + @Autowired + public QuestionController(IQuestionService service){ + this.service = service; + } + + @GetMapping + public List<QuestionResponseDTO> findAll(){ + return this.service.findAll(); + } + + @GetMapping("/findById/{id}") + public QuestionEntity findById(@PathVariable Long id){ + return this.service.findById(id); + } +} diff --git a/src/main/java/br/com/ages/vouimigrar/controller/UserAnswerController.java b/src/main/java/br/com/ages/vouimigrar/controller/UserAnswerController.java new file mode 100644 index 0000000000000000000000000000000000000000..f5dc7daa83e51b4cbfefbfbd42201ffb7d4428de --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/controller/UserAnswerController.java @@ -0,0 +1,27 @@ +package br.com.ages.vouimigrar.controller; + +import br.com.ages.vouimigrar.entity.UserAnswerEntity; +import br.com.ages.vouimigrar.service.useranswer.IUserAnswerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/user-answers") +public class UserAnswerController { + + private final IUserAnswerService service; + + @Autowired + public UserAnswerController(IUserAnswerService service) { + this.service = service; + } + + @GetMapping("/find-by-user") + public List<UserAnswerEntity> findAllByUserId(){ + return this.service.findAllByLoggedUser(); + } +} diff --git a/src/main/java/br/com/ages/vouimigrar/dto/AnswerResponseDTO.java b/src/main/java/br/com/ages/vouimigrar/dto/AnswerResponseDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..88232ba868ccf0ebe8b87361eed9fbc8d525ca92 --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/dto/AnswerResponseDTO.java @@ -0,0 +1,17 @@ +package br.com.ages.vouimigrar.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_EMPTY) +public class AnswerResponseDTO { + private Long id; + private String description; + private Long questionId; +} diff --git a/src/main/java/br/com/ages/vouimigrar/dto/CountryDTO.java b/src/main/java/br/com/ages/vouimigrar/dto/CountryDTO.java index d914b75a90c2e373ecfdcb2c89a99c536fea6a02..b45bd913d91354f5847226b0753e617a15b85981 100644 --- a/src/main/java/br/com/ages/vouimigrar/dto/CountryDTO.java +++ b/src/main/java/br/com/ages/vouimigrar/dto/CountryDTO.java @@ -18,6 +18,7 @@ public class CountryDTO { private long id; private String name; private String description; + private String image; private List<CountryInfoDTO> infos; private List<CountryPhotosDTO> photos; } diff --git a/src/main/java/br/com/ages/vouimigrar/dto/MatchDTO.java b/src/main/java/br/com/ages/vouimigrar/dto/CountryResponseDTO.java similarity index 65% rename from src/main/java/br/com/ages/vouimigrar/dto/MatchDTO.java rename to src/main/java/br/com/ages/vouimigrar/dto/CountryResponseDTO.java index 3bd623deded5718c82c96f0690692731e494b360..a54176e50b1cdc7af018a55ac3a0853701ff6fb3 100644 --- a/src/main/java/br/com/ages/vouimigrar/dto/MatchDTO.java +++ b/src/main/java/br/com/ages/vouimigrar/dto/CountryResponseDTO.java @@ -1,6 +1,5 @@ package br.com.ages.vouimigrar.dto; - import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -10,8 +9,9 @@ import lombok.NoArgsConstructor; @Builder @NoArgsConstructor @AllArgsConstructor -public class MatchDTO { - private Long matchPercentage; +public class CountryResponseDTO { private Long id; - private Long countryId; -} \ No newline at end of file + private String name; + private String description; + private String image; +} diff --git a/src/main/java/br/com/ages/vouimigrar/dto/MatchResponseDTO.java b/src/main/java/br/com/ages/vouimigrar/dto/MatchResponseDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..7e04c85a09c38fa5474bb6aede95de73e03d850b --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/dto/MatchResponseDTO.java @@ -0,0 +1,18 @@ +package br.com.ages.vouimigrar.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_EMPTY) +public class MatchResponseDTO { + private Long id; + private Long matchPercentage; + private CountryResponseDTO country; +} diff --git a/src/main/java/br/com/ages/vouimigrar/dto/QuestionResponseDTO.java b/src/main/java/br/com/ages/vouimigrar/dto/QuestionResponseDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..d410f29a9667dbf4f5f0d7d4f7b24aca2d65f19c --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/dto/QuestionResponseDTO.java @@ -0,0 +1,24 @@ +package br.com.ages.vouimigrar.dto; + +import br.com.ages.vouimigrar.enums.QuestionType; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_EMPTY) +public class QuestionResponseDTO { + private Long id; + private String description; + private Integer position; + private Long requirementId; + private QuestionType questionType; + private List<AnswerResponseDTO> answers; +} diff --git a/src/main/java/br/com/ages/vouimigrar/dto/UserAnswerDTO.java b/src/main/java/br/com/ages/vouimigrar/dto/UserAnswerDTO.java new file mode 100644 index 0000000000000000000000000000000000000000..ef6b787f141b1c564906b438c188842f7efc26bd --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/dto/UserAnswerDTO.java @@ -0,0 +1,17 @@ +package br.com.ages.vouimigrar.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_EMPTY) +public class UserAnswerDTO { + private Long questionId; + private Long answerId; +} diff --git a/src/main/java/br/com/ages/vouimigrar/dto/UserDTO.java b/src/main/java/br/com/ages/vouimigrar/dto/UserDTO.java index 92be2e651875b10141b5d84275e32c295e382ee0..7cea12441f56c45c73ef054b9f8f9d2d4029ae62 100644 --- a/src/main/java/br/com/ages/vouimigrar/dto/UserDTO.java +++ b/src/main/java/br/com/ages/vouimigrar/dto/UserDTO.java @@ -1,6 +1,7 @@ package br.com.ages.vouimigrar.dto; import br.com.ages.vouimigrar.enums.UserType; +import com.fasterxml.jackson.annotation.JsonInclude; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -10,6 +11,7 @@ import lombok.NoArgsConstructor; @Builder @NoArgsConstructor @AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_EMPTY) public class UserDTO { private Long id; private String email; diff --git a/src/main/java/br/com/ages/vouimigrar/entity/QuestionEntity.java b/src/main/java/br/com/ages/vouimigrar/entity/QuestionEntity.java index 95e6ee4d290d7e20c1c41c9e1815580aacf3ea4e..0383b23a1b49572074c857561050230b04f91c21 100644 --- a/src/main/java/br/com/ages/vouimigrar/entity/QuestionEntity.java +++ b/src/main/java/br/com/ages/vouimigrar/entity/QuestionEntity.java @@ -1,5 +1,6 @@ package br.com.ages.vouimigrar.entity; +import br.com.ages.vouimigrar.enums.QuestionType; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -29,4 +30,8 @@ public class QuestionEntity { @ManyToOne @JoinColumn(name = "requirement_id") private RequirementEntity requirement; + + @Enumerated + @Column(name = "question_type") + private QuestionType questionType; } diff --git a/src/main/java/br/com/ages/vouimigrar/entity/UserEntity.java b/src/main/java/br/com/ages/vouimigrar/entity/UserEntity.java index 7d3836134a57fd553d0566c8cabd42685c050591..898900f195c10b03095c3cb55d6d685ec25819b2 100644 --- a/src/main/java/br/com/ages/vouimigrar/entity/UserEntity.java +++ b/src/main/java/br/com/ages/vouimigrar/entity/UserEntity.java @@ -4,7 +4,6 @@ import br.com.ages.vouimigrar.enums.UserType; import lombok.*; import org.hibernate.Hibernate; import org.springframework.lang.NonNull; -import org.springframework.stereotype.Component; import javax.persistence.*; import java.time.LocalDate; diff --git a/src/main/java/br/com/ages/vouimigrar/enums/QuestionType.java b/src/main/java/br/com/ages/vouimigrar/enums/QuestionType.java new file mode 100644 index 0000000000000000000000000000000000000000..6289adb31ff4cece5c2b6fb616dd95ff5610445b --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/enums/QuestionType.java @@ -0,0 +1,6 @@ +package br.com.ages.vouimigrar.enums; + +public enum QuestionType { + OBJECTIVE, + ESSAY +} diff --git a/src/main/java/br/com/ages/vouimigrar/message/Message.java b/src/main/java/br/com/ages/vouimigrar/message/Message.java index 476c7bdf73bccfb706c36952169927a7b333b10a..ba1f6492e8eb3c755ee98ff6d93e426820b6d327 100644 --- a/src/main/java/br/com/ages/vouimigrar/message/Message.java +++ b/src/main/java/br/com/ages/vouimigrar/message/Message.java @@ -7,7 +7,6 @@ public class Message { public static final String SENHA_OBRIGATORIO = "Senha é obrigatório."; public static final String EMAIL_OBRIGATORIO = "Email é obrigatório."; public static final String EMAIL_EXISTENTE = "Email já esta em uso."; - public static final String USUARIO_NAO_ENCONTRADO = "O usuário não foi encontrado."; public static final String EMAIL_INVALIDO = "O formato do email é inválido."; public static final String DATA_INVALIDA = "A data de nascimento é inválida."; public static final String TELEFONE_INVALIDO = "O formato do telefone é inválido."; diff --git a/src/main/java/br/com/ages/vouimigrar/repository/IAnswerRepository.java b/src/main/java/br/com/ages/vouimigrar/repository/IAnswerRepository.java index cd3da585c98b76c5578939b673c048f3f5fc2b1e..d09efb897550652250fe1a4aff66ef7712a93844 100644 --- a/src/main/java/br/com/ages/vouimigrar/repository/IAnswerRepository.java +++ b/src/main/java/br/com/ages/vouimigrar/repository/IAnswerRepository.java @@ -4,6 +4,10 @@ import br.com.ages.vouimigrar.entity.AnswerEntity; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface IAnswerRepository extends CrudRepository<AnswerEntity, Long> { + List<AnswerEntity> findAll(); + List<AnswerEntity> findAllByQuestionId(Long questionId); } diff --git a/src/main/java/br/com/ages/vouimigrar/repository/ICountryRepository.java b/src/main/java/br/com/ages/vouimigrar/repository/ICountryRepository.java index aef3f49cd5e4083036b8d103a1cd802bd6bd2f5f..a74e83b7074a13b332fd24a22380b17fb7afcacd 100644 --- a/src/main/java/br/com/ages/vouimigrar/repository/ICountryRepository.java +++ b/src/main/java/br/com/ages/vouimigrar/repository/ICountryRepository.java @@ -10,7 +10,6 @@ import java.util.Optional; @Repository public interface ICountryRepository extends CrudRepository<CountryEntity, Long>{ List<CountryEntity> findAll(); - Optional<CountryEntity> findAllById(Long id); } diff --git a/src/main/java/br/com/ages/vouimigrar/repository/IMatchRepository.java b/src/main/java/br/com/ages/vouimigrar/repository/IMatchRepository.java index 369bb4247167a8e60a95abe0ce55c76bb7b3ae11..0cd9e4bee9514dabdc6f15239f81d00a42c170da 100644 --- a/src/main/java/br/com/ages/vouimigrar/repository/IMatchRepository.java +++ b/src/main/java/br/com/ages/vouimigrar/repository/IMatchRepository.java @@ -11,4 +11,6 @@ import java.util.List; @Repository public interface IMatchRepository extends CrudRepository<MatchEntity, Long> { List<MatchEntity> findByUser(UserEntity user, Sort sort); + void deleteAllByUserUserId(Long userId); + List<MatchEntity> findAll(); } \ No newline at end of file diff --git a/src/main/java/br/com/ages/vouimigrar/repository/IQuestionRepository.java b/src/main/java/br/com/ages/vouimigrar/repository/IQuestionRepository.java index 908a7efed69c97b69a116b039dbd2fcc00997198..8b54182ed7c5c1607e56befcfb1cf7e70f1cf703 100644 --- a/src/main/java/br/com/ages/vouimigrar/repository/IQuestionRepository.java +++ b/src/main/java/br/com/ages/vouimigrar/repository/IQuestionRepository.java @@ -1,9 +1,15 @@ package br.com.ages.vouimigrar.repository; import br.com.ages.vouimigrar.entity.QuestionEntity; +import org.springframework.data.domain.Sort; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface IQuestionRepository extends CrudRepository<QuestionEntity, Long> { + List<QuestionEntity> findAll(Sort sort); + Optional<QuestionEntity> findById(Long id); } diff --git a/src/main/java/br/com/ages/vouimigrar/repository/IUserAnswerRepository.java b/src/main/java/br/com/ages/vouimigrar/repository/IUserAnswerRepository.java index eae3ea6aa373885ee7318dd190e3b58085d7c773..c37fdba106ff59d38aa92f425a30a3bcf2990359 100644 --- a/src/main/java/br/com/ages/vouimigrar/repository/IUserAnswerRepository.java +++ b/src/main/java/br/com/ages/vouimigrar/repository/IUserAnswerRepository.java @@ -3,5 +3,9 @@ package br.com.ages.vouimigrar.repository; import br.com.ages.vouimigrar.entity.UserAnswerEntity; import org.springframework.data.repository.CrudRepository; +import java.util.List; + public interface IUserAnswerRepository extends CrudRepository<UserAnswerEntity, Long> { + List<UserAnswerEntity> findUserAnswerEntitiesByUserUserId(Long userId); + void deleteAllByUserUserId(Long userId); } diff --git a/src/main/java/br/com/ages/vouimigrar/repository/IWeightCountryAnswerRepository.java b/src/main/java/br/com/ages/vouimigrar/repository/IWeightCountryAnswerRepository.java index 291f28e9af49fe2b1a69a55b1a763cdeaa2d590e..0564e5ff46ed34533428345ed516755867c636b3 100644 --- a/src/main/java/br/com/ages/vouimigrar/repository/IWeightCountryAnswerRepository.java +++ b/src/main/java/br/com/ages/vouimigrar/repository/IWeightCountryAnswerRepository.java @@ -6,4 +6,5 @@ import org.springframework.stereotype.Repository; @Repository public interface IWeightCountryAnswerRepository extends CrudRepository<WeightCountryAnswerEntity, Long> { + WeightCountryAnswerEntity findAllByAnswerIdAndCountryId(Long answerId, Long countryId); } diff --git a/src/main/java/br/com/ages/vouimigrar/security/TokenAuthenticationService.java b/src/main/java/br/com/ages/vouimigrar/security/TokenAuthenticationService.java index 1cb085e0c47df643e983ed7374e6fbe0c161a611..99d92379bc571fd63d6ff5363a7c6357888f59ee 100644 --- a/src/main/java/br/com/ages/vouimigrar/security/TokenAuthenticationService.java +++ b/src/main/java/br/com/ages/vouimigrar/security/TokenAuthenticationService.java @@ -40,16 +40,16 @@ public class TokenAuthenticationService { .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME)) .signWith(SignatureAlgorithm.HS512, SECRET) .compact(); - response.addHeader(HEADER_STRING, TOKEN_PREFIX + " " + JWT); JSONObject loginJson = new JSONObject(); - loginJson.put("authorization",TOKEN_PREFIX + JWT); + loginJson.put("authorization",TOKEN_PREFIX + " " + JWT); loginJson.put("id", userEntity.getUserId()); loginJson.put("type", userEntity.getUserType()); loginJson.put("email", userEntity.getEmail()); loginJson.put("name", userEntity.getName()); loginJson.put("phone", userEntity.getPhone()); + response.addHeader(HEADER_STRING, TOKEN_PREFIX + " " + JWT); response.setContentType(APPLICATION_JSON_VALUE); response.setCharacterEncoding("UTF-8"); response.getWriter().write(loginJson.toString()); diff --git a/src/main/java/br/com/ages/vouimigrar/security/WebSecurityConfig.java b/src/main/java/br/com/ages/vouimigrar/security/WebSecurityConfig.java index 94de90d454688f9a80d933e032c5cc1e72e92352..548bd87b8b307a25f6998000224d65e4350de8c5 100644 --- a/src/main/java/br/com/ages/vouimigrar/security/WebSecurityConfig.java +++ b/src/main/java/br/com/ages/vouimigrar/security/WebSecurityConfig.java @@ -1,7 +1,5 @@ package br.com.ages.vouimigrar.security; -import br.com.ages.vouimigrar.enums.UserType; -import br.com.ages.vouimigrar.service.user.IUserService; import br.com.ages.vouimigrar.service.user.UserServiceImpl; import br.com.ages.vouimigrar.util.MD5PasswordEncoder; import lombok.RequiredArgsConstructor; @@ -10,7 +8,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @@ -54,6 +51,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { http.authorizeRequests().antMatchers(HttpMethod.POST,"/h2/**").access("hasAnyRole('ADMIN')"); http.authorizeRequests().antMatchers(HttpMethod.GET,"/countries/**").permitAll(); http.authorizeRequests().antMatchers(HttpMethod.GET,"/matches/**").permitAll(); + http.authorizeRequests().antMatchers(HttpMethod.GET,"/answers/**").permitAll(); + http.authorizeRequests().antMatchers(HttpMethod.POST,"/user-answers/**").permitAll(); + //http.authorizeRequests().antMatchers(HttpMethod.GET,"/user-answers/**").permitAll(); + http.authorizeRequests().antMatchers(HttpMethod.GET,"/questions/**").permitAll(); http.authorizeRequests().antMatchers(HttpMethod.GET, "/", "/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/security", "/swagger-ui.html", "/webjars/**").permitAll(); http.addFilter(authenticationFilter); diff --git a/src/main/java/br/com/ages/vouimigrar/service/answer/AnswerServiceImpl.java b/src/main/java/br/com/ages/vouimigrar/service/answer/AnswerServiceImpl.java index 9290373a736393108edf22d021b978a973f63aac..4db678cff958f8a5dec3fef8e594298c2d9d6ab6 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/answer/AnswerServiceImpl.java +++ b/src/main/java/br/com/ages/vouimigrar/service/answer/AnswerServiceImpl.java @@ -1,10 +1,15 @@ package br.com.ages.vouimigrar.service.answer; +import br.com.ages.vouimigrar.dto.AnswerResponseDTO; import br.com.ages.vouimigrar.entity.AnswerEntity; import br.com.ages.vouimigrar.repository.IAnswerRepository; +import br.com.ages.vouimigrar.util.exceptions.AnswerNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + @Service public class AnswerServiceImpl implements IAnswerService { private final IAnswerRepository repository; @@ -18,4 +23,29 @@ public class AnswerServiceImpl implements IAnswerService { public AnswerEntity save(AnswerEntity answer) { return this.repository.save(answer); } + + @Override + public AnswerEntity findById(long id){ + return this.repository.findById(id).orElseThrow(AnswerNotFoundException::new); + } + + @Override + public List<AnswerResponseDTO> findAll(){ + List<AnswerEntity> answers = this.repository.findAll(); + List<AnswerResponseDTO> answersDTO = new ArrayList<>(); + for(AnswerEntity answer : answers){ + AnswerResponseDTO answerDTO = AnswerResponseDTO.builder() + .id(answer.getId()) + .description(answer.getDescription()) + .questionId(answer.getQuestion().getId()) + .build(); + answersDTO.add(answerDTO); + } + return answersDTO; + } + + @Override + public List<AnswerEntity> findAllByQuestionId(Long questionId) { + return this.repository.findAllByQuestionId(questionId); + } } diff --git a/src/main/java/br/com/ages/vouimigrar/service/answer/IAnswerService.java b/src/main/java/br/com/ages/vouimigrar/service/answer/IAnswerService.java index 98516c0cd25a560cc8d6eef7190193bb5659440c..70524bdb3c9703d5e221daaeb9777f1df7c9e595 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/answer/IAnswerService.java +++ b/src/main/java/br/com/ages/vouimigrar/service/answer/IAnswerService.java @@ -1,7 +1,13 @@ package br.com.ages.vouimigrar.service.answer; +import br.com.ages.vouimigrar.dto.AnswerResponseDTO; import br.com.ages.vouimigrar.entity.AnswerEntity; +import java.util.List; + public interface IAnswerService { AnswerEntity save(AnswerEntity answer); + AnswerEntity findById(long id); + List<AnswerResponseDTO> findAll(); + List<AnswerEntity> findAllByQuestionId(Long questionId); } diff --git a/src/main/java/br/com/ages/vouimigrar/service/country/CountryServiceImpl.java b/src/main/java/br/com/ages/vouimigrar/service/country/CountryServiceImpl.java index 2236f93d40209c035aaf009184496c5976f640f8..3ac7af408fc7ad8d173dcf73b84b40558e8d1323 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/country/CountryServiceImpl.java +++ b/src/main/java/br/com/ages/vouimigrar/service/country/CountryServiceImpl.java @@ -1,6 +1,5 @@ package br.com.ages.vouimigrar.service.country; - import br.com.ages.vouimigrar.dto.CountryDTO; import br.com.ages.vouimigrar.dto.CountryInfoDTO; import br.com.ages.vouimigrar.dto.CountryPhotosDTO; @@ -47,8 +46,7 @@ public class CountryServiceImpl implements ICountryService{ @Override public List<CountryEntity> findAll() { - List<CountryEntity> countries = this.repository.findAll(); - return countries; + return this.repository.findAll(); } @Override @@ -57,15 +55,14 @@ public class CountryServiceImpl implements ICountryService{ List<CountryInfoEntity> countryInfos = this.countryInfoService.findAllByCountryId(id); List<CountryPhotosEntity> countryPhotos = this.countryPhotosService.findAllByCountryId(id); - CountryDTO countryDTO = CountryDTO.builder() + return CountryDTO.builder() .id(countryEntity.getId()) .name(countryEntity.getName()) + .image(countryEntity.getImage()) .description(countryEntity.getDescription()) .infos(countryInfos.stream().map(this::buildCountryInfoDTO).collect(Collectors.toList())) .photos(countryPhotos.stream().map(this::buildCountryPhotosDTO).collect(Collectors.toList())) .build(); - - return countryDTO; } private CountryInfoDTO buildCountryInfoDTO(CountryInfoEntity entity){ diff --git a/src/main/java/br/com/ages/vouimigrar/service/match/IMatchService.java b/src/main/java/br/com/ages/vouimigrar/service/match/IMatchService.java index 2c28ef579b0d336a0e1ee002867c87daa21d983d..17412d07319cad768743d917150d2fa0bcef2720 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/match/IMatchService.java +++ b/src/main/java/br/com/ages/vouimigrar/service/match/IMatchService.java @@ -1,12 +1,17 @@ package br.com.ages.vouimigrar.service.match; +import br.com.ages.vouimigrar.dto.MatchResponseDTO; +import br.com.ages.vouimigrar.dto.UserAnswerDTO; import br.com.ages.vouimigrar.entity.MatchEntity; import java.util.List; public interface IMatchService { MatchEntity save(MatchEntity matchEntity); - List<MatchEntity> findByUser(); + List<MatchResponseDTO> findByUser(); + void calculateMatchPercentage(List<UserAnswerDTO> userAnswers); + void deleteAllByUserId(Long user); + List<MatchResponseDTO> findAll(); } diff --git a/src/main/java/br/com/ages/vouimigrar/service/match/MatchServiceImpl.java b/src/main/java/br/com/ages/vouimigrar/service/match/MatchServiceImpl.java index 66cfa1f1e3d3182ddb91759c057a9f916a07442d..67cceba433df29e3e2a85d47eec447169c8156ce 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/match/MatchServiceImpl.java +++ b/src/main/java/br/com/ages/vouimigrar/service/match/MatchServiceImpl.java @@ -1,23 +1,46 @@ package br.com.ages.vouimigrar.service.match; -import br.com.ages.vouimigrar.entity.MatchEntity; -import br.com.ages.vouimigrar.entity.UserEntity; +import br.com.ages.vouimigrar.dto.*; +import br.com.ages.vouimigrar.entity.*; +import br.com.ages.vouimigrar.enums.QuestionType; import br.com.ages.vouimigrar.repository.IMatchRepository; +import br.com.ages.vouimigrar.service.answer.IAnswerService; +import br.com.ages.vouimigrar.service.country.ICountryService; +import br.com.ages.vouimigrar.service.question.IQuestionService; import br.com.ages.vouimigrar.service.user.IUserService; +import br.com.ages.vouimigrar.service.useranswer.IUserAnswerService; +import br.com.ages.vouimigrar.service.weightcountryanswer.IWeightCountryAnswerService; +import br.com.ages.vouimigrar.validation.Assert; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.stream.Collectors; +@Slf4j @Service public class MatchServiceImpl implements IMatchService { private final IMatchRepository repository; private final IUserService userService; + private final ICountryService countryService; + private final IWeightCountryAnswerService weightCountryAnswerService; + private final IUserAnswerService userAnswerService; + private final IQuestionService questionService; + private final IAnswerService answerService; + @Autowired - public MatchServiceImpl(IMatchRepository repository, IUserService userService) { + public MatchServiceImpl(IMatchRepository repository, ICountryService countryService, + IWeightCountryAnswerService weightCountryAnswerService, IUserService userService, IUserAnswerService userAnswerService, IQuestionService questionService, IAnswerService answerService) { this.repository = repository; this.userService = userService; + this.countryService = countryService; + this.weightCountryAnswerService = weightCountryAnswerService; + this.userAnswerService = userAnswerService; + this.questionService = questionService; + this.answerService = answerService; } @Override @@ -26,9 +49,97 @@ public class MatchServiceImpl implements IMatchService { } @Override - public List<MatchEntity> findByUser() { + public List<MatchResponseDTO> findByUser() { UserEntity user = this.userService.getLoggedUser(); - return this.repository.findByUser(user, Sort.by(Sort.Direction.DESC,"matchPercentage")); + return this.repository.findByUser(user, Sort.by(Sort.Direction.DESC,"matchPercentage")) + .stream().map(this::buildMatchResponseDTO).collect(Collectors.toList()); + } + + @Override + public List<MatchResponseDTO> findAll() { + List<MatchEntity> matches = this.repository.findAll(); + return matches.stream() + .map(this::buildMatchResponseDTO) + .collect(Collectors.toList()); + } + + @Override + public void deleteAllByUserId(Long id){ + this.repository.deleteAllByUserUserId(id); + } + + @Transactional + @Override + public void calculateMatchPercentage(List<UserAnswerDTO> userAnswers) { + UserEntity user = this.userService.getLoggedUser(); + + this.deleteAllByUserId(user.getUserId()); + + List<UserAnswerEntity> userAnswerEntities = this.deleteAndSaveAllUserAnswers(userAnswers, user); + + List<CountryEntity> countries = this.countryService.findAll(); + countries.forEach(country -> { + try { + List<UserAnswerEntity> objectiveAnswers = userAnswerEntities.stream() + .filter(userAnswer -> Assert.isNotNull(userAnswer.getQuestion()) && QuestionType.OBJECTIVE.equals(userAnswer.getQuestion().getQuestionType())) + .collect(Collectors.toList()); + + long sum = objectiveAnswers.stream() + .map(userAnswer -> this.weightCountryAnswerService.findAllByAnswerIdAndCountryId(userAnswer.getAnswer().getId(), country.getId())) + .mapToLong(WeightCountryAnswerEntity::getWeight) + .sum(); + + Long percentage = sum / objectiveAnswers.size(); + + MatchEntity match = this.buildMatchEntity(user, country, percentage); + + this.save(match); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + }); + + } + + private List<UserAnswerEntity> deleteAndSaveAllUserAnswers(List<UserAnswerDTO> userAnswers, UserEntity user) { + List<UserAnswerEntity> userAnswerEntities = userAnswers.stream() + .map(userAnswerDTO -> buildUserAnswerEntity(user, userAnswerDTO)) + .collect(Collectors.toList()); + + return this.userAnswerService.deleteAndSaveAllUserAnswer(userAnswerEntities); + } + + private UserAnswerEntity buildUserAnswerEntity(UserEntity user, UserAnswerDTO userAnswerDTO) { + return UserAnswerEntity.builder() + .question(this.questionService.findById(userAnswerDTO.getQuestionId())) + .answer(this.answerService.findById(userAnswerDTO.getAnswerId())) + .user(user) + .build(); + } + + private MatchEntity buildMatchEntity(UserEntity user, CountryEntity country, Long percentage) { + return MatchEntity.builder() + .country(country) + .user(user) + .matchPercentage(percentage) + .build(); + } + + private MatchResponseDTO buildMatchResponseDTO(MatchEntity match) { + return MatchResponseDTO.builder() + .id(match.getId()) + .country(this.buildCountryResponseDTO(match.getCountry())) + .matchPercentage(match.getMatchPercentage()) + .build(); + } + + private CountryResponseDTO buildCountryResponseDTO(CountryEntity country){ + return CountryResponseDTO.builder() + .id(country.getId()) + .name(country.getName()) + .description(country.getDescription()) + .image(country.getImage()) + .build(); } } diff --git a/src/main/java/br/com/ages/vouimigrar/service/question/IQuestionService.java b/src/main/java/br/com/ages/vouimigrar/service/question/IQuestionService.java index 9784e832ecaf4e847c8cd2e084ac9550547d99e9..3904744f4423cd39548e85e1509cb21b1aa6d138 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/question/IQuestionService.java +++ b/src/main/java/br/com/ages/vouimigrar/service/question/IQuestionService.java @@ -1,7 +1,12 @@ package br.com.ages.vouimigrar.service.question; +import br.com.ages.vouimigrar.dto.QuestionResponseDTO; import br.com.ages.vouimigrar.entity.QuestionEntity; +import java.util.List; + public interface IQuestionService { QuestionEntity save(QuestionEntity question); + QuestionEntity findById(Long id); + List<QuestionResponseDTO> findAll(); } diff --git a/src/main/java/br/com/ages/vouimigrar/service/question/QuestionServiceImpl.java b/src/main/java/br/com/ages/vouimigrar/service/question/QuestionServiceImpl.java index 261a31e40a16f5f5a4af817c2964e3e3c250171d..4afda1330ee25e5d3b0e4afb0f010bd6122b57a1 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/question/QuestionServiceImpl.java +++ b/src/main/java/br/com/ages/vouimigrar/service/question/QuestionServiceImpl.java @@ -1,21 +1,65 @@ package br.com.ages.vouimigrar.service.question; +import br.com.ages.vouimigrar.dto.AnswerResponseDTO; +import br.com.ages.vouimigrar.dto.QuestionResponseDTO; import br.com.ages.vouimigrar.entity.QuestionEntity; import br.com.ages.vouimigrar.repository.IQuestionRepository; +import br.com.ages.vouimigrar.service.answer.IAnswerService; +import br.com.ages.vouimigrar.util.exceptions.QuestionNotFoundException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.stream.Collectors; + @Service public class QuestionServiceImpl implements IQuestionService { private final IQuestionRepository repository; + private final IAnswerService answerService; @Autowired - public QuestionServiceImpl(IQuestionRepository repository) { + public QuestionServiceImpl(IQuestionRepository repository, IAnswerService answerService) { this.repository = repository; + this.answerService = answerService; } @Override public QuestionEntity save(QuestionEntity question) { return this.repository.save(question); } + + @Override + public QuestionEntity findById(Long id) { + return this.repository.findById(id).orElseThrow(QuestionNotFoundException::new); + } + + @Override + public List<QuestionResponseDTO> findAll() { + List<QuestionEntity> questions = this.repository.findAll(Sort.by(Sort.Direction.ASC, "position")); + return questions.stream() + .map(this::buildQuestionResponseDTO) + .collect(Collectors.toList()); + } + + private QuestionResponseDTO buildQuestionResponseDTO(QuestionEntity question) { + return QuestionResponseDTO.builder() + .id(question.getId()) + .description(question.getDescription()) + .position(question.getPosition()) + .questionType(question.getQuestionType()) + .requirementId(question.getRequirement() != null ? question.getRequirement().getId() : null) + .answers(this.findAllAnswersByQuestionId(question)) + .build(); + } + + private List<AnswerResponseDTO> findAllAnswersByQuestionId(QuestionEntity question) { + return this.answerService.findAllByQuestionId(question.getId()).stream() + .map(answer -> AnswerResponseDTO.builder() + .id(answer.getId()) + .questionId(answer.getQuestion().getId()) + .description(answer.getDescription()) + .build()) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/br/com/ages/vouimigrar/service/useranswer/IUserAnswerService.java b/src/main/java/br/com/ages/vouimigrar/service/useranswer/IUserAnswerService.java index 2a237837c779c4e0a66af7df0c0f5c2b4286d6ff..3ccdfe83a35c8f75db9b754dffc77fba91e0632e 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/useranswer/IUserAnswerService.java +++ b/src/main/java/br/com/ages/vouimigrar/service/useranswer/IUserAnswerService.java @@ -2,6 +2,11 @@ package br.com.ages.vouimigrar.service.useranswer; import br.com.ages.vouimigrar.entity.UserAnswerEntity; +import java.util.List; + public interface IUserAnswerService { UserAnswerEntity save(UserAnswerEntity userAnswer); + List<UserAnswerEntity> deleteAndSaveAllUserAnswer(List<UserAnswerEntity> userAnswers); + List<UserAnswerEntity> findAllByLoggedUser(); + void deleteAllByUserId(Long id); } diff --git a/src/main/java/br/com/ages/vouimigrar/service/useranswer/UserAnswerServiceImpl.java b/src/main/java/br/com/ages/vouimigrar/service/useranswer/UserAnswerServiceImpl.java index 25d5d446219607a4519896974892226fb5e9c228..bbe38222cf22f594f4cb85dbd082ceed9ba5b81c 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/useranswer/UserAnswerServiceImpl.java +++ b/src/main/java/br/com/ages/vouimigrar/service/useranswer/UserAnswerServiceImpl.java @@ -1,21 +1,53 @@ package br.com.ages.vouimigrar.service.useranswer; import br.com.ages.vouimigrar.entity.UserAnswerEntity; +import br.com.ages.vouimigrar.entity.UserEntity; import br.com.ages.vouimigrar.repository.IUserAnswerRepository; +import br.com.ages.vouimigrar.service.user.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; @Service public class UserAnswerServiceImpl implements IUserAnswerService { private final IUserAnswerRepository repository; + private final IUserService userService; @Autowired - public UserAnswerServiceImpl(IUserAnswerRepository repository) { + public UserAnswerServiceImpl(IUserAnswerRepository repository, IUserService userService) { this.repository = repository; + this.userService = userService; + } + + @Override + public List<UserAnswerEntity> findAllByLoggedUser() { + UserEntity user = this.userService.getLoggedUser(); + return this.repository.findUserAnswerEntitiesByUserUserId(user.getUserId()); + } + + @Transactional + @Override + public void deleteAllByUserId(Long userId) { + this.repository.deleteAllByUserUserId(userId); } @Override public UserAnswerEntity save(UserAnswerEntity userAnswer) { return this.repository.save(userAnswer); } + + @Transactional + @Override + public List<UserAnswerEntity> deleteAndSaveAllUserAnswer(List<UserAnswerEntity> userAnswers) { + UserEntity user = this.userService.getLoggedUser(); + this.deleteAllByUserId(user.getUserId()); + + for (UserAnswerEntity userAnswer : userAnswers) { + this.save(userAnswer); + } + + return this.findAllByLoggedUser(); + } } diff --git a/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/IWeightCountryAnswerService.java b/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/IWeightCountryAnswerService.java index e76817471747724b6c42e9087011fef57feee6c4..22c72a03db47113c138cbe0dc61f8556dec9dfe3 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/IWeightCountryAnswerService.java +++ b/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/IWeightCountryAnswerService.java @@ -4,4 +4,6 @@ import br.com.ages.vouimigrar.entity.WeightCountryAnswerEntity; public interface IWeightCountryAnswerService { WeightCountryAnswerEntity save(WeightCountryAnswerEntity weightCountryAnswer); + + WeightCountryAnswerEntity findAllByAnswerIdAndCountryId(Long answerId, Long countryId); } diff --git a/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/WeightCountryAnswerServiceImpl.java b/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/WeightCountryAnswerServiceImpl.java index 4353926a5eb6e9dc16df36bff599584179ba41b4..733a4504552dff18616557990e686d26301784bf 100644 --- a/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/WeightCountryAnswerServiceImpl.java +++ b/src/main/java/br/com/ages/vouimigrar/service/weightcountryanswer/WeightCountryAnswerServiceImpl.java @@ -18,4 +18,9 @@ public class WeightCountryAnswerServiceImpl implements IWeightCountryAnswerServi public WeightCountryAnswerEntity save(WeightCountryAnswerEntity weightCountryAnswer) { return this.repository.save(weightCountryAnswer); } + + @Override + public WeightCountryAnswerEntity findAllByAnswerIdAndCountryId(Long answerId, Long countryId){ + return this.repository.findAllByAnswerIdAndCountryId(answerId, countryId); + } } diff --git a/src/main/java/br/com/ages/vouimigrar/util/exceptions/AnswerNotFoundException.java b/src/main/java/br/com/ages/vouimigrar/util/exceptions/AnswerNotFoundException.java new file mode 100644 index 0000000000000000000000000000000000000000..1c336070e2b1996b34430b12b03940b1a5ff2408 --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/util/exceptions/AnswerNotFoundException.java @@ -0,0 +1,9 @@ +package br.com.ages.vouimigrar.util.exceptions; + +public class AnswerNotFoundException extends RuntimeException{ + private static final long serialVersionUID = 1L; + + public AnswerNotFoundException() { + super("Resposta não encontrada."); + } +} diff --git a/src/main/java/br/com/ages/vouimigrar/util/exceptions/QuestionNotFoundException.java b/src/main/java/br/com/ages/vouimigrar/util/exceptions/QuestionNotFoundException.java new file mode 100644 index 0000000000000000000000000000000000000000..9b7bfd54904bf588ac8b11c19b77e5aed9a100f8 --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/util/exceptions/QuestionNotFoundException.java @@ -0,0 +1,9 @@ +package br.com.ages.vouimigrar.util.exceptions; + +public class QuestionNotFoundException extends RuntimeException{ + private static final long serialVersionUID = 1L; + + public QuestionNotFoundException() { + super("Questão não encontrada."); + } +} diff --git a/src/main/java/br/com/ages/vouimigrar/util/handlers/AnswerExceptionHandler.java b/src/main/java/br/com/ages/vouimigrar/util/handlers/AnswerExceptionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..e3ec7e316579332822edd1923807be49b8ad00b7 --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/util/handlers/AnswerExceptionHandler.java @@ -0,0 +1,16 @@ +package br.com.ages.vouimigrar.util.handlers; + +import br.com.ages.vouimigrar.util.exceptions.AnswerNotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class AnswerExceptionHandler { + + @ExceptionHandler(value = AnswerNotFoundException.class) + public ResponseEntity<Object> handleException(AnswerNotFoundException exception) { + return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND); + } +} diff --git a/src/main/java/br/com/ages/vouimigrar/util/handlers/QuestionExceptionHandler.java b/src/main/java/br/com/ages/vouimigrar/util/handlers/QuestionExceptionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..a14a0f2539caead9952a98ba76e982f2f542a302 --- /dev/null +++ b/src/main/java/br/com/ages/vouimigrar/util/handlers/QuestionExceptionHandler.java @@ -0,0 +1,15 @@ +package br.com.ages.vouimigrar.util.handlers; + +import br.com.ages.vouimigrar.util.exceptions.QuestionNotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class QuestionExceptionHandler { + @ExceptionHandler(value = QuestionNotFoundException.class) + public ResponseEntity<Object> handleException(QuestionNotFoundException exception) { + return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND); + } +} diff --git a/src/main/java/br/com/ages/vouimigrar/validation/Assert.java b/src/main/java/br/com/ages/vouimigrar/validation/Assert.java index a279f8454b3ca4d665e1023ea25ded226ad8c7b3..a9dbeef719092a7391b62890bf75c21b827c19de 100644 --- a/src/main/java/br/com/ages/vouimigrar/validation/Assert.java +++ b/src/main/java/br/com/ages/vouimigrar/validation/Assert.java @@ -1,18 +1,13 @@ package br.com.ages.vouimigrar.validation; -import java.util.Date; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.Objects; public class Assert { - public static void hasField(Object field, String message) { - if (field == null || (field instanceof String && ((String) field).trim().isEmpty())) { -// throw new BusinessException(message); - } - } - public static boolean hasField(Object field) { if (field == null || (field instanceof String && ((String) field).trim().isEmpty())) { return false; @@ -60,4 +55,12 @@ public class Assert { return true; } + public static boolean isNull(Object object) { + return Objects.isNull(object); + } + + public static boolean isNotNull(Object object) { + return !isNull(object); + } + } diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index cd49b22ec47a497ce4ea8914f40043eaa5767f2f..c15303bb065a572781f98dae11b5309529565d4d 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -7,4 +7,4 @@ spring.h2.console.enabled=true spring.h2.console.path=/h2 spring.h2.console.settings.trace=false spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER - +logging.level.org.hibernate.SQL=DEBUG