add IsDeviceIDExistedMethod
This commit is contained in:
		
							parent
							
								
									b3a42ef4b2
								
							
						
					
					
						commit
						59d31f5e63
					
				|  | @ -0,0 +1,24 @@ | ||||||
|  | package devicemngmt | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"context" | ||||||
|  | 
 | ||||||
|  | 	"github.com/Selly-Modules/logger" | ||||||
|  | 	"go.mongodb.org/mongo-driver/bson" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func (s Service) IsDeviceIDExisted(ctx context.Context, deviceID string) bool { | ||||||
|  | 	var ( | ||||||
|  | 		col    = s.getDeviceCollection() | ||||||
|  | 		device = Device{} | ||||||
|  | 	) | ||||||
|  | 
 | ||||||
|  | 	if err := col.FindOne(ctx, bson.M{"deviceId": deviceID}).Decode(&device); err != nil { | ||||||
|  | 		logger.Error("devicemngmt - findByDeviceID", logger.LogData{ | ||||||
|  | 			"deviceId": deviceID, | ||||||
|  | 			"err":      err.Error(), | ||||||
|  | 		}) | ||||||
|  | 		return true | ||||||
|  | 	} | ||||||
|  | 	return !device.ID.IsZero() | ||||||
|  | } | ||||||
|  | @ -40,7 +40,7 @@ func (s Service) Create(payload CreateOptions) error { | ||||||
| 	deviceData := payload.newDevice() | 	deviceData := payload.newDevice() | ||||||
| 
 | 
 | ||||||
| 	// Find deviceID existed or not
 | 	// Find deviceID existed or not
 | ||||||
| 	if s.isDeviceIDExisted(ctx, deviceData.DeviceID) { | 	if s.IsDeviceIDExisted(ctx, deviceData.DeviceID) { | ||||||
| 		return errors.New("this device is already existed") | 		return errors.New("this device is already existed") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										19
									
								
								db.go
								
								
								
								
							
							
						
						
									
										19
									
								
								db.go
								
								
								
								
							|  | @ -1,11 +1,8 @@ | ||||||
| package devicemngmt | package devicemngmt | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 
 | 
 | ||||||
| 	"github.com/Selly-Modules/logger" |  | ||||||
| 	"go.mongodb.org/mongo-driver/bson" |  | ||||||
| 	"go.mongodb.org/mongo-driver/mongo" | 	"go.mongodb.org/mongo-driver/mongo" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -16,19 +13,3 @@ func (s Service) getDeviceCollection() *mongo.Collection { | ||||||
| 	} | 	} | ||||||
| 	return s.DB.Collection(tableDevice) | 	return s.DB.Collection(tableDevice) | ||||||
| } | } | ||||||
| 
 |  | ||||||
| func (s Service) isDeviceIDExisted(ctx context.Context, deviceID string) bool { |  | ||||||
| 	var ( |  | ||||||
| 		col    = s.getDeviceCollection() |  | ||||||
| 		device = Device{} |  | ||||||
| 	) |  | ||||||
| 
 |  | ||||||
| 	if err := col.FindOne(ctx, bson.M{"deviceId": deviceID}).Decode(&device); err != nil { |  | ||||||
| 		logger.Error("devicemngmt - findByDeviceID", logger.LogData{ |  | ||||||
| 			"deviceId": deviceID, |  | ||||||
| 			"err":      err.Error(), |  | ||||||
| 		}) |  | ||||||
| 		return true |  | ||||||
| 	} |  | ||||||
| 	return !device.ID.IsZero() |  | ||||||
| } |  | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue