Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* File responsible for owning the operations assigned to the user. */
- package main
- import (
- "fmt"
- "gopkg.in/mgo.v2"
- "gopkg.in/mgo.v2/bson"
- )
- /* Structure responsible for representing the user. */
- type User struct {
- UserName string `json: "username"`
- Email string `json: "email"`
- Password string `json: "password"`
- }
- /* Function responsible for including a user in the database. */
- func InsertUser(u User) bool {
- session, err := mgo.Dial("localhost:27017")
- if err != nil {
- return false
- }
- defer session.Close()
- // The database is called "crud-go-mongodb" and the collection is called "user"
- c := session.DB("crud-go-mongodb").C("user")
- err = c.Insert(u)
- if err != nil {
- return false
- }
- return true
- }
- /* Function responsible for getting all users. */
- func GetAllUsers() []User {
- session, err := mgo.Dial("localhost:27017")
- if err != nil {
- return nil
- }
- defer session.Close()
- // The database is called "crud-go-mongodb" and the collection is called "user"
- c := session.DB("crud-go-mongodb").C("user")
- var users []User
- err = c.Find(bson.M{}).All(&users)
- return users
- }
- /* Function responsible for validating a user through his username and password. */
- func Login(username string, password string) User {
- session, err := mgo.Dial("localhost:27017")
- user := User{}
- if err != nil {
- return user
- }
- defer session.Close()
- // The database is called "crud-go-mongodb" and the collection is called "user"
- c := session.DB("crud-go-mongodb").C("user")
- w := bson.M{"username": username, "password": password}
- err = c.Find(w).One(&user)
- return user
- }
- /* Responsible for updating the user record in the database, using as input parameter your username. */
- func UpdateUser(u User) bool {
- session, err := mgo.Dial("localhost:27017")
- if err != nil {
- return false
- }
- defer session.Close()
- // The database is called "crud-go-mongodb" and the collection is called "user"
- c := session.DB("crud-go-mongodb").C("user")
- w := bson.M{"username": "maxwellmoreira"}
- err = c.Update(w, u)
- if err != nil {
- return false
- }
- return true
- }
- /* Function responsible for delete a user in the database, using as input parameter his username. */
- func DeleteUser(username string) bool {
- session, err := mgo.Dial("localhost:27017")
- if err != nil {
- return false
- }
- defer session.Close()
- // The database is called "crud-go-mongodb" and the collection is called "user"
- c := session.DB("crud-go-mongodb").C("user")
- w := bson.M{"username": username}
- err = c.Remove(w)
- return true
- }
- /* Function responsible for running the program. */
- func main() {
- // 1- INSERT USER
- user1 := User{UserName: "maxwellmoreira", Email: "example@email.com", Password: "1234"}
- result1 := InsertUser(user1)
- _ = result1
- fmt.Printf("%+v\n", result1)
- // 2- SELECT ALL USERS
- result2 := GetAllUsers()
- _ = result2
- fmt.Printf("%+v\n", result2)
- // 3- UPDATE USER
- user3 := User{UserName: "maxwellmaxwell", Email: "example@email.com", Password: "1234"}
- result3 := UpdateUser(user3)
- _ = result3
- fmt.Printf("%+v\n", result3)
- // 4- LOGIN USER
- username_updated := "maxwellmaxwell"
- password := "1234"
- result4 := Login(username_updated, password)
- _ = result4
- fmt.Printf("%+v\n", result4)
- // 5- DELETE USER
- result5 := DeleteUser(username_updated)
- _ = result5
- fmt.Printf("%+v\n", result5)
- }
Add Comment
Please, Sign In to add comment