Skip to content

Commit

Permalink
changed package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Danushka96 committed Nov 8, 2024
1 parent 5eb22b3 commit b51ae0c
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 40 deletions.
25 changes: 12 additions & 13 deletions ginboot/api.go → api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,42 @@ package ginboot
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/klass-lk/ginboot/types"
"net/http"
"strconv"
"strings"
)

func GetAuthContext(c *gin.Context) (types.AuthContext, error) {
func GetAuthContext(c *gin.Context) (AuthContext, error) {
userId, exists := c.Get("user_id")
if !exists {
c.AbortWithStatus(http.StatusUnauthorized)
return types.AuthContext{}, errors.New("operation not permitted")
return AuthContext{}, errors.New("operation not permitted")
}
role, exists := c.Get("role")
if !exists {
c.AbortWithStatus(http.StatusUnauthorized)
return types.AuthContext{}, errors.New("operation not permitted")
return AuthContext{}, errors.New("operation not permitted")
}
return types.AuthContext{
return AuthContext{
UserId: userId.(string),
Role: role.(string),
}, nil
}

func BuildAuthRequestContext[T interface{}](c *gin.Context) (T, types.AuthContext, error) {
func BuildAuthRequestContext[T interface{}](c *gin.Context) (T, AuthContext, error) {
request, err := BuildRequest[T](c)
if err != nil {
return request, types.AuthContext{}, err
return request, AuthContext{}, err
}
authContext, err := GetAuthContext(c)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return request, types.AuthContext{}, err
return request, AuthContext{}, err
}
return request, authContext, nil
}

func BuildPageRequest(c *gin.Context) types.PageRequest {
func BuildPageRequest(c *gin.Context) PageRequest {
pageString := c.DefaultQuery("page", "1")
sizeString := c.DefaultQuery("size", "10")
sortString := c.DefaultQuery("sort", "_id,asc")
Expand All @@ -52,24 +51,24 @@ func BuildPageRequest(c *gin.Context) types.PageRequest {
c.AbortWithStatus(http.StatusBadRequest)
}
sortSplit := strings.Split(sortString, ",")
var sort types.SortField
var sort SortField
if len(sortSplit) > 1 {
direction := 1
if sortSplit[1] == "desc" {
direction = -1
}
sort = types.SortField{
sort = SortField{
Field: sortSplit[0],
Direction: direction,
}
} else {
sort = types.SortField{
sort = SortField{
Field: sortSplit[0],
Direction: 1,
}
}

return types.PageRequest{Page: int(page), Size: int(size), Sort: sort}
return PageRequest{Page: int(page), Size: int(size), Sort: sort}
}

func BuildRequest[T interface{}](c *gin.Context) (T, error) {
Expand Down
2 changes: 1 addition & 1 deletion types/auth_context.go → auth_context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ginboot

type AuthContext struct {
UserId string
Expand Down
2 changes: 1 addition & 1 deletion security/crypt.go → crypt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package security
package ginboot

import "golang.org/x/crypto/bcrypt"

Expand Down
2 changes: 1 addition & 1 deletion types/db_types.go → db_types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ginboot

type SortField struct {
Field string `json:"field"`
Expand Down
2 changes: 1 addition & 1 deletion errors/error_lib.go → error_lib.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package errors
package ginboot

import (
"errors"
Expand Down
9 changes: 4 additions & 5 deletions generic_repository.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package GinBoot
package ginboot

import (
"github.com/klass-lk/ginboot/types"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

type GenericRepository[T types.Document] interface {
type GenericRepository[T Document] interface {
Query() *mongo.Collection
FindById(id interface{}) (T, error)
FindAllById(idList []string) ([]T, error)
Expand All @@ -20,8 +19,8 @@ type GenericRepository[T types.Document] interface {
FindBy(field string, value interface{}) ([]T, error)
FindByMultiple(filters map[string]interface{}) ([]T, error)
FindAll(opts ...*options.FindOptions) ([]T, error)
FindAllPaginated(pageRequest types.PageRequest) (types.PageResponse[T], error)
FindByPaginated(pageRequest types.PageRequest, filters map[string]interface{}) (types.PageResponse[T], error)
FindAllPaginated(pageRequest PageRequest) (PageResponse[T], error)
FindByPaginated(pageRequest PageRequest, filters map[string]interface{}) (PageResponse[T], error)
CountBy(field string, value interface{}) (int64, error)
CountByFilters(filters map[string]interface{}) (int64, error)
ExistsBy(field string, value interface{}) (bool, error)
Expand Down
2 changes: 1 addition & 1 deletion security/jwt.go → jwt.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package security
package ginboot

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion types/mongo_document.go → mongo_document.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package ginboot

type Document interface {
GetID() string
Expand Down
27 changes: 13 additions & 14 deletions mongo_repository.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package GinBoot
package ginboot

import (
"context"
"fmt"
"github.com/klass-lk/ginboot/types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"math"
"time"
)

type MongoRepository[T types.Document] struct {
type MongoRepository[T Document] struct {
collection *mongo.Collection
}

func NewMongoRepository[T types.Document](db *mongo.Database) *MongoRepository[T] {
func NewMongoRepository[T Document](db *mongo.Database) *MongoRepository[T] {
var doc T
return &MongoRepository[T]{
collection: db.Collection(doc.GetCollectionName()),
Expand Down Expand Up @@ -191,7 +190,7 @@ func (r *MongoRepository[T]) FindAll(opts ...*options.FindOptions) ([]T, error)
return results, err
}

func (r *MongoRepository[T]) FindAllPaginated(pageRequest types.PageRequest) (types.PageResponse[T], error) {
func (r *MongoRepository[T]) FindAllPaginated(pageRequest PageRequest) (PageResponse[T], error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
skip := bson.D{{"$skip", (pageRequest.Page - 1) * pageRequest.Size}}
Expand All @@ -204,21 +203,21 @@ func (r *MongoRepository[T]) FindAllPaginated(pageRequest types.PageRequest) (ty
cursor, err := r.Query().Aggregate(ctx, aggregationPipeline)
if err != nil {
fmt.Println("Error running aggregation:", err)
return types.PageResponse[T]{}, err
return PageResponse[T]{}, err
}
defer cursor.Close(ctx)

var results []T
if err = cursor.All(ctx, &results); err != nil {
fmt.Println("Error retrieving results:", err)
return types.PageResponse[T]{}, err
return PageResponse[T]{}, err
}
count, err := r.Query().CountDocuments(ctx, bson.M{})
if err != nil {
return types.PageResponse[T]{}, err
return PageResponse[T]{}, err
}
totalPages := int(math.Ceil(float64(count) / float64(pageRequest.Size)))
pageResponse := types.PageResponse[T]{
pageResponse := PageResponse[T]{
Contents: results,
NumberOfElements: pageRequest.Size,
Pageable: pageRequest,
Expand All @@ -228,7 +227,7 @@ func (r *MongoRepository[T]) FindAllPaginated(pageRequest types.PageRequest) (ty
return pageResponse, nil
}

func (r *MongoRepository[T]) FindByPaginated(pageRequest types.PageRequest, filters map[string]interface{}) (types.PageResponse[T], error) {
func (r *MongoRepository[T]) FindByPaginated(pageRequest PageRequest, filters map[string]interface{}) (PageResponse[T], error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand All @@ -248,21 +247,21 @@ func (r *MongoRepository[T]) FindByPaginated(pageRequest types.PageRequest, filt
cursor, err := r.Query().Aggregate(ctx, aggregationPipeline)
if err != nil {
fmt.Println("Error running aggregation:", err)
return types.PageResponse[T]{}, err
return PageResponse[T]{}, err
}
defer cursor.Close(ctx)

var results []T
if err = cursor.All(ctx, &results); err != nil {
fmt.Println("Error retrieving results:", err)
return types.PageResponse[T]{}, err
return PageResponse[T]{}, err
}
count, err := r.Query().CountDocuments(ctx, filter)
if err != nil {
return types.PageResponse[T]{}, err
return PageResponse[T]{}, err
}
totalPages := int(math.Ceil(float64(count) / float64(pageRequest.Size)))
pageResponse := types.PageResponse[T]{
pageResponse := PageResponse[T]{
Contents: results,
NumberOfElements: pageRequest.Size,
Pageable: pageRequest,
Expand Down
2 changes: 1 addition & 1 deletion security/password_encoder.go → password_encoder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package security
package ginboot

type PasswordEncoder interface {
GetPasswordHash(password string) (string, error)
Expand Down
2 changes: 1 addition & 1 deletion security/pbkdf2_encoder.go → pbkdf2_encoder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package security
package ginboot

import (
"crypto/sha512"
Expand Down

0 comments on commit b51ae0c

Please sign in to comment.