Skip to content

Commit

Permalink
add repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukmanern committed Jan 24, 2024
1 parent 313d2fc commit dfca9b1
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 450 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/XANi/loremipsum v1.1.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/XANi/loremipsum v1.1.0 h1:pNqL9b0ORlhmlhGPXggwOPe7NifWoQPZmqohLCx04z8=
github.com/XANi/loremipsum v1.1.0/go.mod h1:5W6tlNr1vBCP1dzk36OtF+6e3kWMk06fbgbjS7lspyM=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down
17 changes: 17 additions & 0 deletions internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ import (
"strings"
"time"

"github.com/XANi/loremipsum"
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

func RandomWords(n int) string {
if n < 2 {
n = 2
}
loremIpsumGenerator := loremipsum.New()
words := loremIpsumGenerator.Words(n)
return words
}

// RandomString func generate random string
// used for testing and any needs.
func RandomString(n uint) string {
Expand Down Expand Up @@ -72,3 +82,10 @@ func NewFiberCtx() *fiber.Ctx {
func ToTitle(s string) string {
return cases.Title(language.Und).String(s)
}

func GenerateRandomID() int {
rand.New(rand.NewSource(time.Now().UnixNano()))
min := 9000000
max := 10000000
return rand.Intn(max-min) + min
}
20 changes: 20 additions & 0 deletions internal/helper/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package helper

import (
"log"
"net"
"strings"
"testing"

"github.com/Lukmanern/gost/internal/consts"
"github.com/stretchr/testify/assert"
)

func TestRandomWords(t *testing.T) {
for i := 2; i < 12; i++ {
words := RandomWords(i)
wordsSlice := strings.Split(words, " ")
log.Println(words)
if len(wordsSlice) < i {
t.Fatal("should equal")
}
}
}

func TestRandomString(t *testing.T) {
for i := 0; i < 25; i++ {
s := RandomString(uint(i))
Expand Down Expand Up @@ -78,3 +91,10 @@ func TestToTitle(t *testing.T) {
}
}
}

func TestGenerateRandomID(t *testing.T) {
for i := 0; i < 10; i++ {
v := GenerateRandomID()
assert.True(t, v > 0, "shoult true")
}
}
2 changes: 1 addition & 1 deletion internal/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {

timeNow := time.Now()
params = GenTokenParams{
ID: 1,
ID: helper.GenerateRandomID(),
Email: helper.RandomEmail(),
Roles: map[string]uint8{"test-role": 1},
Exp: timeNow.Add(5 * time.Minute),
Expand Down
1 change: 1 addition & 0 deletions repository/role/role_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (repo *RoleRepositoryImpl) GetByName(ctx context.Context, name string) (rol

func (repo *RoleRepositoryImpl) GetAll(ctx context.Context, filter model.RequestGetAll) (roles []entity.Role, total int, err error) {
var count int64
filter.Sort = ""
args := []interface{}{"%" + filter.Keyword + "%"}
cond := "name LIKE ?"
result := repo.db.Where(cond, args...).Find(&roles)
Expand Down
6 changes: 5 additions & 1 deletion repository/user/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func (repo *UserRepositoryImpl) GetAll(ctx context.Context, filter model.Request
users = []entity.User{}
skip := int64(filter.Limit * (filter.Page - 1))
limit := int64(filter.Limit)
result = repo.db.Where(cond, args...).Limit(int(limit)).Offset(int(skip)).Find(&users)
result = repo.db.Where(cond, args...).Limit(int(limit)).Offset(int(skip))
if filter.Sort != "" {
result = result.Order(filter.Sort + " ASC")
}
result = result.Find(&users)
if result.Error != nil {
return nil, 0, result.Error
}
Expand Down
Loading

0 comments on commit dfca9b1

Please sign in to comment.