-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search Functionality and restructure
- Loading branch information
1 parent
29ddda0
commit 6b5a9a3
Showing
4 changed files
with
110 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package models | ||
|
||
import "strconv" | ||
|
||
type Pagination struct { | ||
TotalRecords int `json:"total_records"` | ||
CurrentPage int `json:"current_page"` | ||
TotalPages int `json:"total_pages"` | ||
NextPage int `json:"next_page"` | ||
PrevPage int `json:"prev_page"` | ||
} | ||
|
||
func (pag *Pagination) Validate() *Pagination { | ||
pag.Set(pag.CurrentPage, pag.TotalRecords) | ||
return pag | ||
} | ||
|
||
func (pag *Pagination) Set(current int, totalRec int) *Pagination { | ||
pag.TotalRecords = totalRec | ||
|
||
if pag.TotalPages = pag.TotalRecords / 10; pag.TotalRecords%10 == 0 { | ||
pag.TotalPages = pag.TotalPages - 1 | ||
} | ||
|
||
if current <= 0 { | ||
pag.PrevPage = 0 | ||
} else { | ||
pag.PrevPage = current - 1 | ||
} | ||
|
||
if current >= pag.TotalPages { | ||
pag.NextPage = pag.TotalPages | ||
} else { | ||
pag.NextPage = current + 1 | ||
} | ||
|
||
pag.CurrentPage = current | ||
return pag | ||
} | ||
|
||
func (pag *Pagination) ParseString(pageString string) { | ||
pag.CurrentPage = 0 | ||
|
||
if pageString == "" { | ||
return | ||
} | ||
|
||
pag.CurrentPage, _ = strconv.Atoi(pageString) | ||
|
||
if pag.CurrentPage == -1 { | ||
return | ||
} | ||
|
||
if pag.CurrentPage < 0 { | ||
pag.CurrentPage = 0 | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func InternalServerError(c *gin.Context, msg string, err error) { | ||
log.Printf("%s %v\n", msg, err) | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"status": http.StatusInternalServerError, | ||
"message": "Something went wrong", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters