Skip to content

Commit

Permalink
feat(obligation_audits): Create endpoint to fetch audits of an obliga…
Browse files Browse the repository at this point in the history
…tion

Signed-off-by: deo002 <[email protected]>
  • Loading branch information
deo002 committed Feb 1, 2024
1 parent fd029ce commit a7178cb
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,68 @@ const docTemplate = `{
}
}
},
"/obligations/{topic}/audits": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Fetches audits corresponding to an obligation",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Obligations"
],
"summary": "Fetches audits corresponding to an obligation",
"operationId": "GetObligationAudits",
"parameters": [
{
"type": "string",
"description": "Topic of the obligation for which audits need to be fetched",
"name": "topic",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Number of records per page",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.AuditResponse"
}
},
"404": {
"description": "No obligation with given topic found",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"500": {
"description": "unable to find audits with such obligation topic",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/search": {
"post": {
"description": "Search licenses on different filters and algorithms",
Expand Down
62 changes: 62 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,68 @@
}
}
},
"/obligations/{topic}/audits": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "Fetches audits corresponding to an obligation",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Obligations"
],
"summary": "Fetches audits corresponding to an obligation",
"operationId": "GetObligationAudits",
"parameters": [
{
"type": "string",
"description": "Topic of the obligation for which audits need to be fetched",
"name": "topic",
"in": "path",
"required": true
},
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Number of records per page",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.AuditResponse"
}
},
"404": {
"description": "No obligation with given topic found",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"500": {
"description": "unable to find audits with such obligation topic",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/search": {
"post": {
"description": "Search licenses on different filters and algorithms",
Expand Down
40 changes: 40 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,46 @@ paths:
summary: Update obligation
tags:
- Obligations
/obligations/{topic}/audits:
get:
consumes:
- application/json
description: Fetches audits corresponding to an obligation
operationId: GetObligationAudits
parameters:
- description: Topic of the obligation for which audits need to be fetched
in: path
name: topic
required: true
type: string
- description: Page number
in: query
name: page
type: integer
- description: Number of records per page
in: query
name: limit
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.AuditResponse'
"404":
description: No obligation with given topic found
schema:
$ref: '#/definitions/models.LicenseError'
"500":
description: unable to find audits with such obligation topic
schema:
$ref: '#/definitions/models.LicenseError'
security:
- ApiKeyAuth: []
summary: Fetches audits corresponding to an obligation
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 @@ -68,6 +68,7 @@ func Router() *gin.Engine {
{
obligations.GET("", GetAllObligation)
obligations.GET(":topic", GetObligation)
obligations.GET(":topic/audits", GetObligationAudits)
}
obMap := unAuthorizedv1.Group("/obligation_maps")
{
Expand Down
62 changes: 62 additions & 0 deletions pkg/api/obligations.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,65 @@ func DeleteObligation(c *gin.Context) {
db.DB.Where(models.Obligation{Topic: tp}).Save(&obligation)
c.Status(http.StatusNoContent)
}

// GetObligationAudits fetches audits corresponding to an obligation

// @Summary Fetches audits corresponding to an obligation
// @Description Fetches audits corresponding to an obligation
// @Id GetObligationAudits
// @Tags Obligations
// @Accept json
// @Produce json
// @Param topic path string true "Topic of the obligation for which audits need to be fetched"
// @Param page query int false "Page number"
// @Param limit query int false "Number of records per page"
// @Success 200 {object} models.AuditResponse
// @Failure 404 {object} models.LicenseError "No obligation with given topic found"
// @Failure 500 {object} models.LicenseError "unable to find audits with such obligation topic"
// @Security ApiKeyAuth
// @Router /obligations/{topic}/audits [get]
func GetObligationAudits(c *gin.Context) {
var obligation models.Obligation
topic := c.Param("topic")

result := db.DB.Where(models.Obligation{Topic: topic}).Select("id").First(&obligation)
if result.Error != nil {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: fmt.Sprintf("obligation with topic '%s' not found", topic),
Error: result.Error.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return
}

var audits []models.Audit
query := db.DB.Model(&models.Audit{})
query.Where(models.Audit{TypeId: obligation.Id, Type: "Obligation"})
_ = utils.PreparePaginateResponse(c, query, &models.AuditResponse{})

res := query.Find(&audits)
if res.Error != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "unable to find audits with such obligation topic",
Error: res.Error.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

response := models.AuditResponse{
Data: audits,
Status: http.StatusOK,
Meta: &models.PaginationMeta{
ResourceCount: len(audits),
},
}

c.JSON(http.StatusOK, response)
}

0 comments on commit a7178cb

Please sign in to comment.