Skip to content

Commit

Permalink
Update Datainitializer and UserMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
niyatanya committed Aug 23, 2024
1 parent fb12a7c commit 3d9d8e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
56 changes: 36 additions & 20 deletions src/main/java/hexlet/code/component/DataInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hexlet.code.model.Label;
import hexlet.code.repository.LabelRepository;
import hexlet.code.repository.TaskStatusRepository;
import hexlet.code.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
Expand All @@ -13,6 +14,9 @@
import hexlet.code.service.CustomUserDetailsService;
import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Component
@AllArgsConstructor
public class DataInitializer implements ApplicationRunner {
Expand All @@ -23,31 +27,43 @@ public class DataInitializer implements ApplicationRunner {
@Autowired
private LabelRepository labelRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private final CustomUserDetailsService userService;

@Override
public void run(ApplicationArguments args) {
String email = "hexlet@example.com";
User userData = new User();
userData.setEmail(email);
userData.setPasswordDigest("qwerty");
userService.createUser(userData);

TaskStatus status1 = new TaskStatus("черновик", "draft");
TaskStatus status2 = new TaskStatus("на проверку", "to_review");
TaskStatus status3 = new TaskStatus("на доработку", "to_be_fixed");
TaskStatus status4 = new TaskStatus("к публикации", "to_publish");
TaskStatus status5 = new TaskStatus("опубликовано", "published");
statusRepository.save(status1);
statusRepository.save(status2);
statusRepository.save(status3);
statusRepository.save(status4);
statusRepository.save(status5);

Label label1 = new Label("feature");
Label label2 = new Label("bug");
labelRepository.save(label1);
labelRepository.save(label2);
if (userRepository.findByEmail(email).isEmpty()) {
User userData = new User();
userData.setEmail(email);
userData.setPasswordDigest("qwerty");
userService.createUser(userData);
}

List<TaskStatus> statuses = new ArrayList<>(List.of(
new TaskStatus("черновик", "draft"),
new TaskStatus("на проверку", "to_review"),
new TaskStatus("на доработку", "to_be_fixed"),
new TaskStatus("к публикации", "to_publish"),
new TaskStatus("опубликовано", "published")));

for (TaskStatus status : statuses) {
if (statusRepository.findByName(status.getName()).isEmpty()) {
statusRepository.save(status);
}
}

List<Label> labels = new ArrayList<>(List.of(
new Label("feature"),
new Label("bug")));

for (Label label : labels) {
if (labelRepository.findByName(label.getName()).isEmpty()) {
labelRepository.save(label);
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/hexlet/code/mapper/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.mapstruct.Mapping;
import org.mapstruct.BeforeMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Mapper(
uses = { JsonNullableMapper.class },
Expand All @@ -24,7 +24,7 @@
public abstract class UserMapper {

@Autowired
private BCryptPasswordEncoder encoder;
private PasswordEncoder encoder;

@Mapping(target = "passwordDigest", source = "password")
public abstract User map(UserCreateDTO dto);
Expand Down

0 comments on commit 3d9d8e4

Please sign in to comment.