|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "execution-service/models" |
| 6 | + "execution-service/utils" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/go-chi/chi/v5" |
| 10 | + "google.golang.org/api/iterator" |
| 11 | +) |
| 12 | + |
| 13 | +func (s *Service) ReadHiddenTests(w http.ResponseWriter, r *http.Request) { |
| 14 | + ctx := r.Context() |
| 15 | + |
| 16 | + questionDocRefId := chi.URLParam(r, "questionDocRefId") |
| 17 | + if questionDocRefId == "" { |
| 18 | + http.Error(w, "questionDocRefId is required", http.StatusBadRequest) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + iter := s.Client.Collection("tests").Where("questionDocRefId", "==", questionDocRefId).Limit(1).Documents(ctx) |
| 23 | + doc, err := iter.Next() |
| 24 | + if err != nil { |
| 25 | + if err == iterator.Done { |
| 26 | + http.Error(w, "Test not found", http.StatusNotFound) |
| 27 | + return |
| 28 | + } |
| 29 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 30 | + return |
| 31 | + } |
| 32 | + defer iter.Stop() |
| 33 | + |
| 34 | + var test models.Test |
| 35 | + if err := doc.DataTo(&test); err != nil { |
| 36 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + _, hiddenTestCases, err := utils.GetTestLengthAndUnexecutedCases(test.HiddenTestCases) |
| 41 | + |
| 42 | + var hiddenTests []models.HiddenTest |
| 43 | + for _, hiddenTestCase := range hiddenTestCases { |
| 44 | + hiddenTests = append(hiddenTests, models.HiddenTest{ |
| 45 | + Input: hiddenTestCase.Input, |
| 46 | + Expected: hiddenTestCase.Expected, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + w.Header().Set("Content-Type", "application/json") |
| 51 | + w.WriteHeader(http.StatusOK) |
| 52 | + json.NewEncoder(w).Encode(hiddenTests) |
| 53 | +} |
| 54 | + |
| 55 | +//curl -X GET http://localhost:8083/tests/bmzFyLMeSOoYU99pi4yZ/ \ |
| 56 | +//-H "Content-Type: application/json" |
0 commit comments