Skip to content

Commit

Permalink
Merge pull request #34 from fossology/feat/api/pagination
Browse files Browse the repository at this point in the history
feat(api): add pagination capabilities with middleware

Reviewed-by: [email protected]
  • Loading branch information
GMishx authored Jan 31, 2024
2 parents a0f7261 + 3939211 commit 079cf3a
Show file tree
Hide file tree
Showing 11 changed files with 510 additions and 135 deletions.
56 changes: 53 additions & 3 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ const docTemplate = `{
],
"summary": "Get audit records",
"operationId": "GetAllAudit",
"parameters": [
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Number of records per page",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"description": "Audit records",
Expand Down Expand Up @@ -313,6 +327,18 @@ const docTemplate = `{
"description": "Copyleft flag status of license",
"name": "copyleft",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Limit of responses per page",
"name": "limit",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -756,6 +782,18 @@ const docTemplate = `{
"name": "active",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Number of records per page",
"name": "limit",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -1718,15 +1756,27 @@ const docTemplate = `{
"models.PaginationMeta": {
"type": "object",
"properties": {
"page": {
"limit": {
"type": "integer",
"example": 1
"example": 10
},
"next": {
"type": "string",
"example": "/api/v1/licenses?limit=10\u0026page=11"
},
"per_page": {
"page": {
"type": "integer",
"example": 10
},
"previous": {
"type": "string",
"example": "/api/v1/licenses?limit=10\u0026page=9"
},
"resource_count": {
"type": "integer",
"example": 200
},
"total_pages": {
"type": "integer",
"example": 20
}
Expand Down
56 changes: 53 additions & 3 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
],
"summary": "Get audit records",
"operationId": "GetAllAudit",
"parameters": [
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Number of records per page",
"name": "limit",
"in": "query"
}
],
"responses": {
"200": {
"description": "Audit records",
Expand Down Expand Up @@ -307,6 +321,18 @@
"description": "Copyleft flag status of license",
"name": "copyleft",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Limit of responses per page",
"name": "limit",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -750,6 +776,18 @@
"name": "active",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Number of records per page",
"name": "limit",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -1712,15 +1750,27 @@
"models.PaginationMeta": {
"type": "object",
"properties": {
"page": {
"limit": {
"type": "integer",
"example": 1
"example": 10
},
"next": {
"type": "string",
"example": "/api/v1/licenses?limit=10\u0026page=11"
},
"per_page": {
"page": {
"type": "integer",
"example": 10
},
"previous": {
"type": "string",
"example": "/api/v1/licenses?limit=10\u0026page=9"
},
"resource_count": {
"type": "integer",
"example": 200
},
"total_pages": {
"type": "integer",
"example": 20
}
Expand Down
40 changes: 37 additions & 3 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,22 @@ definitions:
type: object
models.PaginationMeta:
properties:
page:
example: 1
limit:
example: 10
type: integer
per_page:
next:
example: /api/v1/licenses?limit=10&page=11
type: string
page:
example: 10
type: integer
previous:
example: /api/v1/licenses?limit=10&page=9
type: string
resource_count:
example: 200
type: integer
total_pages:
example: 20
type: integer
type: object
Expand Down Expand Up @@ -535,6 +544,15 @@ paths:
- application/json
description: Get all audit records from the server
operationId: GetAllAudit
parameters:
- 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:
Expand Down Expand Up @@ -715,6 +733,14 @@ paths:
in: query
name: copyleft
type: boolean
- description: Page number
in: query
name: page
type: integer
- description: Limit of responses per page
in: query
name: limit
type: integer
produces:
- application/json
responses:
Expand Down Expand Up @@ -1007,6 +1033,14 @@ paths:
name: active
required: true
type: boolean
- 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:
Expand Down
8 changes: 6 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/fossology/LicenseDb/pkg/auth"
"github.com/fossology/LicenseDb/pkg/db"
"github.com/fossology/LicenseDb/pkg/middleware"
"github.com/fossology/LicenseDb/pkg/models"
)

Expand Down Expand Up @@ -47,7 +48,10 @@ func Router() *gin.Engine {
r.NoRoute(HandleInvalidUrl)

// CORS middleware
r.Use(auth.CORSMiddleware())
r.Use(middleware.CORSMiddleware())

// Pagination middleware
r.Use(middleware.PaginationMiddleware())

unAuthorizedv1 := r.Group("/api/v1")
{
Expand Down Expand Up @@ -81,7 +85,7 @@ func Router() *gin.Engine {
}

authorizedv1 := r.Group("/api/v1")
authorizedv1.Use(auth.AuthenticationMiddleware())
authorizedv1.Use(middleware.AuthenticationMiddleware())
{
licenses := authorizedv1.Group("/licenses")
{
Expand Down
15 changes: 10 additions & 5 deletions pkg/api/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ import (
// @Tags Audits
// @Accept json
// @Produce json
// @Success 200 {object} models.AuditResponse "Audit records"
// @Failure 404 {object} models.LicenseError "Not changelogs in DB"
// @Param page query int false "Page number"
// @Param limit query int false "Number of records per page"
// @Success 200 {object} models.AuditResponse "Audit records"
// @Failure 404 {object} models.LicenseError "Not changelogs in DB"
// @Security ApiKeyAuth
// @Router /audits [get]
func GetAllAudit(c *gin.Context) {
var audit []models.Audit
query := db.DB.Model(&models.Audit{})

if err := db.DB.Find(&audit).Error; err != nil {
_ = utils.PreparePaginateResponse(c, query, &models.AuditResponse{})

if err := query.Find(&audit).Error; err != nil {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: "Change log not found",
Expand All @@ -45,7 +50,7 @@ func GetAllAudit(c *gin.Context) {
res := models.AuditResponse{
Data: audit,
Status: http.StatusOK,
Meta: models.PaginationMeta{
Meta: &models.PaginationMeta{
ResourceCount: len(audit),
},
}
Expand Down Expand Up @@ -88,7 +93,7 @@ func GetAudit(c *gin.Context) {
res := models.AuditResponse{
Data: []models.Audit{changelog},
Status: http.StatusOK,
Meta: models.PaginationMeta{
Meta: &models.PaginationMeta{
ResourceCount: 1,
},
}
Expand Down
Loading

0 comments on commit 079cf3a

Please sign in to comment.