Implement ObjectID Conversion in GetByIDs Method of Company Repository
- Enhanced the GetByIDs method to convert string IDs to BSON ObjectIDs before querying the database, ensuring proper data type handling. - This change improves the robustness of the method by preventing potential errors related to ID format mismatches during database operations.
This commit is contained in:
@@ -101,7 +101,16 @@ func (r *companyRepository) GetByID(ctx context.Context, id string) (*Company, e
|
||||
|
||||
// GetByIDs retrieves companies by IDs
|
||||
func (r *companyRepository) GetByIDs(ctx context.Context, ids []string) ([]Company, error) {
|
||||
companies, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": ids}}, orm.NewPaginationBuilder().Build())
|
||||
objectIDs := make([]bson.ObjectID, len(ids))
|
||||
for i, id := range ids {
|
||||
objectID, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
objectIDs[i] = objectID
|
||||
}
|
||||
|
||||
companies, err := r.ormRepo.FindAll(ctx, bson.M{"_id": bson.M{"$in": objectIDs}}, orm.NewPaginationBuilder().Build())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user