-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(domain): Developed Exercise Solving Functionality
Separated exercise domain layer from course layer, separated models, added routes to initialization, removed `noctx` linter due to unnecessary warnings. Added variables to .env.template Referred issue: #4
- Loading branch information
1 parent
02b633d
commit 0e8a478
Showing
12 changed files
with
317 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package exercise | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
"io" | ||
"net/http" | ||
"rpl-service/models" | ||
"rpl-service/services/exercises" | ||
) | ||
|
||
/** | ||
{ | ||
"exerciseID": "...", | ||
"code" : "..." | ||
} | ||
*/ | ||
|
||
func SolveExercise(w http.ResponseWriter, r *http.Request, db *gorm.DB) { | ||
// With exerciseId, I should search for all its tests, run one by one, and then send the result of each one as JSONs | ||
exerciseID, err := uuid.Parse(r.PathValue("exerciseId")) | ||
if err != nil { | ||
http.Error(w, "Invalid exercise ID format", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var exercise models.Exercise | ||
db.Model(models.Exercise{}).Where(&exercise, "ID = ?", exerciseID) | ||
|
||
// TODO: make a more valid check, exercise.ID is currently a uint, don't know why | ||
// TODO 2: we may need to remove the gorm.Model and manually declare each UUID as primary key | ||
if exercise.Name == "" { | ||
http.Error(w, "No exercise with that ID", http.StatusNotFound) | ||
return | ||
} | ||
var response models.SolveExerciseResponse | ||
body, readErr := io.ReadAll(r.Body) | ||
if readErr != nil { | ||
http.Error(w, "Invalid body format", http.StatusBadRequest) | ||
return | ||
} | ||
if respErr := json.Unmarshal(body, &response); respErr != nil { | ||
http.Error(w, "Invalid body format", http.StatusBadRequest) | ||
return | ||
} | ||
results, exerciseError := exercises.SolveExercise(exerciseID, db, response.ExerciseCode) | ||
if exerciseError != nil { | ||
http.Error(w, "Error while executing tests", http.StatusInternalServerError) | ||
return | ||
} | ||
byteResults, err := json.Marshal(results) //nolint:musttag // No need | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, writeErr := w.Write(byteResults) | ||
if writeErr != nil { | ||
return | ||
} | ||
} | ||
func CreateExercise(_ http.ResponseWriter, _ *http.Request, _ *gorm.DB) { | ||
// TODO | ||
} | ||
|
||
func FindExercise(_ http.ResponseWriter, _ *http.Request, _ *gorm.DB) { | ||
// TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package exercise | ||
|
||
import ( | ||
"rpl-service/models" | ||
) | ||
|
||
const BaseURL = "/exercise" // May change | ||
|
||
var SolveExerciseEndpoint = models.Endpoint{ | ||
Method: models.POST, | ||
Path: BaseURL + "/{exerciseId}", | ||
HandlerFunction: SolveExercise, | ||
IsProtected: true, | ||
} | ||
|
||
var CreateExerciseEndpoint = models.Endpoint{ | ||
Method: models.POST, | ||
Path: BaseURL, | ||
HandlerFunction: CreateExercise, | ||
IsProtected: true, | ||
} | ||
|
||
var GetExerciseEndpoint = models.Endpoint{ | ||
Method: models.GET, | ||
Path: BaseURL + "/{exerciseId}", | ||
HandlerFunction: FindExercise, | ||
IsProtected: true, | ||
} | ||
|
||
var Endpoints = []models.Endpoint{ | ||
SolveExerciseEndpoint, | ||
CreateExerciseEndpoint, | ||
GetExerciseEndpoint, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mappers | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"rpl-service/models" | ||
) | ||
|
||
func SolveExerciseRequestBody(exerciseCode, testScript string) io.Reader { | ||
return bytes.NewReader([]byte(`{"script":"` + exerciseCode + "\", \"tests\":" + testScript + `"`)) | ||
} | ||
|
||
func SolveExerciseResponseToResult(response *http.Response, exerciseName, testName string) models.ExerciseResult { | ||
var exerciseResponse struct { | ||
Stdout string `json:"stdout"` | ||
Stderr string `json:"stderr"` | ||
ReturnCode int `json:"returncode"` | ||
} | ||
err := json.NewDecoder(response.Body).Decode(&exerciseResponse) | ||
if err != nil { | ||
return models.ExerciseResult{} | ||
} | ||
|
||
return models.ExerciseResult{ | ||
ExerciseName: exerciseName, | ||
TestName: testName, | ||
TestPassed: exerciseResponse.ReturnCode == 0, | ||
Stdout: exerciseResponse.Stdout, | ||
Stderr: exerciseResponse.Stderr, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"github.com/lib/pq" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type SolveExerciseResponse struct { | ||
ExerciseCode string `json:"code"` | ||
} | ||
|
||
type ExerciseResult struct { | ||
ExerciseName string | ||
TestName string | ||
TestPassed bool | ||
Stdout string | ||
Stderr string | ||
} | ||
type Exercise struct { | ||
gorm.Model | ||
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()" json:"ID"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
BaseCode string `json:"base-code"` | ||
Points int `json:"points"` | ||
UnitNumber int `json:"unit_number"` | ||
TestIDs pq.StringArray `json:"testIDs" gorm:"type:text[]"` | ||
} | ||
|
||
type Test struct { | ||
gorm.Model | ||
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()" json:"ID"` | ||
Name string `json:"name"` | ||
TestScript string `json:"testScript"` | ||
} | ||
|
||
// TestDTO Should remove DTOs? | ||
type TestDTO struct { | ||
Name string `json:"name"` | ||
TestScript string `json:"testScript"` | ||
} | ||
|
||
// ExerciseDTO Should remove DTOs? | ||
type ExerciseDTO struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
BaseCode string `json:"base-code"` | ||
Points int `json:"points"` | ||
UnitNumber int `json:"unit_number"` | ||
TestData []TestDTO `json:"test-data"` | ||
} |
Oops, something went wrong.