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(obligations_preview): Create an endpoint to fetch topic and type of all obligations #70

Merged
merged 1 commit into from
Jun 18, 2024
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
66 changes: 66 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
66 changes: 66 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
45 changes: 45 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 64 additions & 3 deletions pkg/api/obligations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Loading