Guest User

Untitled

a guest
Apr 14th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. /* File responsible for owning the operations assigned to the user. */
  2.  
  3. package main
  4.  
  5. import (
  6. "fmt"
  7. "gopkg.in/mgo.v2"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10.  
  11. /* Structure responsible for representing the user. */
  12.  
  13. type User struct {
  14. UserName string `json: "username"`
  15. Email string `json: "email"`
  16. Password string `json: "password"`
  17. }
  18.  
  19. /* Function responsible for including a user in the database. */
  20.  
  21. func InsertUser(u User) bool {
  22.  
  23. session, err := mgo.Dial("localhost:27017")
  24.  
  25. if err != nil {
  26. return false
  27. }
  28.  
  29. defer session.Close()
  30.  
  31. // The database is called "crud-go-mongodb" and the collection is called "user"
  32.  
  33. c := session.DB("crud-go-mongodb").C("user")
  34.  
  35. err = c.Insert(u)
  36.  
  37. if err != nil {
  38. return false
  39. }
  40.  
  41. return true
  42.  
  43. }
  44.  
  45. /* Function responsible for getting all users. */
  46.  
  47. func GetAllUsers() []User {
  48.  
  49. session, err := mgo.Dial("localhost:27017")
  50.  
  51. if err != nil {
  52. return nil
  53. }
  54.  
  55. defer session.Close()
  56.  
  57. // The database is called "crud-go-mongodb" and the collection is called "user"
  58.  
  59. c := session.DB("crud-go-mongodb").C("user")
  60.  
  61. var users []User
  62.  
  63. err = c.Find(bson.M{}).All(&users)
  64.  
  65. return users
  66. }
  67.  
  68. /* Function responsible for validating a user through his username and password. */
  69.  
  70. func Login(username string, password string) User {
  71.  
  72. session, err := mgo.Dial("localhost:27017")
  73.  
  74. user := User{}
  75.  
  76. if err != nil {
  77. return user
  78. }
  79.  
  80. defer session.Close()
  81.  
  82. // The database is called "crud-go-mongodb" and the collection is called "user"
  83.  
  84. c := session.DB("crud-go-mongodb").C("user")
  85.  
  86. w := bson.M{"username": username, "password": password}
  87.  
  88. err = c.Find(w).One(&user)
  89.  
  90. return user
  91. }
  92.  
  93. /* Responsible for updating the user record in the database, using as input parameter your username. */
  94.  
  95. func UpdateUser(u User) bool {
  96.  
  97. session, err := mgo.Dial("localhost:27017")
  98.  
  99. if err != nil {
  100. return false
  101. }
  102.  
  103. defer session.Close()
  104.  
  105. // The database is called "crud-go-mongodb" and the collection is called "user"
  106.  
  107. c := session.DB("crud-go-mongodb").C("user")
  108.  
  109. w := bson.M{"username": "maxwellmoreira"}
  110.  
  111. err = c.Update(w, u)
  112.  
  113. if err != nil {
  114. return false
  115. }
  116.  
  117. return true
  118.  
  119. }
  120.  
  121. /* Function responsible for delete a user in the database, using as input parameter his username. */
  122.  
  123. func DeleteUser(username string) bool {
  124.  
  125. session, err := mgo.Dial("localhost:27017")
  126.  
  127. if err != nil {
  128. return false
  129. }
  130.  
  131. defer session.Close()
  132.  
  133. // The database is called "crud-go-mongodb" and the collection is called "user"
  134.  
  135. c := session.DB("crud-go-mongodb").C("user")
  136.  
  137. w := bson.M{"username": username}
  138.  
  139. err = c.Remove(w)
  140.  
  141. return true
  142.  
  143. }
  144.  
  145.  
  146. /* Function responsible for running the program. */
  147.  
  148. func main() {
  149.  
  150. // 1- INSERT USER
  151.  
  152. user1 := User{UserName: "maxwellmoreira", Email: "example@email.com", Password: "1234"}
  153.  
  154. result1 := InsertUser(user1)
  155.  
  156. _ = result1
  157.  
  158. fmt.Printf("%+v\n", result1)
  159.  
  160. // 2- SELECT ALL USERS
  161.  
  162. result2 := GetAllUsers()
  163.  
  164. _ = result2
  165.  
  166. fmt.Printf("%+v\n", result2)
  167.  
  168. // 3- UPDATE USER
  169.  
  170. user3 := User{UserName: "maxwellmaxwell", Email: "example@email.com", Password: "1234"}
  171.  
  172. result3 := UpdateUser(user3)
  173.  
  174. _ = result3
  175.  
  176. fmt.Printf("%+v\n", result3)
  177.  
  178. // 4- LOGIN USER
  179.  
  180. username_updated := "maxwellmaxwell"
  181. password := "1234"
  182.  
  183. result4 := Login(username_updated, password)
  184.  
  185. _ = result4
  186.  
  187. fmt.Printf("%+v\n", result4)
  188.  
  189. // 5- DELETE USER
  190.  
  191. result5 := DeleteUser(username_updated)
  192.  
  193. _ = result5
  194.  
  195. fmt.Printf("%+v\n", result5)
  196.  
  197. }
Add Comment
Please, Sign In to add comment