Skip to content

Commit

Permalink
Fix data import issues
Browse files Browse the repository at this point in the history
  • Loading branch information
orbanszlrd committed Jan 6, 2025
1 parent 50426d2 commit fa1fa0a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.orbanszlrd.quizapi.modules.user.model.User;
import com.orbanszlrd.quizapi.modules.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

Expand All @@ -15,12 +13,9 @@ public class UserImportService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;

@Bean
public CommandLineRunner userImporter() {
return args -> {
userRepository.save(new User(1L, "owner", "[email protected]", passwordEncoder.encode("owner"), true, Role.OWNER));
userRepository.save(new User(2L, "admin", "[email protected]", passwordEncoder.encode("admin"), true, Role.ADMIN));
userRepository.save(new User(3L, "user", "[email protected]", passwordEncoder.encode("user"), true, Role.USER));
};
public void createUsers() {
userRepository.save(new User("owner", "[email protected]", passwordEncoder.encode("owner"), true, Role.OWNER));
userRepository.save(new User("admin", "[email protected]", passwordEncoder.encode("admin"), true, Role.ADMIN));
userRepository.save(new User("user", "[email protected]", passwordEncoder.encode("user"), true, Role.USER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.orbanszlrd.quizapi.modules.user.model.dto.UserResponse;
import com.orbanszlrd.quizapi.modules.user.model.dto.UpdateUserRequest;
import com.orbanszlrd.quizapi.modules.user.error.UserNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
Expand All @@ -20,6 +21,7 @@
import java.util.List;
import java.util.Optional;

@Transactional
@Service
@RequiredArgsConstructor
public class UserService implements UserDetailsService {
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/com/orbanszlrd/quizapi/service/ImportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.orbanszlrd.quizapi.modules.question.repository.QuestionRepository;
import com.orbanszlrd.quizapi.modules.quiz.model.Quiz;
import com.orbanszlrd.quizapi.modules.quiz.repository.QuizRepository;
import com.orbanszlrd.quizapi.modules.user.service.UserImportService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
Expand All @@ -30,18 +32,23 @@ public class ImportService {
private final QuestionRepository questionRepository;
private final AnswerRepository answerRepository;

private final UserImportService userImportService;
private final CountryImportService countryImportService;

@Bean
public CommandLineRunner fillDatabase() {
return args -> {

userImportService.createUsers();

countryImportService.fillDatabase();

// createDummyItQuizzes();
// createDummyGeographyQuizzes();
createDummyItQuizzes();
createDummyGeographyQuizzes();
};
}

@Transactional
private void createDummyGeographyQuizzes() {
Category category = categoryRepository.save(new Category("Geography"));

Expand Down Expand Up @@ -92,36 +99,37 @@ private <T> int getRandomIndex(int currentIndex, Collection<T> collection) {
return randomIndex;
}

@Transactional
private void createDummyItQuizzes() {
System.out.println("Fill the database with categories");
Category it = new Category(1L, "IT");
Category it = new Category("IT");
categoryRepository.save(it);

System.out.println("Fill the database with quizzes");
Quiz java = new Quiz(1L, "Java Basics", 30, it);
Quiz java = new Quiz("Java Basics", 30, it);
quizRepository.save(java);

Quiz javaScript = new Quiz(2L, "Javascript Basics", 30, it);
Quiz javaScript = new Quiz("Javascript Basics", 30, it);
quizRepository.save(javaScript);

Quiz python = new Quiz(3L, "Python Basics", 30, it);
Quiz python = new Quiz("Python Basics", 30, it);
quizRepository.save(python);

System.out.println("Fill the database with questions");
Question q1 = new Question(1L, "What is the range of short data type in Java? ", 1, (byte) 5, java);
Question q1 = new Question("What is the range of short data type in Java? ", 1, (byte) 5, java);
questionRepository.save(q1);

System.out.println("Fill the database with answers");
Answer a1 = new Answer(1L, "-128 to 127", false, q1);
Answer a1 = new Answer( "-128 to 127", false, q1);
answerRepository.save(a1);

Answer a2 = new Answer(2L, "-32 768 to 32 767", true, q1);
Answer a2 = new Answer("-32 768 to 32 767", true, q1);
answerRepository.save(a2);

Answer a3 = new Answer(3L, "-2 147 483 648 to 2 147 483 647", false, q1);
Answer a3 = new Answer("-2 147 483 648 to 2 147 483 647", false, q1);
answerRepository.save(a3);

Answer a4 = new Answer(4L, "None of the mentioned", false, q1);
Answer a4 = new Answer("None of the mentioned", false, q1);
answerRepository.save(a4);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ spring.datasource.password=

spring.flyway.enabled=false

#spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.ddl-auto=create
#spring.jpa.hibernate.ddl-auto=validate
spring.jpa.open-in-view=true
spring.jpa.show-sql=true

Expand Down

0 comments on commit fa1fa0a

Please sign in to comment.