|
| 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 | + // Normalise test cases |
| 28 | + test.VisibleTestCases = utils.NormaliseTestCaseFormat(test.VisibleTestCases) |
| 29 | + test.HiddenTestCases = utils.NormaliseTestCaseFormat(test.HiddenTestCases) |
| 30 | + |
| 31 | + // Automatically populate validation for input and output in test case |
| 32 | + test.InputValidation = utils.GetDefaultValidation() |
| 33 | + test.OutputValidation = utils.GetDefaultValidation() |
| 34 | + |
| 35 | + // Validate test case format |
| 36 | + if _, err := utils.ValidateTestCaseFormat(test.VisibleTestCases, test.InputValidation, |
| 37 | + test.OutputValidation); err != nil { |
| 38 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 39 | + return |
| 40 | + } |
| 41 | + if _, err := utils.ValidateTestCaseFormat(test.HiddenTestCases, test.InputValidation, |
| 42 | + test.OutputValidation); err != nil { |
| 43 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + // Check if a test already exists for the question |
| 48 | + iter := s.Client.Collection("tests").Where("questionDocRefId", "==", test.QuestionDocRefId).Documents(ctx) |
| 49 | + for { |
| 50 | + _, err := iter.Next() |
| 51 | + if err == iterator.Done { |
| 52 | + break |
| 53 | + } |
| 54 | + if err != nil { |
| 55 | + http.Error(w, "Error fetching test", http.StatusInternalServerError) |
| 56 | + return |
| 57 | + } |
| 58 | + http.Error(w, "Test already exists for the question", http.StatusConflict) |
| 59 | + return |
| 60 | + } |
| 61 | + defer iter.Stop() |
| 62 | + |
| 63 | + // Save test to Firestore |
| 64 | + docRef, _, err := s.Client.Collection("tests").Add(ctx, map[string]interface{}{ |
| 65 | + "questionDocRefId": test.QuestionDocRefId, |
| 66 | + "questionTitle": test.QuestionTitle, |
| 67 | + "visibleTestCases": test.VisibleTestCases, |
| 68 | + "hiddenTestCases": test.HiddenTestCases, |
| 69 | + "inputValidation": test.InputValidation, |
| 70 | + "outputValidation": test.OutputValidation, |
| 71 | + "createdAt": firestore.ServerTimestamp, |
| 72 | + "updatedAt": firestore.ServerTimestamp, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // Get data |
| 80 | + doc, err := docRef.Get(ctx) |
| 81 | + if err != nil { |
| 82 | + if err != iterator.Done { |
| 83 | + http.Error(w, "Test not found", http.StatusInternalServerError) |
| 84 | + return |
| 85 | + } |
| 86 | + http.Error(w, "Failed to get test", http.StatusInternalServerError) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + // Map data |
| 91 | + if err := doc.DataTo(&test); err != nil { |
| 92 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + w.Header().Set("Content-Type", "application/json") |
| 97 | + w.WriteHeader(http.StatusCreated) |
| 98 | + json.NewEncoder(w).Encode(test) |
| 99 | +} |
| 100 | + |
| 101 | +// Manual test cases |
| 102 | + |
| 103 | +//curl -X POST http://localhost:8083/tests \ |
| 104 | +//-H "Content-Type: application/json" \ |
| 105 | +//-d '{ |
| 106 | +//"questionDocRefId": "sampleDocRefId123", |
| 107 | +//"questionTitle": "Sample Question Title", |
| 108 | +//"visibleTestCases": "2\nhello\nolleh\nHannah\nhannaH", |
| 109 | +//"hiddenTestCases": "2\nHannah\nhannaH\nabcdefg\ngfedcba" |
| 110 | +//}' |
| 111 | + |
| 112 | +//curl -X POST http://localhost:8083/tests \ |
| 113 | +//-H "Content-Type: application/json" \ |
| 114 | +//-d "{ |
| 115 | +//\"questionDocRefId\": \"sampleDocRefId12345\", |
| 116 | +//\"questionTitle\": \"Sample Question Title\", |
| 117 | +//\"visibleTestCases\": \"2\\nhello\\nolleh\\nHannah\\nhannaH\", |
| 118 | +//\"hiddenTestCases\": \"2\\nHannah\\nhannaH\\nabcdefg\\ngfedcba\" |
| 119 | +//}" |
0 commit comments