Skip to content

Commit

Permalink
Pagination Querry Get all Todos
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsinghvi committed Nov 18, 2023
1 parent fca1dc1 commit 29ddda0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ kubectl rollout restart deployment/name # to update image
kubectl get ingress # ingress exposed url
kubectl port-forward statefulset.apps/postgres 5432:5432
kubectl exec --stdin --tty pod/postgres-0 -- /bin/bash
kubectl logs -f pod/go-todo-api-5587558c9b-zhb75 -c check-db-ready
kubectl logs -f pod/go-todo-api-5587558c9b-zhb75 -c check-db-ready

psql -h localhost -p 5432 -d postgres -U postgres

Expand Down Expand Up @@ -146,7 +146,6 @@ cidr_range=$(aws ec2 describe-vpcs \
--output text \
--region $AWS_EKS_CLUSTER_REGION)


security_group_id=$(aws ec2 create-security-group \
--group-name MyEfsSecurityGroup \
--description "My EFS security group" \
Expand All @@ -165,7 +164,6 @@ file_system_id=$(aws efs create-file-system \
--query 'FileSystemId' \
--output text)


aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=$vpc_id" \
--query 'Subnets[*].{SubnetId: SubnetId,AvailabilityZone: AvailabilityZone,CidrBlock: CidrBlock}' \
Expand Down
44 changes: 35 additions & 9 deletions controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,43 @@ import (
guuid "github.com/google/uuid"
"log"
"net/http"
"strconv"
"time"

"harshsinghvi/golang-postgres-kubernetes/database"
"harshsinghvi/golang-postgres-kubernetes/models"
)

// var (connection *pg.DB )// = *database.GetDatabase()

// init() {
// connection = = *database.GetDatabase()
// }
func GetAllTodos(c *gin.Context) {
var page int
var todos []models.Todo

// connection := *database.GetDatabase()
totalRecords, err := database.Connection.Model(&todos).Count()
if err != nil {
log.Printf("Error while getting all todos, Reason: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": "Something went wrong",
})
return
}

func GetAllTodos(c *gin.Context) {
totalPages := totalRecords / 10

if c.Query("page") == "" {
page = 1
} else {
page, _ = strconv.Atoi(c.Query("page"))
if page == -1 {
err = database.Connection.Model(&todos).Order("created_at DESC").Select()
} else {
if page < 1 {
page = 1
}
err = database.Connection.Model(&todos).Order("created_at DESC").Limit(10).Offset(10 * page).Select()
}
}

var todos []models.Todo
err := database.Connection.Model(&todos).Select()
if err != nil {
log.Printf("Error while getting all todos, Reason: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{
Expand All @@ -36,6 +55,13 @@ func GetAllTodos(c *gin.Context) {
"status": http.StatusOK,
"message": "All Todos",
"data": todos,
"pagination": gin.H{
"total_records": totalRecords,
"current_page": page,
"total_pages": totalPages,
"next_page": page + 1,
"prev_page": page - 1,
},
})
}

Expand Down
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func CreateTodoTable() error {
_, err := Connection.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS index_todo ON todos(id, completed, created_at, updated_at);`)

if err != nil {
log.Printf(err.Error())
log.Println(err.Error())
}

log.Printf("Todo table and indexes created")
Expand Down
4 changes: 2 additions & 2 deletions k8s-eks-system/loadgenerator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
labels:
app: loadgenerator
spec:
replicas: 3
replicas: 50
selector:
matchLabels:
app: loadgenerator
Expand All @@ -22,4 +22,4 @@ spec:
command:
- /bin/sh
- -c
- "while true; do wget -q -O- http://golang-todo-api-service:8080/todos; done"
- "while true; do wget -q -O- http://golang-todo-api-service:8080/api/v2/todo; done"

0 comments on commit 29ddda0

Please sign in to comment.