Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
refactor: remove GetTableName, db.Model already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
FoseFx committed Nov 8, 2022
1 parent 892d7d3 commit b4db02c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 11 deletions.
3 changes: 1 addition & 2 deletions routes/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package routes
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"rest-api/util"
"strconv"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func GetPagination(ctx *gin.Context, db *gorm.DB, model interface{}) (PaginatedR
}

var totalSize int64
tx := db.Table(util.GetTableName(db, model)).Count(&totalSize)
tx := db.Model(model).Count(&totalSize)

if tx.Error != nil {
return PaginatedResponse{}, tx.Error
Expand Down
9 changes: 0 additions & 9 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package util

import (
"fmt"
"gorm.io/gorm"
"os"
"strings"
)
Expand All @@ -20,11 +19,3 @@ func GetEnvOr(key, fallback string) string {
func Formatted(arg any) string {
return strings.Replace(fmt.Sprintf("%v", arg), "\n", "\\n", -1)
}

// GetTableName returns the table name of a model
// e.g.: `GetTableName(db, EmergencyRoom{})` will return "emergency_rooms"
func GetTableName(db *gorm.DB, model interface{}) string {
stmt := &gorm.Statement{DB: db}
_ = stmt.Parse(&model)
return stmt.Schema.Table
}

0 comments on commit b4db02c

Please # to comment.