This repository was archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservice.go
More file actions
94 lines (81 loc) · 2.74 KB
/
service.go
File metadata and controls
94 lines (81 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"log"
"time"
)
const (
trandoshanDatabase = "trandoshan"
resourceCollection = "resources"
)
// Database resource mapping
type resourceData struct {
Id primitive.ObjectID `bson:"_id"`
Url string `bson:"url"`
Title string `bson:"title"`
CrawlDate time.Time `bson:"crawlDate"`
Content string `bson:"content"`
}
// Search resources using search criterias
// and trigger given callback when a resource match given search criteria
// TODO: use channel instead
func searchResources(client *mongo.Client, url string, searchCriteria string, callback func(data *resourceData)) error {
// Setup production context and acquire database collection
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
resourceCollection := client.Database(trandoshanDatabase).Collection(resourceCollection)
// add search criterias
// todo: fix or query not working
filters := bson.M{
"$or": []bson.M{
{
"title": bson.M{"$regex": primitive.Regex{Pattern: searchCriteria, Options: "i"}},
"content": bson.M{"$regex": primitive.Regex{Pattern: searchCriteria, Options: "i"}},
},
}}
// add url criteria if set
if url != "" {
filters["url"] = bson.M{"url": url}
}
// Query the database for result
cur, err := resourceCollection.Find(ctx, filters)
if err != nil {
return fmt.Errorf("error while querying database: %s", err)
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var resource resourceData
err := cur.Decode(&resource)
if err != nil {
// if there is a decoding error dot not return error since other results may be decoded
log.Printf("error while decoding result: %s", err)
// go to next iteration
break
}
// trigger callback with decoded resource
callback(&resource)
}
if err := cur.Err(); err != nil {
return fmt.Errorf("error with cursor: %s", err)
}
return nil
}
// Get resource using his object-id
func getResource(client *mongo.Client, objectIdHex string) (*resourceData, error) {
objectId, err := primitive.ObjectIDFromHex(objectIdHex)
if err != nil {
return nil, fmt.Errorf("unable to convert objectId from hex value: %s", err)
}
// Setup production context and acquire database collection
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
resourceCollection := client.Database(trandoshanDatabase).Collection(resourceCollection)
// Query database for result
var resource resourceData
if err := resourceCollection.FindOne(ctx, bson.M{"_id": objectId}).Decode(&resource); err != nil {
return nil, fmt.Errorf("error while decoding result: %s", err)
}
return &resource, nil
}