Skip to content

Commit

Permalink
fix(bug): Minor bug fixes in audit endpoints
Browse files Browse the repository at this point in the history
Signed-off-by: deo002 <[email protected]>
  • Loading branch information
deo002 committed Feb 2, 2024
1 parent fd029ce commit 5cb2873
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
6 changes: 6 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ const docTemplate = `{
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"500": {
"description": "unable to find changes",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"500": {
"description": "unable to find changes",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@ paths:
description: No audit entry with given ID
schema:
$ref: '#/definitions/models.LicenseError'
"500":
description: unable to find changes
schema:
$ref: '#/definitions/models.LicenseError'
security:
- ApiKeyAuth: []
summary: Get changelogs
Expand Down
38 changes: 28 additions & 10 deletions pkg/api/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func GetAllAudit(c *gin.Context) {

if err := query.Find(&audit).Error; err != nil {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: "Change log not found",
Status: http.StatusInternalServerError,
Message: "unable to fetch audits",
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.AuditResponse{
Expand Down Expand Up @@ -73,25 +73,26 @@ func GetAllAudit(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /audits/{audit_id} [get]
func GetAudit(c *gin.Context) {
var changelog models.Audit
var audit models.Audit
id := c.Param("audit_id")
parsedId, err := utils.ParseIdToInt(c, id, "audit")
if err != nil {
return
}

if err := db.DB.Where(models.Audit{Id: parsedId}).First(&changelog).Error; err != nil {
if err := db.DB.Where(models.Audit{Id: parsedId}).First(&audit).Error; err != nil {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: "no change log with such id exists",
Message: "no audit with such id exists",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return
}
res := models.AuditResponse{
Data: []models.Audit{changelog},
Data: []models.Audit{audit},
Status: http.StatusOK,
Meta: &models.PaginationMeta{
ResourceCount: 1,
Expand All @@ -113,6 +114,7 @@ func GetAudit(c *gin.Context) {
// @Success 200 {object} models.ChangeLogResponse
// @Failure 400 {object} models.LicenseError "Invalid audit ID"
// @Failure 404 {object} models.LicenseError "No audit entry with given ID"
// @Failure 500 {object} models.LicenseError "unable to find changes"
// @Security ApiKeyAuth
// @Router /audits/{audit_id}/changes [get]
func GetChangeLogs(c *gin.Context) {
Expand All @@ -123,15 +125,29 @@ func GetChangeLogs(c *gin.Context) {
return
}

if err := db.DB.Where(models.ChangeLog{AuditId: parsedId}).Find(&changelog).Error; err != nil {
result := db.DB.Where(models.ChangeLog{AuditId: parsedId}).Find(&changelog)
if result.Error != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "unable to fetch changes",
Error: result.Error.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

if result.RowsAffected == 0 {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: "no change log with such id exists",
Error: err.Error(),
Message: "no audit entry with given ID",
Error: "No audit entry with given ID",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return
}

res := models.ChangeLogResponse{
Expand Down Expand Up @@ -182,6 +198,7 @@ func GetChangeLogbyId(c *gin.Context) {
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return
}
if changelog.AuditId != parsedAuditId {
er := models.LicenseError{
Expand All @@ -192,6 +209,7 @@ func GetChangeLogbyId(c *gin.Context) {
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return
}
res := models.ChangeLogResponse{
Data: []models.ChangeLog{changelog},
Expand Down

0 comments on commit 5cb2873

Please sign in to comment.