Skip to content

Commit

Permalink
implemented module/grades response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
beebeeoii committed Jan 22, 2022
1 parent 71887d1 commit 980789d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/api/grades.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ type Grade struct {

const GRADE_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/gradebook/?populate=scores&ParentID=%s"

// getGradeFieldsRequired is a helper function that returns a constant array with fields that a Grade response
// returned by Luminus needs.
func getGradeFieldsRequired() []string {
return []string{"access", "scores", "name", "maxMark"}
}

// getScoreDetailFieldsRequired is a helper function that returns a constant array with fields that a Grade["scores"] element
// returned by Luminus needs.
func getScoreDetailFieldsRequired() []string {
return []string{"finalMark", "remark", "lastUpdatedDate", "maxMark"}
}

// GetGrades retrieves all grades for a particular module represented by moduleCode specified in GradeRequest.
// Find out more about GradeRequests under request.go.
func (req GradeRequest) GetGrades() ([]Grade, error) {
Expand All @@ -28,10 +40,17 @@ func (req GradeRequest) GetGrades() ([]Grade, error) {
}

for _, content := range rawResponse.Data {
if !IsResponseValid(getGradeFieldsRequired(), content) {
continue
}

if _, exists := content["access"]; exists { // only grades that can be accessed will be placed in grades slice
scoreDetail := make(map[string]interface{})
if len(content["scores"].([]interface{})) > 0 {
scoreDetail = (content["scores"].([]interface{})[0]).(map[string]interface{})
if !IsResponseValid(getScoreDetailFieldsRequired(), scoreDetail) {
continue
}
}
testName := content["name"].(string)
mark := -1.0
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ type Module struct {

const MODULE_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/module/?populate=Creator,termDetail,isMandatory"

// getModuleFieldsRequired is a helper function that returns a constant array with fields that a Module response
// returned by Luminus needs.
func getModuleFieldsRequired() []string {
return []string{"access", "termDetail", "courseName", "name", "creatorName", "creatorEmail"}
}

// getTermDetailFieldsRequired is a helper function that returns a constant array with fields that a Module["termDetail"]
// returned by Luminus needs.
func getTermDetailFieldsRequired() []string {
return []string{"description"}
}

// GetModules retrieves all modules that are taken by the user using a ModuleRequest.
// Find out more about ModuleRequests under request.go.
func (req ModuleRequest) GetModules() ([]Module, error) {
Expand All @@ -27,9 +39,17 @@ func (req ModuleRequest) GetModules() ([]Module, error) {
}

for _, content := range rawResponse.Data {
if !IsResponseValid(getModuleFieldsRequired(), content) {
continue
}

_, accessible := content["access"]
if accessible {
termDetail := content["termDetail"].(map[string]interface{})
if !IsResponseValid(getTermDetailFieldsRequired(), termDetail) {
continue
}

module := Module{
Id: content["id"].(string),
Name: content["courseName"].(string),
Expand Down

0 comments on commit 980789d

Please sign in to comment.