Skip to content

Commit ccc9270

Browse files
committed
Add timestamp when donation is inserted to db
1 parent 2f2ff54 commit ccc9270

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

helpers/helpers.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"fmt"
66
"io/ioutil"
77
"net/http"
8+
9+
"go.mongodb.org/mongo-driver/bson/primitive"
810
)
911

1012
type Donation struct {
11-
DonationID string `json:"DonationId" bson:"donationId"`
13+
DonationID string `json:"DonationId" bson:"donationId"`
14+
TimeStamp primitive.DateTime `json:"TimeStamp" bson:"TimeStamp"`
1215
Name string `json:"Name" bson:"nameDonator"`
1316
Amount float32 `json:"Amount" bson:"amountMoney"`
1417
Message string `json:"Message" bson:"message"`
@@ -56,6 +59,8 @@ func GetDonations(url string) (Donations, error) {
5659
}
5760
json.Unmarshal(responseData, &donations)
5861

62+
63+
5964
return donations, nil
6065
}
6166

poll/poll.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/Vauhtijuoksu/gonator/helpers"
1010
"go.mongodb.org/mongo-driver/bson"
1111
"go.mongodb.org/mongo-driver/mongo"
12+
"go.mongodb.org/mongo-driver/bson/primitive"
1213
)
1314

1415
func Poll(ctx context.Context, collection *mongo.Collection, url string, pollRate time.Duration) {
@@ -27,6 +28,7 @@ func Poll(ctx context.Context, collection *mongo.Collection, url string, pollRat
2728
err := collection.FindOne(ctx, filter).Decode(&result)
2829
if err != nil {
2930
if err.Error() == "mongo: no documents in result" {
31+
donation.TimeStamp = primitive.NewDateTimeFromTime(time.Now().AddDate(-1, 0, 0))
3032
insertResult, err := collection.InsertOne(context.TODO(), donation)
3133
fmt.Println("Inserted document: ", insertResult.InsertedID)
3234
if err != nil {

0 commit comments

Comments
 (0)