Skip to content

Commit

Permalink
Update quotes_dao.go
Browse files Browse the repository at this point in the history
  • Loading branch information
St0iK authored May 21, 2019
1 parent 93e34b9 commit a56acd8
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions dao/quotes_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package dao

import (
"context"
"fmt"
"log"
"math/rand"
//"os"

"os"
"time"

"github.com/St0iK/go-quote-parser/model"
Expand All @@ -27,7 +27,7 @@ func Connect() {
log.Println("Initialising MongoDB connection")
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)

client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_DB_URL")))

if err != nil {
log.Fatal(err)
Expand All @@ -45,27 +45,20 @@ func Connect() {

}

// something
func GetRandomQuote() model.Quote {

rand.Seed(time.Now().UnixNano())
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)

c, error := collection.CountDocuments(ctx, bson.D{}, nil)
fmt.Println("Getting info")
fmt.Println(c)
fmt.Println(error)

c, _ := collection.CountDocuments(ctx, bson.D{}, nil)

random:= rand.Int63n(c - 1) + 1
fmt.Println("random")
fmt.Println(random)
random := rand.Int63n(c-1) + 1

findOptions := options.Find()
findOptions.SetLimit(1)
findOptions.SetSkip(random)

// var results []*model.Quote


cur, err := collection.Find(ctx, bson.D{}, findOptions)
if err != nil {
log.Fatal(err)
Expand All @@ -80,16 +73,12 @@ func GetRandomQuote() model.Quote {
if err != nil {
log.Fatal(err)
}

return elem

}

if err := cur.Err(); err != nil {
log.Fatal(err)
}

return model.Quote{};


return model.Quote{}
}

0 comments on commit a56acd8

Please sign in to comment.