From b51ae0c78882183ed1955d78c52fa89141388eb0 Mon Sep 17 00:00:00 2001 From: danushka96 Date: Fri, 8 Nov 2024 18:16:24 +0530 Subject: [PATCH] changed package structure --- ginboot/api.go => api.go | 25 +++++++++-------- types/auth_context.go => auth_context.go | 2 +- security/crypt.go => crypt.go | 2 +- types/db_types.go => db_types.go | 2 +- errors/error_lib.go => error_lib.go | 2 +- generic_repository.go | 9 +++---- security/jwt.go => jwt.go | 2 +- types/mongo_document.go => mongo_document.go | 2 +- mongo_repository.go | 27 +++++++++---------- ...password_encoder.go => password_encoder.go | 2 +- .../pbkdf2_encoder.go => pbkdf2_encoder.go | 2 +- 11 files changed, 37 insertions(+), 40 deletions(-) rename ginboot/api.go => api.go (71%) rename types/auth_context.go => auth_context.go (78%) rename security/crypt.go => crypt.go (96%) rename types/db_types.go => db_types.go (96%) rename errors/error_lib.go => error_lib.go (98%) rename security/jwt.go => jwt.go (99%) rename types/mongo_document.go => mongo_document.go (82%) rename security/password_encoder.go => password_encoder.go (88%) rename security/pbkdf2_encoder.go => pbkdf2_encoder.go (98%) diff --git a/ginboot/api.go b/api.go similarity index 71% rename from ginboot/api.go rename to api.go index 4c4af9b..cdd6613 100644 --- a/ginboot/api.go +++ b/api.go @@ -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") @@ -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) { diff --git a/types/auth_context.go b/auth_context.go similarity index 78% rename from types/auth_context.go rename to auth_context.go index c193c66..b2c68b6 100644 --- a/types/auth_context.go +++ b/auth_context.go @@ -1,4 +1,4 @@ -package types +package ginboot type AuthContext struct { UserId string diff --git a/security/crypt.go b/crypt.go similarity index 96% rename from security/crypt.go rename to crypt.go index 92a0c6d..cfd7700 100644 --- a/security/crypt.go +++ b/crypt.go @@ -1,4 +1,4 @@ -package security +package ginboot import "golang.org/x/crypto/bcrypt" diff --git a/types/db_types.go b/db_types.go similarity index 96% rename from types/db_types.go rename to db_types.go index 962ed76..7476d48 100644 --- a/types/db_types.go +++ b/db_types.go @@ -1,4 +1,4 @@ -package types +package ginboot type SortField struct { Field string `json:"field"` diff --git a/errors/error_lib.go b/error_lib.go similarity index 98% rename from errors/error_lib.go rename to error_lib.go index f699d77..b06ca5b 100644 --- a/errors/error_lib.go +++ b/error_lib.go @@ -1,4 +1,4 @@ -package errors +package ginboot import ( "errors" diff --git a/generic_repository.go b/generic_repository.go index 4e46e40..ce06e30 100644 --- a/generic_repository.go +++ b/generic_repository.go @@ -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) @@ -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) diff --git a/security/jwt.go b/jwt.go similarity index 99% rename from security/jwt.go rename to jwt.go index debdf38..61443fd 100644 --- a/security/jwt.go +++ b/jwt.go @@ -1,4 +1,4 @@ -package security +package ginboot import ( "errors" diff --git a/types/mongo_document.go b/mongo_document.go similarity index 82% rename from types/mongo_document.go rename to mongo_document.go index c9edc50..c4a515f 100644 --- a/types/mongo_document.go +++ b/mongo_document.go @@ -1,4 +1,4 @@ -package types +package ginboot type Document interface { GetID() string diff --git a/mongo_repository.go b/mongo_repository.go index d38c2e6..a78891a 100644 --- a/mongo_repository.go +++ b/mongo_repository.go @@ -1,9 +1,8 @@ -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" @@ -11,11 +10,11 @@ import ( "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()), @@ -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}} @@ -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, @@ -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() @@ -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, diff --git a/security/password_encoder.go b/password_encoder.go similarity index 88% rename from security/password_encoder.go rename to password_encoder.go index 86a6d9d..27773b3 100644 --- a/security/password_encoder.go +++ b/password_encoder.go @@ -1,4 +1,4 @@ -package security +package ginboot type PasswordEncoder interface { GetPasswordHash(password string) (string, error) diff --git a/security/pbkdf2_encoder.go b/pbkdf2_encoder.go similarity index 98% rename from security/pbkdf2_encoder.go rename to pbkdf2_encoder.go index 70d29fe..bb08cde 100644 --- a/security/pbkdf2_encoder.go +++ b/pbkdf2_encoder.go @@ -1,4 +1,4 @@ -package security +package ginboot import ( "crypto/sha512"