|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "cloud.google.com/go/firestore" |
| 5 | + "encoding/json" |
| 6 | + "execution-service/models" |
| 7 | + "execution-service/utils" |
| 8 | + "google.golang.org/api/iterator" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) { |
| 13 | + ctx := r.Context() |
| 14 | + |
| 15 | + var test models.Test |
| 16 | + if err := utils.DecodeJSONBody(w, r, &test); err != nil { |
| 17 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + // Basic validation for question title and question ID |
| 22 | + if test.QuestionDocRefId == "" || test.QuestionTitle == "" { |
| 23 | + http.Error(w, "QuestionDocRefId and QuestionTitle are required", http.StatusBadRequest) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + // Automatically populate validation for input and output in test case |
| 28 | + test.InputValidation = utils.GetDefaultValidation() |
| 29 | + test.OutputValidation = utils.GetDefaultValidation() |
| 30 | + |
| 31 | + // Validate test case format |
| 32 | + if _, err := utils.ValidateTestCaseFormat(test.VisibleTestCases, test.InputValidation, |
| 33 | + test.OutputValidation); err != nil { |
| 34 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 35 | + return |
| 36 | + } |
| 37 | + if _, err := utils.ValidateTestCaseFormat(test.HiddenTestCases, test.InputValidation, |
| 38 | + test.OutputValidation); err != nil { |
| 39 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + // Save test to Firestore |
| 44 | + docRef, _, err := s.Client.Collection("tests").Add(ctx, map[string]interface{}{ |
| 45 | + "questionDocRefId": test.QuestionDocRefId, |
| 46 | + "questionTitle": test.QuestionTitle, |
| 47 | + "visibleTestCases": test.VisibleTestCases, |
| 48 | + "hiddenTestCases": test.HiddenTestCases, |
| 49 | + "inputValidation": test.InputValidation, |
| 50 | + "outputValidation": test.OutputValidation, |
| 51 | + "createdAt": firestore.ServerTimestamp, |
| 52 | + "updatedAt": firestore.ServerTimestamp, |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // Get data |
| 60 | + doc, err := docRef.Get(ctx) |
| 61 | + if err != nil { |
| 62 | + if err != iterator.Done { |
| 63 | + http.Error(w, "Test not found", http.StatusInternalServerError) |
| 64 | + return |
| 65 | + } |
| 66 | + http.Error(w, "Failed to get test", http.StatusInternalServerError) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Map data |
| 71 | + if err := doc.DataTo(&test); err != nil { |
| 72 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + w.Header().Set("Content-Type", "application/json") |
| 77 | + w.WriteHeader(http.StatusCreated) |
| 78 | + json.NewEncoder(w).Encode(test) |
| 79 | +} |
| 80 | + |
| 81 | +// Manual test cases |
| 82 | + |
| 83 | +//curl -X POST http://localhost:8083/tests \ |
| 84 | +//-H "Content-Type: application/json" \ |
| 85 | +//-d '{ |
| 86 | +//"questionDocRefId": "sampleDocRefId123", |
| 87 | +//"questionTitle": "Sample Question Title", |
| 88 | +//"visibleTestCases": "2\nhello\nolleh\nHannah\nhannaH", |
| 89 | +//"hiddenTestCases": "2\nHannah\nhannaH\nabcdefg\ngfedcba" |
| 90 | +//}' |
0 commit comments