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