Skip to content

Commit 8cb36dc

Browse files
🚧 Init stats ressources
1 parent c2a70c8 commit 8cb36dc

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/main/java/zenika/oss/stats/ressources/ContributionsRessources.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
import jakarta.ws.rs.Path;
66
import jakarta.ws.rs.PathParam;
77
import jakarta.ws.rs.core.Response;
8+
import zenika.oss.stats.exception.DatabaseException;
9+
import zenika.oss.stats.services.FirestoreServices;
810

911
@ApplicationScoped
1012
@Path("/v1/contributions/")
1113
public class ContributionsRessources {
14+
private final FirestoreServices firestoreServices;
15+
16+
@jakarta.inject.Inject
17+
public ContributionsRessources(FirestoreServices firestoreServices) {
18+
this.firestoreServices = firestoreServices;
19+
}
20+
1221
/**
1322
* Get contributions for one member.
1423
*
@@ -17,11 +26,8 @@ public class ContributionsRessources {
1726
*/
1827
@GET
1928
@Path("/contributions/member/{memberId}")
20-
public Response getContributionsByMember(@PathParam("memberId") String memberId) {
21-
22-
return Response.ok()
23-
.build();
24-
29+
public Response getContributionsByMember(@PathParam("memberId") String memberId) throws DatabaseException {
30+
return Response.ok(firestoreServices.getContributionsForAMemberOrderByYear(memberId)).build();
2531
}
2632

2733
@GET

src/main/java/zenika/oss/stats/services/FirestoreServices.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,22 @@ public <T> void deleteAllDocuments(FirestoreCollections collectionType) throws D
161161
throw new DatabaseException(exception);
162162
}
163163
}
164+
165+
public List<StatsContribution> getContributionsForAMemberOrderByYear(String memberId) throws DatabaseException {
166+
List<StatsContribution> stats = null;
167+
CollectionReference zStats = firestore.collection(FirestoreCollections.STATS.value);
168+
Query query = zStats.whereEqualTo("githubHandle", memberId);
169+
ApiFuture<QuerySnapshot> querySnapshot = query.get();
170+
try {
171+
stats = querySnapshot.get().getDocuments().stream()
172+
.map(document -> document.toObject(StatsContribution.class))
173+
.sorted((s1, s2) -> s2.getYear().compareTo(s1.getYear()))
174+
.sorted((s1, s2) -> s2.getMonth().compareTo(s1.getMonth()))
175+
.collect(java.util.stream.Collectors.toList());
176+
} catch (InterruptedException | ExecutionException exception) {
177+
throw new DatabaseException(exception);
178+
}
179+
180+
return stats;
181+
}
164182
}

0 commit comments

Comments
 (0)