Автозагрузка пользователей в БД
parent
148c511b9e
commit
e1ea86b36b
|
@ -0,0 +1,17 @@
|
|||
package ru.ldeloff.servermonitorbot.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ldeloff.servermonitorbot.model.dto.InitUserDto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "bot")
|
||||
@Getter
|
||||
@Setter
|
||||
public class UserConfig {
|
||||
private List<InitUserDto> users;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.config.UserConfig;
|
||||
import ru.ldeloff.servermonitorbot.mapper.UserMapper;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.model.dto.InitUserDto;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class AddUsers implements ApplicationRunner {
|
||||
final List<InitUserDto> initUsers;
|
||||
final UserService userService;
|
||||
final UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
public AddUsers(UserConfig userConfig, UserService userService, UserMapper userMapper) {
|
||||
this.initUsers = userConfig.getUsers();
|
||||
this.userService = userService;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
initUsers.forEach(initUserDto -> {
|
||||
User user = userService.saveOrUpdateUser(userMapper.dtoToUser(initUserDto));
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package ru.ldeloff.servermonitorbot.mapper;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.model.dto.InitUserDto;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
|
||||
@Component
|
||||
public class UserMapper {
|
||||
final RoleService roleService;
|
||||
|
||||
public UserMapper(RoleService roleService) {
|
||||
this.roleService = roleService;
|
||||
}
|
||||
|
||||
public User dtoToUser(InitUserDto initUserDto) {
|
||||
User user = new User();
|
||||
user.setTelegramId(initUserDto.getTelegramId());
|
||||
user.setRole(roleService.findRoleByName(initUserDto.getRole()));
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
public class InitUserDto {
|
||||
@NotNull
|
||||
private long telegramId;
|
||||
@NotNull
|
||||
private String role;
|
||||
}
|
|
@ -19,6 +19,9 @@ spring:
|
|||
bot:
|
||||
name: "ServerMonitorBot"
|
||||
token: "token"
|
||||
users:
|
||||
- telegramId: 123456789
|
||||
role: admin
|
||||
ssh:
|
||||
servers:
|
||||
-
|
||||
|
|
Loading…
Reference in New Issue