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

refactor /solutions/my enpoint

parent 43a25a56
......@@ -15,7 +15,6 @@ import java.util.List;
public abstract class Task
{
@Id
//todo: use id to identify task?
protected ObjectId id;
@PublicProperty
protected String name;
......@@ -63,4 +62,29 @@ public abstract class Task
{
this.flags = flags;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Task task = (Task) o;
if (level != task.level) return false;
if (id != null ? !id.equals(task.id) : task.id != null) return false;
if (name != null ? !name.equals(task.name) : task.name != null) return false;
return flags != null ? flags.equals(task.flags) : task.flags == null;
}
@Override
public int hashCode()
{
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + level;
result = 31 * result + (flags != null ? flags.hashCode() : 0);
return result;
}
}
......@@ -7,7 +7,6 @@ import org.mongodb.morphia.Datastore;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@Singleton
......
......@@ -3,6 +3,7 @@ package com.telephoners.krakyournet.ctf.resources;
import com.telephoners.krakyournet.ctf.objects.Solution;
import com.telephoners.krakyournet.ctf.objects.Team;
import com.telephoners.krakyournet.ctf.objects.User;
import com.telephoners.krakyournet.ctf.objects.tasks.Task;
import com.telephoners.krakyournet.ctf.repositories.SolutionsRepository;
import com.telephoners.krakyournet.ctf.repositories.TasksRepository;
import com.telephoners.krakyournet.ctf.repositories.TeamsRepository;
......@@ -52,10 +53,28 @@ public class SolutionsResource
@GET
@Path("/my")
public List<Solution> getTeamSolutions(@Auth User user)
public Map<Integer, List<String>> getTeamSolutions(@Auth User user)
{
Optional<Team> team = teamsRepository.getTeamByUser(user);
return solutionsRepository.getByTeam(team.get()); //todo: per task
List<Solution> byTeam = solutionsRepository.getByTeam(team.get());
Map<Task, List<Solution>> collect = byTeam.stream()
.collect(Collectors.groupingBy(new Function<Solution, Task>()
{
@Override
public Task apply(Solution solution)
{
return solution.getTask();
}
}));
Map<Integer, List<String>> collect1 = collect.entrySet().stream()
.collect(Collectors.toMap(
taskSolutions -> taskSolutions.getKey().getLevel(),
taskSolutions -> taskSolutions.getValue().stream()
.map(solution -> solution.getFlag().getDescription()).collect(Collectors.toList())
));
return collect1;
}
@GET
......
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