auto gen dao beans

This commit is contained in:
MiniDigger 2020-07-16 23:23:04 +02:00
parent 1a55e5bf88
commit a3199624f4
7 changed files with 54 additions and 25 deletions

View File

@ -97,6 +97,10 @@
<groupId>org.jdbi</groupId> <groupId>org.jdbi</groupId>
<artifactId>jdbi3-postgres</artifactId> <artifactId>jdbi3-postgres</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-spring4</artifactId>
</dependency>
<!-- webjars --> <!-- webjars -->
<dependency> <dependency>

View File

@ -1,31 +1,25 @@
package me.minidigger.hangar.config; package me.minidigger.hangar.config;
import org.jdbi.v3.core.Jdbi; import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.spi.JdbiPlugin; import org.jdbi.v3.core.spi.JdbiPlugin;
import org.jdbi.v3.postgres.PostgresPlugin; import org.jdbi.v3.postgres.PostgresPlugin;
import org.jdbi.v3.spring4.JdbiFactoryBean;
import org.jdbi.v3.sqlobject.SqlObjectPlugin; import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; import org.springframework.context.annotation.Scope;
import java.util.List; import java.util.List;
import javax.sql.DataSource; import javax.sql.DataSource;
import me.minidigger.hangar.db.dao.UserDao; import me.minidigger.hangar.db.dao.HangarDao;
@Configuration @Configuration
public class JDBIConfig { public class JDBIConfig {
@Bean
public Jdbi jdbi(DataSource ds, List<JdbiPlugin> jdbiPlugins, List<RowMapper<?>> rowMappers) {
TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds);
Jdbi jdbi = Jdbi.create(proxy);
jdbiPlugins.forEach(jdbi::installPlugin);
rowMappers.forEach(jdbi::registerRowMapper);
return jdbi;
}
@Bean @Bean
public JdbiPlugin sqlObjectPlugin() { public JdbiPlugin sqlObjectPlugin() {
return new SqlObjectPlugin(); return new SqlObjectPlugin();
@ -37,7 +31,20 @@ public class JDBIConfig {
} }
@Bean @Bean
public UserDao userDao(Jdbi jdbi) { public JdbiFactoryBean jdbiFactoryBean(DataSource dataSource, List<JdbiPlugin> jdbiPlugins) {
return jdbi.onDemand(UserDao.class); // TODO this is bad, replace me with not on demand JdbiFactoryBean jdbiFactoryBean = new JdbiFactoryBean(dataSource);
jdbiFactoryBean.setPlugins(jdbiPlugins);
return jdbiFactoryBean;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public <T> HangarDao<T> hangarDao(Jdbi jdbi, InjectionPoint injectionPoint) {
if (injectionPoint instanceof DependencyDescriptor) {
DependencyDescriptor descriptor = (DependencyDescriptor) injectionPoint;
//noinspection unchecked
return new HangarDao<>((T) jdbi.onDemand(descriptor.getResolvableType().getGeneric(0).getRawClass()));
}
return null;
} }
} }

View File

@ -13,6 +13,7 @@ import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import me.minidigger.hangar.config.HangarConfig; import me.minidigger.hangar.config.HangarConfig;
import me.minidigger.hangar.db.dao.HangarDao;
import me.minidigger.hangar.db.dao.UserDao; import me.minidigger.hangar.db.dao.UserDao;
import me.minidigger.hangar.db.model.UsersTable; import me.minidigger.hangar.db.model.UsersTable;
import me.minidigger.hangar.service.AuthenticationService; import me.minidigger.hangar.service.AuthenticationService;
@ -20,13 +21,13 @@ import me.minidigger.hangar.service.AuthenticationService;
@Controller @Controller
public class UsersController extends HangarController { public class UsersController extends HangarController {
private final UserDao userDao; private final HangarDao<UserDao> userDao;
private final HangarConfig hangarConfig; private final HangarConfig hangarConfig;
private final AuthenticationService authenticationService; private final AuthenticationService authenticationService;
private final ApplicationController applicationController; private final ApplicationController applicationController;
@Autowired @Autowired
public UsersController(HangarConfig hangarConfig, UserDao userDao, AuthenticationService authenticationService, ApplicationController applicationController) { public UsersController(HangarConfig hangarConfig, HangarDao<UserDao> userDao, AuthenticationService authenticationService, ApplicationController applicationController) {
this.hangarConfig = hangarConfig; this.hangarConfig = hangarConfig;
this.userDao = userDao; this.userDao = userDao;
this.authenticationService = authenticationService; this.authenticationService = authenticationService;
@ -99,7 +100,7 @@ public class UsersController extends HangarController {
@RequestMapping("/{user}") @RequestMapping("/{user}")
public Object showProjects(@PathVariable String user) { public Object showProjects(@PathVariable String user) {
// TODO hacky test shit // TODO hacky test shit
UsersTable dbUser = userDao.getByName(user); UsersTable dbUser = userDao.get().getByName(user);
if (dbUser == null) { if (dbUser == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND); throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} }

View File

@ -0,0 +1,14 @@
package me.minidigger.hangar.db.dao;
public class HangarDao<T> {
private final T dao;
public HangarDao(T dao) {
this.dao = dao;
}
public T get() {
return dao;
}
}

View File

@ -1,10 +1,11 @@
package me.minidigger.hangar.db.model; package me.minidigger.hangar.db.model;
import java.io.Serializable;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.util.StringJoiner; import java.util.StringJoiner;
public class UsersTable { public class UsersTable implements Serializable {
private long id; private long id;
private OffsetDateTime createdAt; private OffsetDateTime createdAt;

View File

@ -8,15 +8,16 @@ import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
import me.minidigger.hangar.db.dao.HangarDao;
import me.minidigger.hangar.db.dao.UserDao; import me.minidigger.hangar.db.dao.UserDao;
import me.minidigger.hangar.db.model.UsersTable; import me.minidigger.hangar.db.model.UsersTable;
@Component @Component
public class HangarAuthenticationProvider implements AuthenticationProvider { public class HangarAuthenticationProvider implements AuthenticationProvider {
private final UserDao userDao; private final HangarDao<UserDao> userDao;
public HangarAuthenticationProvider(UserDao userDao) { public HangarAuthenticationProvider(HangarDao<UserDao> userDao) {
this.userDao = userDao; this.userDao = userDao;
} }
@ -26,7 +27,7 @@ public class HangarAuthenticationProvider implements AuthenticationProvider {
HangarAuthentication auth = (HangarAuthentication) authentication; HangarAuthentication auth = (HangarAuthentication) authentication;
String name = auth.getName(); String name = auth.getName();
UsersTable usersTable = userDao.getByName(name); UsersTable usersTable = userDao.get().getByName(name);
// TODO validate stuff, guess we need to pass sso stuff here? // TODO validate stuff, guess we need to pass sso stuff here?
if (usersTable != null) { if (usersTable != null) {

View File

@ -12,6 +12,7 @@ import java.time.OffsetDateTime;
import java.util.UUID; import java.util.UUID;
import me.minidigger.hangar.config.HangarConfig; import me.minidigger.hangar.config.HangarConfig;
import me.minidigger.hangar.db.dao.HangarDao;
import me.minidigger.hangar.db.dao.UserDao; import me.minidigger.hangar.db.dao.UserDao;
import me.minidigger.hangar.db.model.UsersTable; import me.minidigger.hangar.db.model.UsersTable;
import me.minidigger.hangar.model.generated.ApiSession; import me.minidigger.hangar.model.generated.ApiSession;
@ -24,11 +25,11 @@ import me.minidigger.hangar.security.HangarAuthentication;
public class AuthenticationService { public class AuthenticationService {
private final HangarConfig hangarConfig; private final HangarConfig hangarConfig;
private final UserDao userDao; private final HangarDao<UserDao> userDao;
private final AuthenticationManager authenticationManager; private final AuthenticationManager authenticationManager;
@Autowired @Autowired
public AuthenticationService(HangarConfig hangarConfig, UserDao userDao, AuthenticationManager authenticationManager) { public AuthenticationService(HangarConfig hangarConfig, HangarDao<UserDao> userDao, AuthenticationManager authenticationManager) {
this.hangarConfig = hangarConfig; this.hangarConfig = hangarConfig;
this.userDao = userDao; this.userDao = userDao;
this.authenticationManager = authenticationManager; this.authenticationManager = authenticationManager;
@ -77,7 +78,7 @@ public class AuthenticationService {
public void loginAsFakeUser() { public void loginAsFakeUser() {
String username = hangarConfig.getFakeUserName(); String username = hangarConfig.getFakeUserName();
UsersTable userEntry = userDao.getByName(username); UsersTable userEntry = userDao.get().getByName(username);
if (userEntry == null) { if (userEntry == null) {
userEntry = new UsersTable(); userEntry = new UsersTable();
userEntry.setEmail(hangarConfig.getFakeUserEmail()); userEntry.setEmail(hangarConfig.getFakeUserEmail());
@ -86,7 +87,7 @@ public class AuthenticationService {
userEntry.setId(hangarConfig.getFakeUserId()); userEntry.setId(hangarConfig.getFakeUserId());
userEntry.setReadPrompts(new int[0]); userEntry.setReadPrompts(new int[0]);
userEntry = userDao.insert(userEntry); userEntry = userDao.get().insert(userEntry);
} }
// TODO properly do auth, remember me shit too // TODO properly do auth, remember me shit too
Authentication auth = new HangarAuthentication(userEntry.getName()); Authentication auth = new HangarAuthentication(userEntry.getName());