You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.3 KiB

6 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. mgo "gopkg.in/mgo.v2"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. //MongoConfig stores the configuration of mongodb to connect
  11. type MongoConfig struct {
  12. Ip string `json:"ip"`
  13. Database string `json:"database"`
  14. }
  15. var mongoConfig MongoConfig
  16. func readMongodbConfig(path string) {
  17. file, e := ioutil.ReadFile(path)
  18. if e != nil {
  19. fmt.Println("error:", e)
  20. }
  21. content := string(file)
  22. json.Unmarshal([]byte(content), &mongoConfig)
  23. }
  24. func getSession() (*mgo.Session, error) {
  25. session, err := mgo.Dial("mongodb://" + mongoConfig.Ip)
  26. if err != nil {
  27. panic(err)
  28. }
  29. //defer session.Close()
  30. // Optional. Switch the session to a monotonic behavior.
  31. session.SetMode(mgo.Monotonic, true)
  32. // Optional. Switch the session to a monotonic behavior.
  33. session.SetMode(mgo.Monotonic, true)
  34. return session, err
  35. }
  36. func getCollection(session *mgo.Session, collection string) *mgo.Collection {
  37. c := session.DB(mongoConfig.Database).C(collection)
  38. return c
  39. }
  40. func saveDataEntryToMongo(c *mgo.Collection, user UserModel) {
  41. /*
  42. how to call this function
  43. var auxArr []string
  44. user := UserModel{"123", auxArr}
  45. saveDataEntryToMongo(c, user)
  46. */
  47. err := c.Insert(user)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. }
  52. func saveItem(c *mgo.Collection, item ItemModel) {
  53. //first, check if the item already exists
  54. result := ItemModel{}
  55. err := c.Find(bson.M{"id": item.ID}).One(&result)
  56. if err != nil {
  57. //item not found, so let's add a new entry
  58. err = c.Insert(item)
  59. check(err)
  60. } else {
  61. /*result.Data = append(result.Data, dataEntry)
  62. err = c.Update(bson.M{"id": dataEntry.ContratoCOD}, result)
  63. if err != nil {
  64. log.Fatal(err)
  65. }*/
  66. }
  67. }
  68. func saveUser(c *mgo.Collection, user UserModel) {
  69. //first, check if the item already exists
  70. result := UserModel{}
  71. err := c.Find(bson.M{"id": user.ID}).One(&result)
  72. if err != nil {
  73. //item not found, so let's add a new entry
  74. err = c.Insert(user)
  75. check(err)
  76. } else {
  77. /*result.Data = append(result.Data, dataEntry)
  78. err = c.Update(bson.M{"id": dataEntry.ContratoCOD}, result)
  79. if err != nil {
  80. log.Fatal(err)
  81. }*/
  82. }
  83. }
  84. func getUserById(id string) (UserModel, error) {
  85. result := UserModel{}
  86. err := userCollection.Find(bson.M{"id": id}).One(&result)
  87. if err != nil {
  88. //user not exist
  89. return result, err
  90. } else {
  91. //user exist
  92. return result, err
  93. }
  94. }
  95. func getItemById(id string) (ItemModel, error) {
  96. result := ItemModel{}
  97. err := itemCollection.Find(bson.M{"id": id}).One(&result)
  98. if err != nil {
  99. //user not exist
  100. return result, err
  101. } else {
  102. //user exist
  103. return result, err
  104. }
  105. }
  106. func updateItem(item ItemModel) (ItemModel, error) {
  107. err := itemCollection.Update(bson.M{"id": item.ID}, item)
  108. if err != nil {
  109. //log.Fatal(err)
  110. return item, err
  111. }
  112. return item, err
  113. }
  114. func updateUser(user UserModel) (UserModel, error) {
  115. err := userCollection.Update(bson.M{"id": user.ID}, user)
  116. if err != nil {
  117. return user, err
  118. }
  119. return user, err
  120. }
  121. func getAllItems() ([]ItemModel, error) {
  122. result := []ItemModel{}
  123. iter := itemCollection.Find(nil).Limit(100).Iter()
  124. err := iter.All(&result)
  125. return result, err
  126. }
  127. func getItemsNotActed(actedItems []string) ([]ItemModel, error) {
  128. result := []ItemModel{}
  129. iter := itemCollection.Find(bson.M{"id": bson.M{"$nin": actedItems}}).Limit(100).Iter()
  130. err := iter.All(&result)
  131. return result, err
  132. }