Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Get rubric by UUID #82

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions __tests__/integration/rubrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,104 @@ func GetCreatedRubrics(cookie *http.Cookie) (response map[string]interface{}, st

return ParseJsonResponse(w.Body), w.Code
}

func TestGetRubricByUUID(t *testing.T) {
c := require.New(t)

// Login as a teacher
w, r := PrepareRequest("POST", "/api/v1/session/login", map[string]interface{}{
"email": registeredTeacherEmail,
"password": registeredTeacherPass,
})
router.ServeHTTP(w, r)
cookie := w.Result().Cookies()[0]

// Create a rubric
response, status := CreateRubric(cookie, map[string]interface{}{
"name": "Rubric 1",
})
c.Equal(http.StatusCreated, status)
rubricUUID := response["uuid"].(string)

// Create a teacher
testTeacherEmail := "[email protected]"
testTeacherPass := "henriette/password/2020"
code := RegisterTeacherAccount(requests.RegisterTeacherRequest{
FullName: "Henriette Otylia",
Email: testTeacherEmail,
Password: testTeacherPass,
})
c.Equal(201, code)

// Login as the new teacher
w, r = PrepareRequest("POST", "/api/v1/session/login", map[string]interface{}{
"email": testTeacherEmail,
"password": testTeacherPass,
})
router.ServeHTTP(w, r)
cookie = w.Result().Cookies()[0]

// Create a rubric
response, status = CreateRubric(cookie, map[string]interface{}{
"name": "Rubric 2",
})
c.Equal(http.StatusCreated, status)
rubricUUID2 := response["uuid"].(string)

// Test cases
testCases := []GenericTestCase{
GenericTestCase{
Payload: map[string]interface{}{
"rubricUUID": "not-valid-uuid",
},
ExpectedStatusCode: http.StatusBadRequest,
},
GenericTestCase{
Payload: map[string]interface{}{
"rubricUUID": "90b2edf3-72fc-4682-be1c-1274c70785d9",
},
ExpectedStatusCode: http.StatusNotFound,
},
GenericTestCase{
Payload: map[string]interface{}{
"rubricUUID": rubricUUID,
},
ExpectedStatusCode: http.StatusForbidden,
},
GenericTestCase{
Payload: map[string]interface{}{
"rubricUUID": rubricUUID2,
},
ExpectedStatusCode: http.StatusOK,
},
}

for _, testCase := range testCases {
response, status := GetRubricByUUID(cookie, testCase.Payload["rubricUUID"].(string))
c.Equal(testCase.ExpectedStatusCode, status)

if testCase.ExpectedStatusCode == http.StatusOK {
rubric := response["rubric"].(map[string]interface{})
c.NotEmpty(response["message"])
c.Equal(rubricUUID2, rubric["uuid"])
c.Equal("Rubric 2", rubric["name"])

objective := rubric["objectives"].([]interface{})[0].(map[string]interface{})
c.NotEmpty(objective["uuid"])
c.NotEmpty(objective["description"])

criteria := objective["criteria"].([]interface{})[0].(map[string]interface{})
c.NotEmpty(criteria["uuid"])
c.NotEmpty(criteria["description"])
c.NotEmpty(criteria["weight"])
}
}
}

func GetRubricByUUID(cookie *http.Cookie, uuid string) (response map[string]interface{}, status int) {
w, r := PrepareRequest("GET", "/api/v1/rubrics/"+uuid, nil)
r.AddCookie(cookie)
router.ServeHTTP(w, r)

return ParseJsonResponse(w.Body), w.Code
}
9 changes: 8 additions & 1 deletion docs/openapi/spec.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,14 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/rubric"
type: object
properties:
message:
type: string
example: "Rubric was retrieved"
rubric:
allOf:
- $ref: "#/components/schemas/rubric"
"400":
description: Required fields were missed or doesn't fulfill the required format.
content:
Expand Down
16 changes: 16 additions & 0 deletions src/rubrics/application/use_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/UPB-Code-Labs/main-api/src/rubrics/domain/definitions"
"github.com/UPB-Code-Labs/main-api/src/rubrics/domain/dtos"
"github.com/UPB-Code-Labs/main-api/src/rubrics/domain/entities"
"github.com/UPB-Code-Labs/main-api/src/rubrics/domain/errors"
)

type RubricsUseCases struct {
Expand All @@ -27,3 +28,18 @@ func (useCases *RubricsUseCases) GetRubricsCreatedByTeacher(teacherUUID string)

return rubrics, nil
}

func (useCases *RubricsUseCases) GetRubricByUUID(dto *dtos.GetRubricDto) (rubric *entities.Rubric, err error) {
// Get the rubric
rubric, err = useCases.RubricsRepository.GetByUUID(dto.RubricUUID)
if err != nil {
return nil, err
}

// Check if the rubric belongs to the teacher
if rubric.TeacherUUID != dto.TeacherUUID {
return nil, &errors.TeacherDoesNotOwnsRubric{}
}

return rubric, nil
}
6 changes: 6 additions & 0 deletions src/rubrics/domain/dtos/get_rubric_by_uuid_dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dtos

type GetRubricDto struct {
TeacherUUID string
RubricUUID string
}
13 changes: 13 additions & 0 deletions src/rubrics/domain/errors/teacher_does_not_owns_rubric_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package errors

import "net/http"

type TeacherDoesNotOwnsRubric struct{}

func (err *TeacherDoesNotOwnsRubric) Error() string {
return "You do not own the rubric"
}

func (err *TeacherDoesNotOwnsRubric) StatusCode() int {
return http.StatusForbidden
}
31 changes: 31 additions & 0 deletions src/rubrics/infrastructure/http/http_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,34 @@ func (controller *RubricsController) HandleGetRubricsCreatedByTeacher(c *gin.Con
"message": "Rubrics were retrieved",
})
}

func (controller *RubricsController) HandleGetRubricByUUID(c *gin.Context) {
teacher_uuid := c.GetString("session_uuid")

// Validate rubric UUID
rubric_uuid := c.Param("rubricUUID")
if err := shared_infrastructure.GetValidator().Var(rubric_uuid, "uuid4"); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid rubric uuid",
})
return
}

// Create DTO
dto := dtos.GetRubricDto{
TeacherUUID: teacher_uuid,
RubricUUID: rubric_uuid,
}

// Get the rubric
rubric, err := controller.UseCases.GetRubricByUUID(&dto)
if err != nil {
c.Error(err)
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Rubric was retrieved",
"rubric": rubric,
})
}
7 changes: 7 additions & 0 deletions src/rubrics/infrastructure/http/http_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ func StartRubricsRoutes(g *gin.RouterGroup) {
shared_infrastructure.WithAuthorizationMiddleware([]string{"teacher"}),
controller.HandleGetRubricsCreatedByTeacher,
)

rubricsGroup.GET(
"/:rubricUUID",
shared_infrastructure.WithAuthenticationMiddleware(),
shared_infrastructure.WithAuthorizationMiddleware([]string{"teacher"}),
controller.HandleGetRubricByUUID,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package implementations
import (
"context"
"database/sql"
"fmt"
"time"

"github.com/UPB-Code-Labs/main-api/src/rubrics/domain/dtos"
Expand Down Expand Up @@ -140,7 +139,6 @@ func (repository *RubricsPostgresRepository) GetByUUID(uuid string) (rubric *ent
WHERE objective_id = ANY($1)
`, pq.Array(objectivesUUIDs))
if err != nil {
fmt.Println("Error selecting criteria")
return nil, err
}

Expand Down