Skip to content

Commit 4015266

Browse files
committed
fix: backend history sorting
1 parent a3bcb12 commit 4015266

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

apps/history-service/handlers/listquestionhistory.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"encoding/json"
55
"history-service/models"
66
"net/http"
7+
"sort"
78

8-
"cloud.google.com/go/firestore"
99
"github.com/go-chi/chi/v5"
1010
"google.golang.org/api/iterator"
1111
)
@@ -23,12 +23,12 @@ func (s *Service) ListUserQuestionHistories(w http.ResponseWriter, r *http.Reque
2323
// Query data
2424
iterUser := collRef.Where("user", "==", username).
2525
Where("questionDocRefId", "==", questionDocRefID).
26-
OrderBy("createdAt", firestore.Desc).
2726
Documents(ctx)
27+
defer iterUser.Stop()
2828
iterMatchedUser := collRef.Where("matchedUser", "==", username).
2929
Where("questionDocRefId", "==", questionDocRefID).
30-
OrderBy("createdAt", firestore.Desc).
3130
Documents(ctx)
31+
defer iterMatchedUser.Stop()
3232

3333
// Map data
3434
var histories []models.CollaborationHistory
@@ -76,6 +76,9 @@ func (s *Service) ListUserQuestionHistories(w http.ResponseWriter, r *http.Reque
7676
histories = append(histories, history)
7777
}
7878

79+
// Sort the histories by created at time
80+
sort.Sort(models.HistorySorter(histories))
81+
7982
w.Header().Set("Content-Type", "application/json")
8083
w.WriteHeader(http.StatusOK)
8184
json.NewEncoder(w).Encode(histories)

apps/history-service/handlers/listuserhistory.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"encoding/json"
55
"history-service/models"
66
"net/http"
7+
"sort"
78

8-
"cloud.google.com/go/firestore"
99
"github.com/go-chi/chi/v5"
1010
"google.golang.org/api/iterator"
1111
)
@@ -21,11 +21,11 @@ func (s *Service) ListUserHistories(w http.ResponseWriter, r *http.Request) {
2121

2222
// Query data
2323
iterUser := collRef.Where("user", "==", username).
24-
OrderBy("createdAt", firestore.Desc).
2524
Documents(ctx)
25+
defer iterUser.Stop()
2626
iterMatchedUser := collRef.Where("matchedUser", "==", username).
27-
OrderBy("createdAt", firestore.Desc).
2827
Documents(ctx)
28+
defer iterUser.Stop()
2929

3030
// Map data
3131
var histories []models.CollaborationHistory
@@ -73,6 +73,9 @@ func (s *Service) ListUserHistories(w http.ResponseWriter, r *http.Request) {
7373
histories = append(histories, history)
7474
}
7575

76+
// Sort the histories by created at time
77+
sort.Sort(models.HistorySorter(histories))
78+
7679
w.Header().Set("Content-Type", "application/json")
7780
w.WriteHeader(http.StatusOK)
7881
json.NewEncoder(w).Encode(histories)

apps/history-service/models/models.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ type CollaborationHistory struct {
2323
UpdatedAt time.Time `json:"updatedAt" firestore:"updatedAt"` // updatedAt is unused as history is never updated once created
2424
HistoryDocRefID string `json:"historyDocRefId"`
2525
}
26+
27+
// Sorting interface for history, which sorts by created at in desc order
28+
type HistorySorter []CollaborationHistory
29+
30+
func (s HistorySorter) Len() int { return len(s) }
31+
func (s HistorySorter) Less(i, j int) bool {
32+
return s[i].CreatedAt.After(s[j].CreatedAt)
33+
}
34+
func (s HistorySorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

0 commit comments

Comments
 (0)