Commit 1d59a411 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

lots of fixes

parent 2f110b49
...@@ -134,7 +134,6 @@ public class CTFApplication extends Application<ApplicationConfiguration> { ...@@ -134,7 +134,6 @@ public class CTFApplication extends Application<ApplicationConfiguration> {
} }
//todo: inject dbonnector
private Injector createInjector(ApplicationConfiguration applicationConfiguration, Datastore datastore) { private Injector createInjector(ApplicationConfiguration applicationConfiguration, Datastore datastore) {
return Guice.createInjector(new AbstractModule() { return Guice.createInjector(new AbstractModule() {
@Override @Override
...@@ -145,7 +144,8 @@ public class CTFApplication extends Application<ApplicationConfiguration> { ...@@ -145,7 +144,8 @@ public class CTFApplication extends Application<ApplicationConfiguration> {
teamsRepository = new TeamsRepository(datastore); teamsRepository = new TeamsRepository(datastore);
usersRepository = new UsersRepository(datastore, teamsRepository); usersRepository = new UsersRepository(datastore, teamsRepository);
SolutionsRepository solutionsRepository = new SolutionsRepository(datastore); SolutionsRepository solutionsRepository = new SolutionsRepository(datastore);
tasksRepository = new TasksRepository(applicationConfiguration, datastore, teamsRepository, solutionsRepository); tasksRepository = new TasksRepository(applicationConfiguration, datastore, teamsRepository, solutionsRepository,
usersRepository);
bind(TeamsRepository.class).toInstance(teamsRepository); bind(TeamsRepository.class).toInstance(teamsRepository);
bind(TasksRepository.class).toInstance(tasksRepository); bind(TasksRepository.class).toInstance(tasksRepository);
......
...@@ -9,9 +9,6 @@ import javax.ws.rs.Path; ...@@ -9,9 +9,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/** /**
* Created by gpietrus on 16.02.16. * Created by gpietrus on 16.02.16.
...@@ -30,9 +27,10 @@ public class SolutionsResource { ...@@ -30,9 +27,10 @@ public class SolutionsResource {
@GET @GET
@Path("/all") @Path("/all")
//todo: refactor mapping //todo: refactor mapping
public Map<String, List<String>> getTeamsSolutions() { public List<Solution> getTeamsSolutions() {
return solutionsRepository.getAll();
//todo: general refactor //todo: general refactor
return solutionsRepository.getAll() /*return solutionsRepository.getAll()
.stream() .stream()
.collect(Collectors.groupingBy(new Function<Solution, String>() { .collect(Collectors.groupingBy(new Function<Solution, String>() {
@Override @Override
...@@ -45,9 +43,9 @@ public class SolutionsResource { ...@@ -45,9 +43,9 @@ public class SolutionsResource {
.collect(Collectors.toMap( .collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getKey,
entry -> entry.getValue().stream() entry -> entry.getValue().stream()
.map(Solution::getTeamId) .map(Solution::getTeam)
.collect(Collectors.toList()) .collect(Collectors.toList())
)); ));*/
} }
} }
package objects; package objects;
import com.google.common.collect.ImmutableMap;
import org.bson.Document;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity; import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import java.util.Map;
/** /**
* Created by gpietrus on 20.02.2016. * Created by gpietrus on 20.02.2016.
*/ */
@Entity("solutions") @Entity("solutions")
public class Solution { public class Solution {
@Id
private ObjectId id; private ObjectId id;
private String taskName; private String taskName;
private String teamId; private Team team;
public Solution(String teamId, String taskName) { public Solution(Team team, String taskName) {
this.teamId = teamId; this.team = team;
this.taskName = taskName; this.taskName = taskName;
} }
public Solution() {} public Solution() {
}
public String getTaskName() { public void setTaskName(String taskName) {
return taskName; this.taskName = taskName;
} }
public String getTeamId() { public void setTeam(Team team) {
return teamId; this.team = team;
} }
public Solution(Document document) { public String getTaskName() {
this.taskName = document.get("taskName").toString(); //todo: refactor mapping return taskName;
this.teamId = document.get("teamId").toString();
} }
//todo: refactor mapping public Team getTeam() {
public Map<String, String> toMap() { return team;
return ImmutableMap.<String, String>builder()
.put("teamId",teamId)
.put("taskName", taskName)
.build();
} }
} }
package repositories; package repositories;
import core.ApplicationConfiguration; import core.ApplicationConfiguration;
import objects.Solution;
import objects.Task; import objects.Task;
import objects.Team;
import objects.User;
import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.Hex;
import org.mongodb.morphia.Datastore; import org.mongodb.morphia.Datastore;
...@@ -23,15 +26,18 @@ public class TasksRepository implements Repository { ...@@ -23,15 +26,18 @@ public class TasksRepository implements Repository {
private Datastore datastore; private Datastore datastore;
private TeamsRepository teamsRepository; private TeamsRepository teamsRepository;
private SolutionsRepository solutionsRepository; private SolutionsRepository solutionsRepository;
private UsersRepository usersRepository;
private String salt = "SECURE_SALT"; //todo private String salt = "SECURE_SALT"; //todo
@Inject @Inject
public TasksRepository(ApplicationConfiguration applicationConfiguration, Datastore datastore, public TasksRepository(ApplicationConfiguration applicationConfiguration, Datastore datastore,
TeamsRepository teamsRepository, SolutionsRepository solutionsRepository) { TeamsRepository teamsRepository, SolutionsRepository solutionsRepository,
UsersRepository usersRepository) {
this.applicationConfiguration = applicationConfiguration; this.applicationConfiguration = applicationConfiguration;
this.datastore = datastore; this.datastore = datastore;
this.teamsRepository = teamsRepository; this.teamsRepository = teamsRepository;
this.solutionsRepository = solutionsRepository; this.solutionsRepository = solutionsRepository;
this.usersRepository = usersRepository;
} }
public void get(UUID uuid) { public void get(UUID uuid) {
...@@ -71,8 +77,11 @@ public class TasksRepository implements Repository { ...@@ -71,8 +77,11 @@ public class TasksRepository implements Repository {
} }
private void acceptSolution(String username, Task task) { private void acceptSolution(String username, Task task) {
//todo: getname, of get id? User user = usersRepository.getUserByName(username);
// solutionsRepository.add(new Solution(teamsRepository.getTeamByUser(username).getName(), task.getName())); //tidod Optional<Team> team = teamsRepository.getTeamByUser(user);
if(team.isPresent()) {
solutionsRepository.add(new Solution(team.get(),task.getName()));
}
} }
private boolean compareHash(String hash, String username) throws Exception { private boolean compareHash(String hash, String username) throws Exception {
......
package repositories; package repositories;
import objects.Team; import objects.Team;
import objects.User;
import org.mongodb.morphia.Datastore; import org.mongodb.morphia.Datastore;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
/** /**
...@@ -19,15 +21,10 @@ public class TeamsRepository implements Repository { ...@@ -19,15 +21,10 @@ public class TeamsRepository implements Repository {
this.datastore = datastore; this.datastore = datastore;
} }
public Team getTeamByUser(String username) { public Optional<Team> getTeamByUser(User user) {
return null; return datastore.createQuery(Team.class).asList().stream()
/* return getAll().stream() .filter(team -> team.getMembers().contains(user))
.filter(team -> team.getMembers().stream() .findFirst();
.map(User::getName)
.collect(Collectors.toList())
.contains(username))
.findFirst()
.get();*/
} }
public void get(UUID uuid) { public void get(UUID uuid) {
......
...@@ -29,15 +29,11 @@ public class UsersRepository implements Repository { ...@@ -29,15 +29,11 @@ public class UsersRepository implements Repository {
} }
} }
// public Optional<User> get(String username) { public User getUserByName(String username) {
// Optional<User> userOptional = teamsRepository.getAll() return datastore.createQuery(User.class)
// .stream() .field("name").equal(username)
// .map(Team::getMembers) .get();
// .flatMap(Collection::stream) }
// .filter(user -> user.getName().equals(username))
// .findFirst();
// return userOptional;
// }
public User authenticateUser(BasicCredentials basicCredentials) { public User authenticateUser(BasicCredentials basicCredentials) {
...@@ -51,22 +47,4 @@ public class UsersRepository implements Repository { ...@@ -51,22 +47,4 @@ public class UsersRepository implements Repository {
public List getAll() { public List getAll() {
return null; return null;
} }
// public void get(UUID uuid) {
// }
// public List<User> getAll() {
// return mongoDBConnector.getCollection("users")
// .stream()
// .map((Function<Document, User>) User::new)
// .collect(Collectors.toList());
// }
// public void add(User user) {
// mongoDBConnector.addDocument("users", new Document(user.toMap()));
// }
// public void clean() {
// mongoDBConnector.removeCollection("users");
// }
} }
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