• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vocdoni / saas-backend / 17557469823

08 Sep 2025 04:25PM UTC coverage: 58.777% (-0.06%) from 58.841%
17557469823

Pull #213

github

altergui
fix
Pull Request #213: api: standardize parameters ProcessID, CensusID, GroupID, JobID, UserID, BundleID

254 of 345 new or added lines in 22 files covered. (73.62%)

19 existing lines in 7 files now uncovered.

5652 of 9616 relevant lines covered (58.78%)

32.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.31
/db/object.go
1
package db
2

3
import (
4
        "context"
5
        "fmt"
6
        "time"
7

8
        "github.com/vocdoni/saas-backend/internal"
9
        "go.mongodb.org/mongo-driver/bson"
10
        "go.mongodb.org/mongo-driver/mongo"
11
        "go.mongodb.org/mongo-driver/mongo/options"
12
)
13

14
// The Object entity represents a generic object stored in the database
15
// intended for s3-like storage.
16

17
// Object retrieves an object from the MongoDB collection by its ID.
18
func (ms *MongoStorage) Object(id internal.ObjectID) (*Object, error) {
5✔
19
        if id.IsZero() {
5✔
NEW
20
                return nil, ErrInvalidData
×
NEW
21
        }
×
22

23
        ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
5✔
24
        defer cancel()
5✔
25

5✔
26
        // find the object in the database
5✔
27
        result := ms.objects.FindOne(ctx, bson.M{"_id": id})
5✔
28
        obj := &Object{}
5✔
29
        if err := result.Decode(obj); err != nil {
6✔
30
                if err == mongo.ErrNoDocuments {
2✔
31
                        return nil, ErrNotFound
1✔
32
                }
1✔
33
                return nil, err
×
34
        }
35
        return obj, nil
4✔
36
}
37

38
// SetObject sets the object data for the given objectID. If the
39
// object does not exist, it will be created with the given data, otherwise it
40
// will be updated.
41
func (ms *MongoStorage) SetObject(objectID internal.ObjectID, user, contentType string, data []byte) error {
7✔
42
        if objectID.IsZero() {
7✔
NEW
43
                return ErrInvalidData
×
NEW
44
        }
×
45

46
        ms.keysLock.Lock()
7✔
47
        defer ms.keysLock.Unlock()
7✔
48
        ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
7✔
49
        defer cancel()
7✔
50
        object := &Object{
7✔
51
                ID:          objectID,
7✔
52
                Data:        data,
7✔
53
                CreatedAt:   time.Now(),
7✔
54
                User:        user,
7✔
55
                ContentType: contentType,
7✔
56
        }
7✔
57
        opts := options.ReplaceOptions{}
7✔
58
        opts.Upsert = new(bool)
7✔
59
        *opts.Upsert = true
7✔
60
        _, err := ms.objects.ReplaceOne(ctx, bson.M{"_id": object.ID}, object, &opts)
7✔
61
        if err != nil {
7✔
62
                return fmt.Errorf("cannot update object: %w", err)
×
63
        }
×
64
        return err
7✔
65
}
66

67
// RemoveObject removes the object data for the given objectID.
NEW
68
func (ms *MongoStorage) RemoveObject(objectID internal.ObjectID) error {
×
NEW
69
        if objectID.IsZero() {
×
NEW
70
                return ErrInvalidData
×
NEW
71
        }
×
72

73
        ms.keysLock.Lock()
×
74
        defer ms.keysLock.Unlock()
×
75
        ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
×
76
        defer cancel()
×
77
        _, err := ms.objects.DeleteOne(ctx, bson.M{"_id": objectID})
×
78
        return err
×
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc