[FEATURE] Обобщение UserDAO. Команда регистрации и стандартный UserDAO

This commit is contained in:
Gravit 2019-06-03 14:03:17 +07:00
parent aced376e5e
commit e13c5580bc
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
10 changed files with 91 additions and 12 deletions

View file

@ -1,7 +1,7 @@
package pro.gravit.launchserver.auth.handler; package pro.gravit.launchserver.auth.handler;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.hibernate.User; import pro.gravit.launchserver.dao.User;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;

View file

@ -2,7 +2,7 @@
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.hibernate.User; import pro.gravit.launchserver.dao.User;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
import java.io.IOException; import java.io.IOException;

View file

@ -0,0 +1,33 @@
package pro.gravit.launchserver.command.dao;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.Command;
import pro.gravit.launchserver.dao.User;
import java.util.UUID;
public class RegisterCommand extends Command {
public RegisterCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[login] [password]";
}
@Override
public String getUsageDescription() {
return "Register new user";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 2);
User user = new User();
user.username = args[0];
user.setPassword(args[1]);
user.uuid = UUID.randomUUID();
LaunchServer.server.userService.saveUser(user);
}
}

View file

@ -4,8 +4,8 @@
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.LaunchServerDaoFactory; import pro.gravit.launchserver.dao.LaunchServerDaoFactory;
import pro.gravit.launchserver.hibernate.HibernateManager; import pro.gravit.launchserver.hibernate.HibernateManager;
import pro.gravit.launchserver.hibernate.User; import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.hibernate.UserDAOImpl; import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
import pro.gravit.utils.helper.CommonHelper; import pro.gravit.utils.helper.CommonHelper;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -20,7 +20,7 @@ public class HibernateConfiguratorComponent extends Component {
public boolean parallelHibernateInit; public boolean parallelHibernateInit;
@Override @Override
public void preInit(LaunchServer launchServer) { public void preInit(LaunchServer launchServer) {
LaunchServerDaoFactory.setUserDaoProvider(UserDAOImpl::new); LaunchServerDaoFactory.setUserDaoProvider(HibernateUserDAOImpl::new);
Runnable init = () -> { Runnable init = () -> {
Configuration cfg = new Configuration() Configuration cfg = new Configuration()
.addAnnotatedClass(User.class) .addAnnotatedClass(User.class)

View file

@ -1,9 +1,11 @@
package pro.gravit.launchserver.dao; package pro.gravit.launchserver.dao;
import pro.gravit.launchserver.dao.impl.DefaultUserDAOImpl;
import java.util.function.Supplier; import java.util.function.Supplier;
public class LaunchServerDaoFactory { public class LaunchServerDaoFactory {
private static Supplier<UserDAO> getUserDao; private static Supplier<UserDAO> getUserDao = DefaultUserDAOImpl::new;
public static void setUserDaoProvider(Supplier<UserDAO> getDao) { public static void setUserDaoProvider(Supplier<UserDAO> getDao) {
LaunchServerDaoFactory.getUserDao = getDao; LaunchServerDaoFactory.getUserDao = getDao;

View file

@ -1,4 +1,4 @@
package pro.gravit.launchserver.hibernate; package pro.gravit.launchserver.dao;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;

View file

@ -1,7 +1,5 @@
package pro.gravit.launchserver.dao; package pro.gravit.launchserver.dao;
import pro.gravit.launchserver.hibernate.User;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;

View file

@ -1,7 +1,6 @@
package pro.gravit.launchserver.dao; package pro.gravit.launchserver.dao;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.hibernate.User;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;

View file

@ -0,0 +1,45 @@
package pro.gravit.launchserver.dao.impl;
import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class DefaultUserDAOImpl implements UserDAO {
@Override
public User findById(int id) {
return null;
}
@Override
public User findByUsername(String username) {
return null;
}
@Override
public User findByUUID(UUID uuid) {
return null;
}
@Override
public void save(User user) {
throw new UnsupportedOperationException();
}
@Override
public void update(User user) {
throw new UnsupportedOperationException();
}
@Override
public void delete(User user) {
throw new UnsupportedOperationException();
}
@Override
public List<User> findAll() {
return new ArrayList<>();
}
}

View file

@ -1,8 +1,10 @@
package pro.gravit.launchserver.hibernate; package pro.gravit.launchserver.dao.impl;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserDAO; import pro.gravit.launchserver.dao.UserDAO;
import pro.gravit.launchserver.hibernate.HibernateManager;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
@ -11,7 +13,7 @@
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class UserDAOImpl implements UserDAO { public class HibernateUserDAOImpl implements UserDAO {
public User findById(int id) { public User findById(int id) {
return HibernateManager.sessionFactory.openSession().get(User.class, id); return HibernateManager.sessionFactory.openSession().get(User.class, id);