Commit 322f305c authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

move add method to repository

parent e13e8aa7
...@@ -20,7 +20,7 @@ public class RegisterTeamsCommand extends ConfiguredCommand<ApplicationConfigura ...@@ -20,7 +20,7 @@ public class RegisterTeamsCommand extends ConfiguredCommand<ApplicationConfigura
super(COMMAND_NAME, COMMAND_DESCRIPTION); super(COMMAND_NAME, COMMAND_DESCRIPTION);
} }
private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector) private void initializeTasks(ApplicationConfiguration applicationConfiguration, Injector injector) throws ClassNotFoundException
{ {
TeamsRepository teamsRepository = injector.getInstance(TeamsRepository.class); TeamsRepository teamsRepository = injector.getInstance(TeamsRepository.class);
UsersRepository usersRepository = injector.getInstance(UsersRepository.class); UsersRepository usersRepository = injector.getInstance(UsersRepository.class);
......
...@@ -4,10 +4,10 @@ import org.mongodb.morphia.Datastore; ...@@ -4,10 +4,10 @@ import org.mongodb.morphia.Datastore;
import javax.inject.Inject; import javax.inject.Inject;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List; import java.util.List;
public abstract class Repository<T> { public abstract class Repository<T>
{
protected Datastore datastore; protected Datastore datastore;
@Inject @Inject
...@@ -21,10 +21,20 @@ public abstract class Repository<T> { ...@@ -21,10 +21,20 @@ public abstract class Repository<T> {
} }
abstract List<T> getAll(); abstract List<T> getAll();
abstract void add(T t); //todo: the same for add
public void add(T item)
{
datastore.save(item);
}
Class getRepositoryType() throws ClassNotFoundException
{
return Class.forName((((ParameterizedType) this.getClass().getGenericSuperclass())
.getActualTypeArguments()[0]).getTypeName());
}
public void clean() throws ClassNotFoundException public void clean() throws ClassNotFoundException
{ {
Type type = ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; datastore.getCollection(getRepositoryType()).drop();
datastore.getCollection(Class.forName(type.getTypeName())).drop();
} }
} }
...@@ -30,12 +30,6 @@ public class SolutionsRepository extends Repository<Solution> ...@@ -30,12 +30,6 @@ public class SolutionsRepository extends Repository<Solution>
return datastore.createQuery(Solution.class).asList(); return datastore.createQuery(Solution.class).asList();
} }
public void add(Solution solution)
{ //todo
datastore.save(solution); //todo: error handling?
//todo: do not add if already exists
}
public List<Solution> getByTeam(Team team) public List<Solution> getByTeam(Team team)
{ {
//todo: merge with upper //todo: merge with upper
......
...@@ -58,11 +58,6 @@ public class TasksRepository extends Repository<Task> ...@@ -58,11 +58,6 @@ public class TasksRepository extends Repository<Task>
return datastore.createQuery(Task.class).asList(); return datastore.createQuery(Task.class).asList();
} }
public void add(Task task)
{
datastore.save(task);
}
//todo: refactor? //todo: refactor?
public Map<List<String>, Integer> getUserFlagsHashes(String username) public Map<List<String>, Integer> getUserFlagsHashes(String username)
{ {
......
...@@ -47,17 +47,4 @@ public class TeamsRepository extends Repository<Team> ...@@ -47,17 +47,4 @@ public class TeamsRepository extends Repository<Team>
{ {
return datastore.createQuery(Team.class).asList(); return datastore.createQuery(Team.class).asList();
} }
//todo: move to interface
//todo: use default as interface-implemented methods
public void add(Team team)
{
datastore.save(team);
}
//todo: move clean to upper class?
public void clean()
{
datastore.getCollection(Team.class).drop();
}
} }
...@@ -3,7 +3,6 @@ package com.telephoners.krakyournet.ctf.repositories; ...@@ -3,7 +3,6 @@ package com.telephoners.krakyournet.ctf.repositories;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import com.telephoners.krakyournet.ctf.beans.User; import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import io.dropwizard.auth.basic.BasicCredentials; import io.dropwizard.auth.basic.BasicCredentials;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import org.mongodb.morphia.Datastore; import org.mongodb.morphia.Datastore;
...@@ -28,17 +27,6 @@ public class UsersRepository extends Repository<User> ...@@ -28,17 +27,6 @@ public class UsersRepository extends Repository<User>
} }
} }
public void add(User user)
{
datastore.save(user);
}
@Override
public void clean()
{
datastore.getCollection(User.class).drop();
}
public User getUserByName(String username) public User getUserByName(String username)
{ {
return datastore.createQuery(User.class) return datastore.createQuery(User.class)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment