@@ -5,14 +5,20 @@ import (
55 "encoding/json"
66 "fmt"
77 "io"
8+ "log"
9+ "math/big"
810 "net/http"
11+ "strconv"
912 "strings"
1013 "time"
1114
15+ "github.com/CodeChefVIT/cookoff-backend/internal/db"
16+ "github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
1217 httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
1318 logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
1419 "github.com/CodeChefVIT/cookoff-backend/internal/helpers/submission"
15- "github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
20+ "github.com/go-chi/chi/v5"
21+ "github.com/jackc/pgx/v5/pgtype"
1622
1723 "github.com/google/uuid"
1824)
@@ -26,17 +32,8 @@ func GetResult(w http.ResponseWriter, r *http.Request) {
2632 defer cancel ()
2733
2834 var req resultreq
29- err := httphelpers .ParseJSON (r , & req )
30- if err != nil {
31- httphelpers .WriteError (w , http .StatusBadRequest , "Invalid request payload" )
32- return
33- }
34-
35- if err := validator .ValidatePayload (w , req ); err != nil {
36- httphelpers .WriteError (w , http .StatusNotAcceptable , "Please provide values for all required fields." )
37- return
38- }
3935
36+ req .SubID = chi .URLParam (r , "submission_id" )
4037 subid , err := uuid .Parse (req .SubID )
4138 if err != nil {
4239 httphelpers .WriteError (w , http .StatusBadRequest , "Invalid UUID format" )
@@ -59,7 +56,7 @@ func GetResult(w http.ResponseWriter, r *http.Request) {
5956 return
6057 }
6158
62- ticker := time .NewTicker (20 * time .Second )
59+ ticker := time .NewTicker (10 * time .Second )
6360 defer ticker .Stop ()
6461
6562 for {
@@ -184,8 +181,72 @@ func BadCodeAlert(ctx context.Context, id uuid.UUID, w http.ResponseWriter) erro
184181 for _ , v := range temp .Submissions {
185182 if v .Status .ID != 1 || v .Status .ID != 2 {
186183 submission .Tokens .DeleteToken (ctx , v .Token )
184+ _ , testcase , err := submission .GetSubID (ctx , v .Token )
185+ if err != nil {
186+ fmt .Println ("Failed to get details from redis:" , err )
187+ httphelpers .WriteError (w , http .StatusInternalServerError , "Internal server error!(Failed to get from redis)" )
188+ return err
189+ }
190+ timeValue , err := parseTime (* v .Time )
191+ if err != nil {
192+ fmt .Println ("Error converting to float:" , err )
193+ httphelpers .WriteError (w , http .StatusInternalServerError , "Internal server error!(Failed to convert to float)" )
194+ return err
195+ }
196+ tid := uuid .MustParse (testcase )
197+ switch v .Status .ID {
198+ case 3 :
199+ err = HandleCompilationError (ctx , id , v , int (timeValue * 1000 ), tid , "success" )
200+ case 4 :
201+ err = HandleCompilationError (ctx , id , v , int (timeValue * 1000 ), tid , "wrong answer" )
202+ case 6 :
203+ err = HandleCompilationError (ctx , id , v , int (timeValue * 1000 ), tid , "Compilation error" )
204+ case 11 :
205+ err = HandleCompilationError (ctx , id , v , int (timeValue * 1000 ), tid , "Runtime error" )
206+ }
207+ if err != nil {
208+ fmt .Println ("Failed to add submission_results" )
209+ }
187210 }
188211 }
189212
190213 return nil
191214}
215+
216+ func parseTime (timeStr string ) (float64 , error ) {
217+ if timeStr == "" {
218+ log .Println ("Time value is empty, setting time to 0 for this submission." )
219+ return 0 , nil
220+ }
221+
222+ timeValue , err := strconv .ParseFloat (timeStr , 64 )
223+ if err != nil {
224+ return 0 , err
225+ }
226+ return timeValue , nil
227+ }
228+
229+ func HandleCompilationError (ctx context.Context , idUUID uuid.UUID , data GetSub , time int , testcase uuid.UUID , status string ) error {
230+ subID , err := uuid .NewV7 ()
231+
232+ if err != nil {
233+ log .Println ("Error updating submission for compilation error: " , err )
234+ return err
235+ }
236+
237+ err = database .Queries .CreateSubmissionStatus (ctx , db.CreateSubmissionStatusParams {
238+ ID : subID ,
239+ SubmissionID : idUUID ,
240+ TestcaseID : uuid.NullUUID {UUID : testcase , Valid : true },
241+ Runtime : pgtype.Numeric {Int : big .NewInt (int64 (time )), Valid : true },
242+ Memory : pgtype.Numeric {Int : big .NewInt (int64 (* data .Memory )), Valid : true },
243+ Description : & data .Status .Description ,
244+ Status : status ,
245+ })
246+
247+ if err != nil {
248+ log .Println ("Error creating submission status error: " , err )
249+ return err
250+ }
251+ return nil
252+ }
0 commit comments