Skip to content

Commit 5484438

Browse files
committed
feat; update history service model
1 parent 3b810cf commit 5484438

File tree

9 files changed

+39
-22
lines changed

9 files changed

+39
-22
lines changed

apps/history-service/handlers/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (s *Service) CreateHistory(w http.ResponseWriter, r *http.Request) {
2222
}
2323

2424
// Document reference ID in firestore mapped to the match ID in model
25-
docRef := s.Client.Collection("collaboration-history").Doc(collaborationHistory.MatchID)
25+
docRef := s.Client.Collection("collaboration-history").Doc(collaborationHistory.HistoryDocRefID)
2626

2727
_, err := docRef.Set(ctx, map[string]interface{}{
2828
"title": collaborationHistory.Title,
@@ -58,7 +58,7 @@ func (s *Service) CreateHistory(w http.ResponseWriter, r *http.Request) {
5858
http.Error(w, "Failed to map history data", http.StatusInternalServerError)
5959
return
6060
}
61-
collaborationHistory.MatchID = doc.Ref.ID
61+
collaborationHistory.HistoryDocRefID = doc.Ref.ID
6262

6363
w.Header().Set("Content-Type", "application/json")
6464
w.WriteHeader(http.StatusOK)

apps/history-service/handlers/createOrUpdate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *Service) CreateOrUpdateHistory(w http.ResponseWriter, r *http.Request)
6666
http.Error(w, "Failed to map history data", http.StatusInternalServerError)
6767
return
6868
}
69-
collaborationHistory.MatchID = doc.Ref.ID
69+
collaborationHistory.HistoryDocRefID = doc.Ref.ID
7070

7171
w.Header().Set("Content-Type", "application/json")
7272
w.WriteHeader(http.StatusOK)
@@ -118,7 +118,7 @@ func (s *Service) CreateOrUpdateHistory(w http.ResponseWriter, r *http.Request)
118118
http.Error(w, "Failed to map history data", http.StatusInternalServerError)
119119
return
120120
}
121-
collaborationHistory.MatchID = doc.Ref.ID
121+
collaborationHistory.HistoryDocRefID = doc.Ref.ID
122122

123123
w.Header().Set("Content-Type", "application/json")
124124
w.WriteHeader(http.StatusOK)

apps/history-service/handlers/listquestionhistory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *Service) ListUserQuestionHistories(w http.ResponseWriter, r *http.Reque
3939
http.Error(w, "Failed to map history data for user", http.StatusInternalServerError)
4040
return
4141
}
42-
history.MatchID = doc.Ref.ID
42+
history.HistoryDocRefID = doc.Ref.ID
4343

4444
histories = append(histories, history)
4545
}
@@ -59,7 +59,7 @@ func (s *Service) ListUserQuestionHistories(w http.ResponseWriter, r *http.Reque
5959
http.Error(w, "Failed to map history data for matched user", http.StatusInternalServerError)
6060
return
6161
}
62-
history.MatchID = doc.Ref.ID
62+
history.HistoryDocRefID = doc.Ref.ID
6363

6464
histories = append(histories, history)
6565
}

apps/history-service/handlers/listuserhistory.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package handlers
22

33
import (
44
"encoding/json"
5-
"github.com/go-chi/chi/v5"
6-
"google.golang.org/api/iterator"
75
"history-service/models"
86
"net/http"
7+
8+
"github.com/go-chi/chi/v5"
9+
"google.golang.org/api/iterator"
910
)
1011

1112
func (s *Service) ListUserHistories(w http.ResponseWriter, r *http.Request) {
@@ -38,7 +39,7 @@ func (s *Service) ListUserHistories(w http.ResponseWriter, r *http.Request) {
3839
http.Error(w, "Failed to map history data for user", http.StatusInternalServerError)
3940
return
4041
}
41-
history.MatchID = doc.Ref.ID
42+
history.HistoryDocRefID = doc.Ref.ID
4243

4344
histories = append(histories, history)
4445
}
@@ -58,7 +59,7 @@ func (s *Service) ListUserHistories(w http.ResponseWriter, r *http.Request) {
5859
http.Error(w, "Failed to map history data for matched user", http.StatusInternalServerError)
5960
return
6061
}
61-
history.MatchID = doc.Ref.ID
62+
history.HistoryDocRefID = doc.Ref.ID
6263

6364
histories = append(histories, history)
6465
}

apps/history-service/handlers/read.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
func (s *Service) ReadHistory(w http.ResponseWriter, r *http.Request) {
1414
ctx := r.Context()
1515

16-
matchId := chi.URLParam(r, "matchId")
16+
historyDocRefId := chi.URLParam(r, "historyDocRefId")
1717

1818
// Reference document
19-
docRef := s.Client.Collection("collaboration-history").Doc(matchId)
19+
docRef := s.Client.Collection("collaboration-history").Doc(historyDocRefId)
2020

2121
// Get data
2222
doc, err := docRef.Get(ctx)
@@ -35,7 +35,7 @@ func (s *Service) ReadHistory(w http.ResponseWriter, r *http.Request) {
3535
http.Error(w, "Failed to map history data", http.StatusInternalServerError)
3636
return
3737
}
38-
collaborationHistory.MatchID = doc.Ref.ID
38+
collaborationHistory.HistoryDocRefID = doc.Ref.ID
3939

4040
w.Header().Set("Content-Type", "application/json")
4141
w.WriteHeader(http.StatusOK)

apps/history-service/handlers/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (s *Service) UpdateHistory(w http.ResponseWriter, r *http.Request) {
6969
http.Error(w, "Failed to map history data", http.StatusInternalServerError)
7070
return
7171
}
72-
updatedHistory.MatchID = doc.Ref.ID
72+
updatedHistory.HistoryDocRefID = doc.Ref.ID
7373

7474
w.Header().Set("Content-Type", "application/json")
7575
w.WriteHeader(http.StatusOK)

apps/history-service/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func initChiRouter(service *handlers.Service) *chi.Mux {
7777
func registerRoutes(r *chi.Mux, service *handlers.Service) {
7878
r.Route("/histories", func(r chi.Router) {
7979
r.Post("/", service.CreateHistory)
80+
r.Get("/{historyDocRefId}", service.ReadHistory)
8081
r.Route("/{username}", func(r chi.Router) {
8182
r.Get("/", service.ListUserHistories)
8283
r.Get("/{questionDocRefId}", service.ListUserQuestionHistories)

apps/history-service/models/models.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ package models
33
import "time"
44

55
type CollaborationHistory struct {
6+
// Submission related details
7+
Code string `json:"code" firestore:"code"`
8+
Language string `json:"language" firestore:"language"`
9+
10+
// Match related details
11+
User string `json:"user" firestore:"user"`
12+
MatchedUser string `json:"matchedUser" firestore:"matchedUser"`
13+
MatchedTopics []string `json:"matchedTopics" firestore:"matchedTopics"`
14+
15+
// Question related details
616
Title string `json:"title" firestore:"title"`
7-
Code string `json:"code" firestore:"code"`
8-
Language string `json:"language" firestore:"language"`
9-
User string `json:"user" firestore:"user"`
10-
MatchedUser string `json:"matchedUser" firestore:"matchedUser"`
11-
MatchID string `json:"matchId" firestore:"matchId"`
12-
MatchedTopics []string `json:"matchedTopics" firestore:"matchedTopics"`
1317
QuestionDocRefID string `json:"questionDocRefId" firestore:"questionDocRefId"`
1418
QuestionDifficulty string `json:"questionDifficulty" firestore:"questionDifficulty"`
1519
QuestionTopics []string `json:"questionTopics" firestore:"questionTopics"`
1620

1721
// Special DB fields
18-
CreatedAt time.Time `json:"createdAt" firestore:"createdAt"`
19-
UpdatedAt time.Time `json:"updatedAt" firestore:"updatedAt"`
22+
CreatedAt time.Time `json:"createdAt" firestore:"createdAt"`
23+
UpdatedAt time.Time `json:"updatedAt" firestore:"updatedAt"` // updatedAt is unused as history is never updated once created
24+
HistoryDocRefID string `json:"historyDocRefId"`
2025
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package models
2+
3+
type HistoriesResponse struct {
4+
TotalCount int `json:"totalCount"`
5+
TotalPages int `json:"totalPages"`
6+
CurrentPage int `json:"currentPage"`
7+
Limit int `json:"limit"`
8+
HasNextPage bool `json:"hasNextPage"`
9+
Questions []CollaborationHistory `json:"histories"`
10+
}

0 commit comments

Comments
 (0)