-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
/role
endpoint for provide role
to user
Signed-off-by: anisurrahman75 <sunny.cse7575@gmail.com>
- Loading branch information
1 parent
207bccd
commit 49c7fa3
Showing
17 changed files
with
343 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2024 Praromvik | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package db | ||
|
||
import ( | ||
"cloud.google.com/go/firestore" | ||
|
||
"context" | ||
"fmt" | ||
|
||
"github.com/praromvik/praromvik/models/db/client" | ||
) | ||
|
||
type Firestore struct{} | ||
|
||
func (_ Firestore) GetDocument(collName string, id string) (*firestore.DocumentSnapshot, error) { | ||
dSnap, err := client.Firestore.Collection(collName).Doc(id).Get(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return dSnap, nil | ||
} | ||
|
||
func (_ Firestore) AddDocument(collName string, id string, data interface{}) error { | ||
_, err := client.Firestore.Collection(collName).Doc(id).Create(context.Background(), data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Inserted document with _id: %v\n", id) | ||
return nil | ||
} | ||
|
||
func (_ Firestore) UpdateDocument(collName string, id string, data interface{}) error { | ||
_, err := client.Firestore.Collection(collName).Doc(id).Set(context.Background(), data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Update Document with id %s\n", id) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2024 Praromvik | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package db | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/praromvik/praromvik/models/db/client" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
|
||
"reflect" | ||
) | ||
|
||
func getDBCollection(dbAndCollList []string) (*mongo.Collection, error) { | ||
if len(dbAndCollList) == 0 { | ||
return nil, fmt.Errorf("empty database name") | ||
} else if len(dbAndCollList)%2 == 1 { | ||
return nil, fmt.Errorf("invalid number of DB and collection names provided, must be even") | ||
} | ||
db, collection := &mongo.Database{}, &mongo.Collection{} | ||
for i, val := range dbAndCollList { | ||
if i%2 == 0 { | ||
db = client.Mongo.Database(val) | ||
} else { | ||
collection = db.Collection(val) | ||
} | ||
} | ||
return collection, nil | ||
} | ||
|
||
func MergeStruct(oldStruct interface{}, newStruct interface{}) { | ||
oldValue, newValue := reflect.ValueOf(oldStruct).Elem(), reflect.ValueOf(newStruct) | ||
for i := 0; i < oldValue.NumField(); i++ { | ||
oldFieldValue, newFieldValue := oldValue.Field(i), newValue.Field(i) | ||
if oldFieldValue.Kind() == reflect.Struct { | ||
MergeStruct(oldFieldValue.Addr().Interface(), newFieldValue.Interface()) | ||
} else if !reflect.DeepEqual(newFieldValue.Interface(), reflect.Zero(newFieldValue.Type()).Interface()) { | ||
oldFieldValue.Set(newFieldValue) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2024 Praromvik | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package db | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
type Mongo struct{} | ||
|
||
func (_ Mongo) GetDocument(dbAndCollList []string, filter interface{}) (*mongo.SingleResult, error) { | ||
collection, err := getDBCollection(dbAndCollList) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result := collection.FindOne(context.TODO(), filter) | ||
return result, nil | ||
} | ||
|
||
func (_ Mongo) AddDocument(dbAndCollList []string, data interface{}) error { | ||
collection, err := getDBCollection(dbAndCollList) | ||
if err != nil { | ||
return err | ||
} | ||
result, err := collection.InsertOne(context.TODO(), data) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Inserted document with _id: %v\n", result.InsertedID) | ||
return nil | ||
} | ||
|
||
func (_ Mongo) UpdateDocument(dbAndCollList []string, filter interface{}, newDocument interface{}) error { | ||
collection, err := getDBCollection(dbAndCollList) | ||
if err != nil { | ||
return err | ||
} | ||
result, err := collection.ReplaceOne(context.TODO(), filter, newDocument) | ||
if err != nil { | ||
return fmt.Errorf("failed to update document: %v", err) | ||
} | ||
fmt.Printf("Update Document. Result. MatchedCount:"+ | ||
" %d, UpsertedCount: %d, ModifiedCount: %d.\n", result.MatchedCount, result.UpsertedCount, result.ModifiedCount) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.