From e933e01b7d619e7fc0f9b585318cbd6f4b5933d6 Mon Sep 17 00:00:00 2001 From: deo002 Date: Fri, 7 Jun 2024 14:39:24 +0530 Subject: [PATCH] feat(obligations_preview): Create an endpoint to fetch topic and type of all obligations Signed-off-by: deo002 --- cmd/laas/docs/docs.go | 66 +++++++++++++++++++++++++++++++++++++ cmd/laas/docs/swagger.json | 66 +++++++++++++++++++++++++++++++++++++ cmd/laas/docs/swagger.yaml | 45 +++++++++++++++++++++++++ pkg/api/api.go | 1 + pkg/api/obligations.go | 67 ++++++++++++++++++++++++++++++++++++-- pkg/models/types.go | 12 +++++++ 6 files changed, 254 insertions(+), 3 deletions(-) diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index e5639ad..2938c8d 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -1075,6 +1075,39 @@ const docTemplate = `{ } } }, + "/obligations/preview": { + "get": { + "description": "Get topic and type of all active obligations from the service", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Obligations" + ], + "summary": "Get topic and types of all active obligations", + "operationId": "GetAllObligationPreviews", + "parameters": [ + { + "type": "boolean", + "description": "Active obligation only", + "name": "active", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.ObligationPreviewResponse" + } + } + } + } + }, "/obligations/{topic}": { "get": { "description": "Get an active based on given topic", @@ -2301,6 +2334,39 @@ const docTemplate = `{ } } }, + "models.ObligationPreview": { + "type": "object", + "properties": { + "topic": { + "type": "string", + "example": "Provide Copyright Notices" + }, + "type": { + "type": "string", + "enum": [ + "obligation", + "restriction", + "risk", + "right" + ] + } + } + }, + "models.ObligationPreviewResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/models.ObligationPreview" + } + }, + "status": { + "type": "integer", + "example": 200 + } + } + }, "models.ObligationResponse": { "type": "object", "properties": { diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index 19354bd..3ac21fb 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -1068,6 +1068,39 @@ } } }, + "/obligations/preview": { + "get": { + "description": "Get topic and type of all active obligations from the service", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Obligations" + ], + "summary": "Get topic and types of all active obligations", + "operationId": "GetAllObligationPreviews", + "parameters": [ + { + "type": "boolean", + "description": "Active obligation only", + "name": "active", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.ObligationPreviewResponse" + } + } + } + } + }, "/obligations/{topic}": { "get": { "description": "Get an active based on given topic", @@ -2294,6 +2327,39 @@ } } }, + "models.ObligationPreview": { + "type": "object", + "properties": { + "topic": { + "type": "string", + "example": "Provide Copyright Notices" + }, + "type": { + "type": "string", + "enum": [ + "obligation", + "restriction", + "risk", + "right" + ] + } + } + }, + "models.ObligationPreviewResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/models.ObligationPreview" + } + }, + "status": { + "type": "integer", + "example": 200 + } + } + }, "models.ObligationResponse": { "type": "object", "properties": { diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index 99ffdc8..35bd53f 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -592,6 +592,29 @@ definitions: - topic - type type: object + models.ObligationPreview: + properties: + topic: + example: Provide Copyright Notices + type: string + type: + enum: + - obligation + - restriction + - risk + - right + type: string + type: object + models.ObligationPreviewResponse: + properties: + data: + items: + $ref: '#/definitions/models.ObligationPreview' + type: array + status: + example: 200 + type: integer + type: object models.ObligationResponse: properties: data: @@ -1524,6 +1547,28 @@ paths: summary: Import obligations by uploading a json file tags: - Obligations + /obligations/preview: + get: + consumes: + - application/json + description: Get topic and type of all active obligations from the service + operationId: GetAllObligationPreviews + parameters: + - description: Active obligation only + in: query + name: active + required: true + type: boolean + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.ObligationPreviewResponse' + summary: Get topic and types of all active obligations + tags: + - Obligations /search: post: consumes: diff --git a/pkg/api/api.go b/pkg/api/api.go index d7a4b4e..e8e1970 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -80,6 +80,7 @@ func Router() *gin.Engine { obligations := unAuthorizedv1.Group("/obligations") { obligations.GET("", GetAllObligation) + obligations.GET("/preview", GetAllObligationPreviews) obligations.GET(":topic", GetObligation) obligations.GET(":topic/audits", GetObligationAudits) obligations.GET("export", ExportObligations) diff --git a/pkg/api/obligations.go b/pkg/api/obligations.go index 5604b6a..c215ff8 100644 --- a/pkg/api/obligations.go +++ b/pkg/api/obligations.go @@ -66,13 +66,13 @@ func GetAllObligation(c *gin.Context) { if err = query.Find(&obligations).Error; err != nil { er := models.LicenseError{ - Status: http.StatusNotFound, - Message: "Obligations not found", + Status: http.StatusInternalServerError, + Message: "Unable to fetch obligations", Error: err.Error(), Path: c.Request.URL.Path, Timestamp: time.Now().Format(time.RFC3339), } - c.JSON(http.StatusNotFound, er) + c.JSON(http.StatusInternalServerError, er) return } res := models.ObligationResponse{ @@ -782,3 +782,64 @@ func addChangelogsForObligationUpdate(tx *gorm.DB, username string, return nil } + +// GetAllObligationPreviews retrieves a list of topics and types of all obligations +// +// @Summary Get topic and types of all active obligations +// @Description Get topic and type of all active obligations from the service +// @Id GetAllObligationPreviews +// @Tags Obligations +// @Accept json +// @Produce json +// @Param active query bool true "Active obligation only" +// @Success 200 {object} models.ObligationPreviewResponse +// @Router /obligations/preview [get] +func GetAllObligationPreviews(c *gin.Context) { + var obligations []models.Obligation + var obligationPreviews []models.ObligationPreview + active := c.Query("active") + if active == "" { + active = "true" + } + var parsedActive bool + parsedActive, err := strconv.ParseBool(active) + if err != nil { + er := models.LicenseError{ + Status: http.StatusBadRequest, + Message: "Invalid active value", + Error: fmt.Sprintf("Parsing failed for value '%s'", active), + Path: c.Request.URL.Path, + Timestamp: time.Now().Format(time.RFC3339), + } + c.JSON(http.StatusBadRequest, er) + return + } + query := db.DB.Model(&models.Obligation{}) + query.Where("active = ?", parsedActive) + + if err = query.Find(&obligations).Error; err != nil { + er := models.LicenseError{ + Status: http.StatusInternalServerError, + Message: "Unable to fetch obligations", + Error: err.Error(), + Path: c.Request.URL.Path, + Timestamp: time.Now().Format(time.RFC3339), + } + c.JSON(http.StatusInternalServerError, er) + return + } + + for _, ob := range obligations { + obligationPreviews = append(obligationPreviews, models.ObligationPreview{ + Topic: ob.Topic, + Type: ob.Type, + }) + } + + res := models.ObligationPreviewResponse{ + Data: obligationPreviews, + Status: http.StatusOK, + } + + c.JSON(http.StatusOK, res) +} diff --git a/pkg/models/types.go b/pkg/models/types.go index a4f1e9c..c475cae 100644 --- a/pkg/models/types.go +++ b/pkg/models/types.go @@ -333,6 +333,18 @@ type Obligation struct { Md5 string `gorm:"unique" json:"-"` } +// ObligationPreview is just the Type and Topic of Obligation +type ObligationPreview struct { + Topic string `json:"topic" example:"Provide Copyright Notices"` + Type string `json:"type" enums:"obligation,restriction,risk,right"` +} + +// ObligationResponse represents the response format for obligation data. +type ObligationPreviewResponse struct { + Status int `json:"status" example:"200"` + Data []ObligationPreview `json:"data"` +} + // ObligationPOSTRequestJSONSchema represents the data format of POST request for obligation type ObligationPOSTRequestJSONSchema struct { Topic string `json:"topic" binding:"required" example:"copyleft"`